This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Compiling static and shared libraries
- From: Jeffrey Walton <noloader at gmail dot com>
- To: "Andy Falanga (afalanga)" <afalanga at micron dot com>
- Cc: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Mon, 29 Apr 2013 13:58:35 -0400
- Subject: Re: Compiling static and shared libraries
- References: <60F6FAE47D1BCE4380CC06D18F49789B3030D34B at NTXBOIMBX02 dot micron dot com>
- Reply-to: noloader at gmail dot com
On Mon, Apr 29, 2013 at 12:27 PM, Andy Falanga (afalanga)
<afalanga@micron.com> wrote:
> Hello everyone,
>
> I have a library for both static compilation with C++ and a python module (exposed through boost python). The functionality is split so that everything C++ only is expected to be compiled into a set of static object files that are then wrapped into a single archive file. The python module must be compiled into a shared object file. Therefore, the build process builds a *.a for the static stuff and *.so for the python layer.
>
> I'm having issues with bringing the two together. Here is the compilation lines used for the various parts (excerpt from the makefiles):
>
> # the static archive library makefile is in a different directory from the shared
> SOURCES = File1.cpp File2.cpp File3.cpp
> outputdir/%.o: %.cpp
> $(CPP) -I/path/for/includes -std=c++0x -c -o $@ $<
>
> outputdir/archive.a:
> ar qc archive.a outputdir/*.o
Not sure if this matters, but CPP is the C preprocessor. I would
probably use something like:
outputdir/%.o: %.cpp
$(CXX) $(CXXFLAGS) -I/path/for/includes -std=c++0x -c -o $@ $<
CXXFLAGS should probably include -fPIC and -shared. Otherwise, you can
have problems in library constructors and destructors.
Also, when linking an archive in the final executable, you will want
to link with -Wl,--exclude-libs,ALL. Otherwise, you can get violations
of the One Definition Rule (ODR) *if* two different libraries each
include the archive. The symptom is often (from my experience) a glibc
crash due to a double free triggered by the destructors during unload.
Jeff