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?
|
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.) |
|||
|
|
C and C++ behave very much the same in this regard -- you can have |
|||
|
|
|
You can do this in C99: |
|||
|
|
|
The concept of header file needs a little explanation: Either you give a file on command line of compiler, or do a '#include' Most of the compiler accept that a command file with extension 'c/C/cpp/c++ etc' as source file. However they usually include a command line option to enable usage of any arbitrary extension to source file also. Generally the file given on command line is called 'Source', and the one included is called 'Header'. The prepeocessor step actually take them all .. and makes everything appears 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 compiler which can show the output of this stage. So for each file that was given on compiler command line , a huge file is given to compiler. This may have code/data which will occupy memory and/or create symbol to be refrenced from other files. Now each of this will generate an 'object' image. The linker can give a 'duplicate symbol' if 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 header file, which can create symbols in obj file. The 'inline' are usually inlined .. but when debugging they may not be inlined. So why does the linker doesn't give multiply defined error ? 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 . |
|||
|
|
|
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. |
|||||||||
|
