[prog] C++ Inheritance

Mary mary-linuxchix at puzzling.org
Sat May 24 18:07:47 EST 2003


On Sat, May 24, 2003, Sue Stones wrote:
>   Would someone mind giving me some pointers on the following error message.
>  I just cant see what is going wrong.
> 
> I have 3 files, each befining with  a comment block, included below
> 
> The output when I attemp to compile the program is below.
> 
>  make
> g++ -o ftest fstest.C
> /tmp/cclfc7rJ.o: In function `main':
> /tmp/cclfc7rJ.o(.text+0x1a): undefined reference to 
> `fstream_ext::fstream_ext(int, char const *, int)'
> collect2: ld returned 1 exit status
> make: *** [fstest] Error 1

There are two ways to compile C++ (and C) programs. Either you compile
every file you need at once:

g++ -o [executable name] [filenames]

eg: g++ -o myprog *.C

or you compile each file one by one into an object file (with the -c
flag, which tells g++ not to attempt to link the output of this compile
together into a working program) and then link them together:

eg: g++ -o myfile.o -c myfile.C
    g++ -o myfile2.o -c myfile2.C

    g++ -o myprog myfile.o myfile2.o

If you don't use the -c flag, g++ will try and link your program into a
working executable, and myfile.C won't compile into a working
execuatable if it relies on functions implemented in myfile2.C

Incidently, the reason you can make object files and link them all
together later is this: imagine you have a project that takes 12 hours
to compile. You change 1 line. You wish to test it. Instead of doing the
entire 12 hour compile again, you recompile that file into an object
file, and then do the linking step with all the other object files that
are unchanged from old compiles.

-Mary


More information about the Programming mailing list