View Full Version : how to link a lot of files use gcc

March 29th, 2007, 11:14 AM
I have wrote a program and generated some files:
a.h a.cpp b.h b.cpp c.h c.cpp main.cpp
how to compile and link them in g++.

March 29th, 2007, 11:19 AM
This is from memory, about a decade ago, but from the command line...

g++ 1.cpp 2.cpp 3.cpp -o programname --flags --libs

making sure that you've got all your *.h files as '#include's.

hmmm.. I think that's right. Or you need a makefile.

March 29th, 2007, 01:00 PM
g++ 1.cpp 2.cpp 3.cpp -o programname

That's all right!Thank you!

March 29th, 2007, 02:48 PM
create a file called Makefile in your source directory.

in it put ...

CFLAGS=-g -Wall

all: main

main: a.o b.o c.o main.o
$(CC) $(CFLAGS) -o main main.o a.o b.o c.o

main.o: a.h b.h c.h main.cpp
$(CC) $(CFLAGS) -c main.cpp

a.o: a.ccp a.h
$(CC) $(CFLAGS) -c a.cpp

b.o: b.ccp b.h
$(CC) $(CFLAGS) -c c.cpp

c.o: c.cpp c.h
$(CC) $(CFLAGS) -c c.cpp

rm main main.o a.o b.o c.o

now run

and it will build your program.

if you make changes to the code it will rebuild only the files that it needs to.

make clean
cleans up all the compiled files.