Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In C, you cannot have the function definition/implementation inside the header file. However, in C++ you can have full method implementation inside the header file. Why is the behaviour different?

share|improve this question

5 Answers 5

up vote 17 down vote accepted

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.)

share|improve this answer
    
It is not such a bad idea - this sort of things can be found even in the GNU Libc headers. –  SK-logic Mar 9 '11 at 9:49

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.

share|improve this answer

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.

share|improve this answer

The concept of a header file needs a little explanation:

Either you give a file on the command line of compiler, or do a '#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 take 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.

share|improve this answer

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.

share|improve this answer
    
No, there is actually a real answer, and one of the primary goals of C++ was to be backward compatible with C. –  Ed S. Mar 9 '11 at 8:51
3  
No, it was designed to have "A High degree of C compatibility", and "No gratuitous incompatibilities with C". (both from Stroustrup). I agree that a more in-depth answer could be given, to highlight why this particular incompatibility is not gratuitous. Feel free to supply one. –  Paul Butcher Mar 9 '11 at 9:19
    
I would have, but Simon Richter already had when I posted that. We can quibble about the difference between "backward compatible" and "A High degree of C compatibility", but the fact remains that this answer is incorrect. The last statement would be correct if we were comparing C# and C++, but not so much with C and C++. –  Ed S. Mar 9 '11 at 18:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.