On 29 June 2011 22:11, GM wrote: > I have my own libraries I've written in C++. So for instance, I have > written "cartman.h" and "cartman.cpp" and I have managed to end up > with "cartman.h" and "libcartman.a" for use in my own programs. > > The issue I'm having is that I would like to be able to use my own > libraries in the same manner as I can use the standard C++ libraries. > > If I do something like... > > #include <iostream> > #include <vector> > > ....then when I compile my program, g++ auto-magically finds the > correct libraries and links them up with my code. > > On the other hand, if I try to use my libraries in this way.... > > #include "cartman.h" > > ...then I must go the extra step of telling g++ to find and use my > library by name... > > g++ -o myprogram.exe -lcartman object1.o object2.o [etc] > > This extra step seems not nearly as elegant as using the standard > libraries, and also seems like it would quickly get unwieldy and > tedious in a large program. > > > > So I considered doing something like this in my makefile... > > mylibs = -lcartman -lkenny -lstan -letc > CC = g++ -L/path/to/mylibs/ $(mylibs) > > program.exe: program.o object1.o object2.o [etc] > $(CC) -o program.exe program.o object1.o object2.o [etc] > > However, this results in warning messages from ld whenever any of the > libraries specified aren't used to link with anything. For instance, > if I #included "cartman.h" and "kenny.h" but did not use "stan.h", > then when I compile my program, ld warns me that "libstan.a" was not > used during the compilation of my program. Also very irritating and > inelegant. I would rather not get into the habit of just ignoring > tons of error messages from my linker. > > I would like to be able to just #include "cartman.h" and have g++ > magically Do The Right Thing (TM) and link with libcartman.a if > necessary, and just not use libcartman.a if it's not necessary. Just > like the way things Just Work if you include a standard library like > <vector> or <iostream>. > > How can I do that? You can't with GCC. Some compilers support an auto-linking feature via a special #pragma, but GCC doesn't. One solution is to fix your makefile, as the problem comes because you have unneeded libs in the $(CC) command. Instead of always listing all the libraries in $(CC) consider: CC=gcc LDFLAGS = -L/path/to/mylibs/ program.exe: program.o object1.o object2.o [etc] $(CC) -o $@ $^ $(LDFLAGS) -lcartman -lkenny i.e. program.exe only links to the required libraries. You can also do that like this: program.exe: LIBS += -lcartman -lkenny program.exe: program.o object1.o object2.o [etc] $(CC) -o $@ $^ $(LDFLAGS) $(LIBS) $ make gcc -c -o program.o program.c gcc -c -o object1.o object1.c gcc -c -o object2.o object2.c gcc -o program.exe program.o object1.o object2.o -L. -lcartman -lkenny The advantage is you use the same compilation rule for other programs, just changing $(LIBS) prog2.exe: LIBS += -lstan -lkenny prog2.exe: prog2.o object1.o object3.o [etc] $(CC) -o $@ $^ $(LDFLAGS) $(LIBS) $ make prog2.exe gcc -c -o prog2.o prog2.c gcc -c -o object3.o object3.c gcc -o prog2.exe prog2.o object1.o object3.o -L. -lstan -lkenny