how to compile files together in Linux
I'm currently using a text to learn C programming, in which an instructive program (below) uses separate files, but talks only of Unix & DOS environments for how to compile them together. Wondering if anyone can explain how to "compile them together" in the Unix environment (using clang compiler)? All 3 files are in the same directory.
>>>>>>>>>>>>>>> PROGRAM 9.9 Code:
/* usehotel.c -- room rate program */ Code:
/* hotel.c -- hotel management functions */ Code:
/* hotel.h -- constants and declarations for hotel.c */ |
What you have here are two .c files and a header file that are parts of the same program.
The third file is supposed to have the filename "hotel.h" - it even says as much in the comments. It is pulled in by the #include "hotel.h" statement in the first file. The second file by itself is not a program at all, as it lacks the "main" function. It's pretty obvious that it belongs to the same program as the first file, as it contains functions that are used but never defined in the first file. Either #include the second file in the first, or just join the two files together with cut and paste as the comment in the first file suggests. |
Sorry, but that doesn't seem to jibe with the text discussion, where they speak of such things as a command line such as "gcc file1.c file2.c for Unix compilers.
In this case, there are 2 separate .c files (and the .h file). The text discusses the use of putting the functions related to the driver in a separate file, and then putting constants, etc in a separate header file. |
One basic way to do it is:
Code:
gcc -c -o usehotel.o usehotel.c http://www.linuxquestions.org/linux/...grams_on_Linux http://pages.cs.wisc.edu/~beechung/ref/gcc-intro.html And some would actually prefer to use ld like many open-source softwares. I did prefer it before. http://stackoverflow.com/questions/5...utable-with-ld |
> they speak of such things as a command line such as "gcc file1.c file2.c for Unix compilers.
That works perfectly well, try this: Code:
gcc -o hotel hotel.c usehotel.c |
Yes! Tutorials very helpful. Are gcc compilers present in Unix & Linux?
How does clang differ from gcc? |
gcc compiler definitely exists in Linux: g=gnu, as in GNU/Linux :)
|
All times are GMT -5. The time now is 11:45 AM. |