In C, you cannot have the function definition/implementation inside the header file. However, in C++ you can have a full method implementation inside the header file. Why is the behaviour different?
6 Answers
In C, if you define a function in a header file, then that function will appear in each module that is compiled that includes that header file, and a public symbol will be exported for the function. So if function additup is defined in header.h, and foo.c and bar.c both include header.h, then foo.o and bar.o will both include copies of additup.
When you go to link those two object files together, the linker will see that the symbol additup is defined more than once, and won't allow it.
If you declare the function to be static, then no symbol will be exported. The object files foo.o and bar.o will still both contain separate copies of the code for the function, and they will be able to use them, but the linker won't be able to see any copy of the function, so it won't complain. Of course, no other module will be able to see the function, either. And your program will be bloated up with two identical copies of the same function.
If you only declare the function in the header file, but do not define it, and then define it in just one module, then the linker will see one copy of the function, and every module in your program will be able to see it and use it. And your compiled program will contain just one copy of the function.
So, you can have the function definition in the header file in C, it's just bad style, bad form, and an all-around bad idea.
(By "declare", I mean provide a function prototype without a body; by "define" I mean provide the actual code of the function body; this is standard C terminology.)
- 103
- 864
- 8
- 9
C and C++ behave very much the same in this regard -- you can have inline functions in headers. In C++, any method whose body is inside the class definition is implicitly inline. If you want to do the same in C, declare the functions static inline.
- 1,618
The concept of a header file needs a little explanation:
Either you give a file on the command line of compiler, or do an #include.
Most of the compilers accept a command file with extension c, C, cpp, c++, etc. as the source file. However, they usually include a command-line option to enable usage of any arbitrary extension to a source file also.
Generally the file given on the command line is called 'Source', and the one included is called 'Header'.
The preprocessor step actually takes them all and makes everything appear like a single big file to the compiler. What was in the header or in the source is actually not relevant at this point. There is usually an option of a compiler which can show the output of this stage.
So for each file that was given on the compiler command line, a huge file is given to the compiler. This may have code/data which will occupy memory and/or create a symbol to be referenced from other files. Now each of these will generate an 'object' image. The linker can give a 'duplicate symbol' if the same symbol is found in more than two object files which are being linked together. Perhaps this is the reason; it's not advised to put code in a header file, which can create symbols in the object file.
The 'inline' are usually inlined .. but when debugging they may not be inlined. So why does the linker not give multiply defined errors? Simple... these are 'weak' symbols, and as long as all the data/code for a weak symbol from all the objects are same size and content, the linked will keep one copy and drop copy from other objects. It works.
- 3,167
- 251
You can do this in C99: inline functions are guaranteed to be provided somewhere else, so if a function was not inlined, its definition is translated into a declaration (i.e., implementation is discarded). And, of course, you can use static.
- 8,517
C++ standard quotes
The C++17 N4659 standard draft 10.1.6 "The inline specifier" says that methods are implicitly inline:
4 A function defined within a class definition is an inline function.
and then further down we see that inline methods not only can, but must be defined on all translation units:
6 An inline function or variable shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case (6.2).
This is also explicitly mentioned in a note at 12.2.1 "Member functions":
1 A member function may be defined (11.4) in its class definition, in which case it is an inline member function (10.1.6) [...]
3 [ Note: There can be at most one definition of a non-inline member function in a program. There may be more than one inline member function definition in a program. See 6.2 and 10.1.6. — end note ]
GCC 8.3 implementation
main.cpp
struct MyClass {
void myMethod() {}
};
int main() {
MyClass().myMethod();
}
Compile and view symbols:
g++ -c main.cpp
nm -C main.o
output:
U _GLOBAL_OFFSET_TABLE_
0000000000000000 W MyClass::myMethod()
U __stack_chk_fail
0000000000000000 T main
then we see from man nm that the MyClass::myMethod symbol is marked as weak on the ELF object files, which implies that it can appear on multiple object files:
"W" "w" The symbol is a weak symbol that has not been specifically tagged as a weak object symbol. When a weak defined symbol is linked with a normal defined symbol, the normal defined symbol is used with no error. When a weak undefined symbol is linked and the symbol is not defined, the value of the symbol is determined in a system-specific manner without error. On some systems, uppercase indicates that a default value has been specified.
Probably for the same reason that you must put the full method implementation inside the class definition in Java.
They might look similar, with squiggly brackets and many of the same keywords, but they are different languages.
- 2,817