-
gcc or g++ Question
I'm working through a Linux programming book, however the first half is all on shell scripting, the latter half is on gcc/g++ and make files. I glanced at the gcc part and learned that I can do a simple compile like so:
gcc -o newname filename.cpp
Well, I want to read this book sequentially, however can someone quickly help me out and tell me what the command line option is for building a project with multiple files: Say I have these files:
Set.h
Pet.h
source1.c
source2.c
How would I compile this with gcc. I glanced at the chapter on make files. I don't want to get into that just yet. What is the long solution?
gcc ?
Yes I am lazy. I didn't even try to anything. Just give me the answer anyway!
-
I assume that those headers are only headers and they are already #included in the files?
gcc -c source1.c
gcc -c source2.c
Now you have source1.o and source2.o
gcc -o program source1.o source2.o
If you have two source files then Makefile would speed up your developing process, because it compiles only those files which have changed since last compilation. Here is a suitable Makefile for this:
Code:
# Makefile
# <tabs>('\t') are replaced with 8 spaces
program: source1.o source2.o
gcc -o program source1.o source2.o
source1.o: source1.c
gcc -c source1.c
source2.o: source2.c
gcc -c source2.c
#
# Makefile ends
-
I saw something like this in the book, but I was skipping ahead too far and didn't know if I was missing something. I was wondering if the .h file needed to be specified, however I see that it just needs to be found in the sources and that it will be preprocessed. I'll test it out soon.
-
> I was wondering if the .h file needed to be specified
Yes they do (if the makefile is going to work all the time)
For instance
source1.o: source1.c pet.h
This would tell make to recreate source1.o if either (or both) source1.c or pet.h has changed since source1.o was last updated.
In this example, I've assumed that source1.c includes pet.h
You should really list all the include files each .c file has on the dependency line
Fortunately, the command
gcc -M source1.c
Does most of this work for you (there are other options like -MM, see your manual :) )