include question
I'm currently following "Internetworking with TCP /IP vol III" by Comer, but I'm having trouble compiling his examples, and been forced to change the include / declarations in all his examples. E.g his example.
int some_function_in_seperate_c__file(parameterts); int current_function() { return some_function_in_sperate_c_file(p); } I not able to compile this, unless I include the c file #include "some_function_in...c" Can this be right?! If so, how come the examples don't how this in the book (I know that it might be a long shot comparing examples, but I think that this is a widely read book). I'm used to c++ , and just trying to get a feel of the C syntax. Regards Jasper |
Normally, you compile only the .c files not the .h files, so to compile proprely a .c file with a function declared in a .h file, you include the .h file in the .c file.
|
Well actually I haven't made matching header files for the function (nor does he do that in the examples). It's a single c file, containing a single function. In the book he only declares the function prototype, but does not include the *.c file. Because of this, I'm not able to compile without including the matching *.c file.
Is it even possible to use a function, with just declaring the function prototype? |
Quote:
- Other #includes - #defines - struct (or class) definitions - function prototypes In other words: things the compiler needs to know, but no code (inline member functions of a C++ class being the exception). Quote:
For programs larger than, say, a book example, it is normally recommended to put function prototypes in a header *.h) file, and include it in c-files that need the definitions/prototypes that are in the header. Now about your main problem: When you have a program that is split into seperate C-files (.c), you compile them seperately into object-files (.o). The object-files contain the compiled machine-code, but they are not executable: they need to be linked together and some added code/information to become one executable file. When you have a one-c-file program, and do "gcc -o program program.c", the compiler is smart enough to do both the compiling and linking in one go. Here's a basic example of a two c-file + one header file program: Code:
/* hello.h */ Code:
/* hello.c */ Code:
/* hello_main.c */ (note the -c option to gcc to tell it to compile only and not try to link (which would fail, because hello.c doe not have a main() function): Code:
gcc -Wall -c -o hello_main.o hello_main.c Code:
gcc -Wall -o hello_program hello.o hello_main.o Code:
./hello_program Make is another subject on its own right.. |
All times are GMT -5. The time now is 01:29 AM. |