C++ Library/Link Question

Rob Wehrli plug-devel@lists.PLUG.phoenix.az.us
Thu Jul 12 13:47:01 2001


Douglas Jerome wrote:
> 
> So, if this list is not apropriate for this sort of direct
> question, let me know and I wont do it (anymore). But, I
> have a question about linking together C++ code.
> 
> I compile several C++ files (f1.cc, f2.cc, and f3.cc) and
> incrementally link them together:
> 
>         ld -r -o lib.o f1.o f2.o f3.o
>                 or
>         ar r lib.a f1.o f2.o f3.o
> 
> then I compile some other files (main.cc, p1.cc, and p2.cc)
> with a main and link them together with lib.o from above:
> 
>         g++ -o prog main.o p1.o p2.o lib.o
>                 or
>         g++ -o prog main.o p1.o p2.o lib.a
> 

You'll want to name your library file as follows:  lib(NAME).a

EG:  	ar -r libmylib.a *.o

Then specify on your commandline:

g++ -o prog main.o -L. -lNAME (any .o files not in libNAME.a)

...or you could simply rename your current lib.a file to liblib.a and
your

>         g++ -o prog main.o p1.o p2.o lib.a

Should work with the -L. -llib switches added...you can also add the
-static switch to the commandline.

> It works fine for gcc, but doesn't work for g++; the
> code in main.cc, p1.cc, and p2.cc that try to make objects
> from the classes in lib.o can't find them. There are
> unresolved references, as if I didn't have lib.o involved
> at all.
> 
> Anyone know where I can find information on making and using
> a library with C++?

man ar :)

> 
> Thanks.
> 
> --
> Douglas Jerome <djerome@users.sourceforge.net>

Take Care.

Rob!