How to Use Your Own Static Libraries with g++ like the Standard Libraries

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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?

Thanks very much for the help in advance!
Please also reply to me directly if you answer, as I am off-list.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux