Defining C ++ functions inside header files

I want to know if it is good to store regular C ++ functions and not methods (the ones in classes) inside header files.

Example:

#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

int add(int a, int b)
{
   return a + b;
}

#endif

      

And use it like this:

#include <iostream>
#include "Functions.h"

int main(int argc, char* args[])
{
    std::cout << add(5, 8) << std::endl;
    return 1;
}

      

Is this good good practice? Thanks to

+3


source to share


3 answers


If you want to use a function in multiple source files (or rather translation units ), then you put the function declaration (i.e. the function prototype) in the header file and the definition in the same source file.

When you build, you first compile the source files to object files and then link the object files to the final executable.


Sample code:

  • Header file

    #ifndef FUNCTIONS_H_INCLUDED
    #define FUNCTIONS_H_INCLUDED
    
    int add(int a, int b);  // Function prototype, its declaration
    
    #endif
    
          

  • First source file

    #include "functions.h"
    
    // Function definition
    int add(int a, int b)
    {
        return a + b;
    }
    
          

  • Second source file

    #include <iostream>
    #include "functions.h"
    
    int main()
    {
        std::cout << "add(1, 2) = " << add(1, 2) << '\n';
    }
    
          



How you build it depends a lot on your environment. If you are using an IDE (like Visual Studio, Eclipse, Xcode, etc.), you put all the files in the project in the correct places.

If you are building from the command line eg. Linux or OSX then you like

$ g++ -c file1.cpp
$ g++ -c file2.cpp
$ g++ file1.o file2.o -o my_program

      

The flag -c

tells the compiler to generate an object file and name it the same as the original one, but with a suffix .o

. The last command links the two object files together to form the final executable file, and names it my_program

(which the parameter does -o

, reports the name of the output file).

+13


source


Not. If you import the same header from two files, you get a function override.

However, usually if the function is inline. Every file needs a definition in order to generate code, which is why people usually put the definition in the header.



Usage static

also works because static functions are not exported from the object file and thus cannot interfere with other functions of the same name during communication.

It is also possible to define member functions inside the header class

, as the C ++ standard treats them as inline

.

+6


source


Not. After preprocessing, each source file will contain a header file. Then, in the link step, you will get a multiple definition error because you will have multiple definitions of the same function.

Use inline

or static

get rid of the linking error. If you don't want the function to be inline

, your best bet is to declare the function in the header and define it in the same source file and link it.

If you declare a function as inline

, then each function call in the source file will be replaced by the code inside the inline

d function . Thus, no additional character is defined.

If you declare a function as static

, then the function symbol will not be exported from the translation unit. Therefore, there are no duplicate symbols.

-1


source







All Articles