|
Boost-Build : |
From: Roland Schwarz (roland.schwarz_at_[hidden])
Date: 2007-08-31 12:29:02
Repeatedly I have heard, that on linux it is not possible to create a
shared library that is statically bound to a runtime lib.
Boost.Build simply bails out before even trying, because of this believe.
While I do understand that this is questionable practice in general, or
as the boost auto_link headers tell: "Mixing a dll boost library with a
static runtime is a really bad idea...", I was wondering if the "not
possible" also is true.
Though in practice I can imagine situations where it might be useful to
be able to do this: Shipping a shrink wrapped lib; need to be able to
link to two different runtime libs; creating a lib that will link to a
foreign language e.g. C ;-); ... (your idea here) ...
I know these examples are not tremendously convincing. But knowing where
the borders really are can be a helpful thing.
So I grabbed the man pages and created the attached example.
I am sure I overlooked something fundamental ;-) but the example
seemingly works.
I would be glad to hear opinions of experts in this field.
Roland aka speedsnail
#!/bin/sh
echo "**** compiling foo.cpp ****"
cat <<FOO > foo.cpp
#include <iostream>
extern "C" char* foo_s;
extern "C" char* foo()
{
std::cout << "in foo" << std::endl;
static char* s = "foo" ;
return s;
}
FOO
g++ -c foo.cpp -o foo.o
echo "**** compiling bar.cpp ****"
cat <<BAR > bar.cpp
#include <iostream>
extern "C" char* bar()
{
std::cout << "in bar" << std::endl;
static char* s = "bar" ;
return s;
}
BAR
g++ -c bar.cpp -o bar.o
echo "**** compiling baz.cpp ****"
cat <<BAZ > baz.cpp
#include <iostream>
extern "C" char* baz()
{
std::cout << "in baz" << std::endl;
static char* s = "baz" ;
return s;
}
BAZ
g++ -c baz.cpp -o baz.o
echo "**** create shared foobar lib with static runtime ****"
g++ -o libfoobar-s.so -shared -Wl,--start-group foo.o bar.o -Wl,--end-group -Wl,--strip-all -Wl,--exclude-libs,ALL,-Bstatic -static-libgcc
echo "**** create shared baz lib with static runtime ****"
g++ -o libbaz-s.so -shared -Wl,--start-group baz.o -Wl,--end-group -Wl,--strip-all -Wl,--exclude-libs,ALL,-Bstatic -static-libgcc
echo "**** create shared foobar lib ****"
g++ -o libfoobar.so -shared -Wl,--start-group foo.o bar.o -Wl,--end-group
echo "**** compiling main.cpp ****"
cat <<MAIN_CPP > main.cpp
#include <iostream>
extern "C" char* foo();
extern "C" char* bar();
extern "C" char* baz();
int main(int argc, char* argv[])
{
std::cout << "Hello" << foo() << bar() << baz() << std::endl;
return 0;
}
MAIN_CPP
g++ -c main.cpp -o main_cpp.o
echo "**** compiling main.c ****"
cat <<MAIN_C > main.c
#include <stdio.h>
char* foo();
char* bar();
char* baz();
int main(int argc, char* argv[])
{
printf("Hello%s%s%s\n", foo(), bar(), baz());
return 0;
}
MAIN_C
gcc -c main.c -o main_c.o
echo "**** link main_c with foobar-s baz-s ****"
gcc main_c.o -o main_c -L. -lfoobar-s -lbaz-s
echo "**** link main_cpp with foobar-s baz-s ****"
g++ main_cpp.o -o main_cpp -L. -lfoobar-s -lbaz-s
echo "**** dependencies of main_c ****"
ldd main_c
echo "**** run main_c ****"
./main_c
echo "**** dependencies of main_cpp ****"
ldd main_cpp
echo "**** run main_cpp ****"
./main_cpp
Boost-Build list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk