[TriLUG] newbie question about g++

Jeff Jackowski jeffj1 at hiwaay.net
Sun Dec 15 00:17:04 EST 2002


On Sat, 14 Dec 2002, ray bordt wrote:

>i have three files: dive.h, dive.cpp, and divemain.cpp
>i tried g++ -o divemain.o divemain.cpp and receive errors.
>dive.h is included in divemain.cpp and  dive.cpp.
>how can i compile dive.cpp and divemain.cpp at the same time?

You don't. You compile them separately, and then link them together.
First, run these two commands to compile your code into separate object
files:
	g++ -c dive.cpp
	g++ -c divemain.cpp

Then, link the object files together with this command:
	g++ -o dive dive.o divemain.o

If it compiles, you'll get an executable named dive.

Alternately, you could make a makefile to do the work. That way you just
run make and it'll compile the program rather than issuing three commands
every time yourself. It might look something like this, but I haven't
tested it, so it might not (do not convert the tabs to spaces):

dive: dive.o divemain.o
	g++ -o dive dive.o divemain.o

dive.o: dive.cpp dive.h
	g++ -c dive.cpp

divemain.o: divemain.cpp dive.h
	g++ -c divemain.cpp

dive.cpp: dive.h

divemain.cpp: dive.h

-- 
Jeff Jackowski
        http://ro.com/~jeffj/




More information about the TriLUG mailing list