42

I'm wondering if it's a good practice to store C++ regular functions, 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 a good a good practice? Thanks in advance!

Edwin Pratt
  • 745
  • 9
  • 19
Nobody
  • 475
  • 1
  • 6
  • 9
  • 14
    No, you're inevitably going to violate the One Definition Rule. – chris Aug 12 '14 at 21:27
  • 1
    Think about what would happen if the header file was included in two or more source files. – Some programmer dude Aug 12 '14 at 21:28
  • 3
    @chris Why? Aren't the include guards precisely to prevent that? – Daniel Daranas Aug 12 '14 at 21:29
  • 13
    @DanielDaranas No, the include guards protect against including the same header file multiple times *in the same source file*. It doesn't protect against inclusion over multiple source files (how would that even work if compiling source files separately into object files?) Also read about [translation units](http://en.wikipedia.org/wiki/Translation_unit_%28programming%29) (And the OP: You should read the link too) – Some programmer dude Aug 12 '14 at 21:29
  • so is there any other way i can store my functions in another file, then include it in my program? – Nobody Aug 12 '14 at 21:29
  • You can create a class that has all your needed small functions and make them public and static so those that include the header have access to them. – Benjamin Trent Aug 12 '14 at 21:31
  • can you please show some code sample? – Nobody Aug 12 '14 at 21:32
  • 11
    You would have to mark the function `inline` to avoid breaking the *one definition rule*. – juanchopanza Aug 12 '14 at 21:34
  • But it's still a bad idea as it harms compile times. – Neil Kirk Aug 12 '14 at 21:48
  • @JoachimPileborg Thank you, I got it now. A very interesting difference. – Daniel Daranas Aug 12 '14 at 23:58
  • Remember that if changes are made to one or more functions in the header file, all files including that header file must be rebuilt. If the changes were made to functions in a source file, only that source file would need to be rebuilt. – Thomas Matthews Aug 13 '14 at 00:27

3 Answers3

70

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

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


Example 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 very much on your environment. If you are using an IDE (like Visual Studio, Eclipse, Xcode etc.) then you put all files into the project in the correct places.

If you are building from the command line in, for example, Linux or OSX, then you do:

$ 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 source file but with a .o suffix. The last command links the two object files together to form the final executable, and names it my_program (that's what the -o option does, tells the name of the output file).

Matias Chara
  • 841
  • 5
  • 19
Some programmer dude
  • 380,411
  • 33
  • 383
  • 585
24

No. After preprocessing, each source file will contain the header file. Then, at the linking stage you will end up with a multiple definition error because you will have multiple definitions of the same function.

Using inline or static will get rid of the linking error. Unless you want the function to be inline, it is best to declare the function in the header and define it in a single source file and link it.

If you declare the function as inline, then each of its function call in the source file will be replaced with the code inside the inlined function. So, there's no extra symbol defined.

If you declare the function as static, then the function symbol will not be exported from the translation unit. Therefore, no duplicate symbols.

Adding to what is said above, a function defined entirely inside a class/struct/union definition, whether it's a member function or a non-member friend function , is implicitly an inline function. So you do not need to explicitly write inline for the mentioned situations.

Koi.
  • 3
  • 2
Nikopol
  • 933
  • 1
  • 10
  • 23
22

No. If you import the same header from two files, you get redefinition of function.

However, it's usual if the function is inline. Every file needs it's definition to generate code, so people usually put the definition in header.

Using static also works because of fact that static functions are not exported from object file and in this way can't interfere with other functions with the same name during linkage.

It's also OK to define member functions inside the class in header as C++ standard considers them as inline.

Alexey Shmalko
  • 3,558
  • 1
  • 16
  • 35