views:

1151

answers:

2

Hi,

I want to create a shared library from several static libs using GCC under OS X.

In some static libs, there's no code in shared library call it, I just want to export the symbols in these static libs. This works under debug mode, but doesn't under release mode (especially when I enable the dead code striping). I can understand the reason, gcc think these functions on static libs are never used. but how can I force gcc to include these symbols?

I already tried adding -u option for loader, but it only generates a 'local' symbol. how to make linker generate an export symbol?

Also, I'm wondering if there's a way to add the linker directives in source code, just like the MSVC #pragrma comment(linker, "/INCLUDE:xxxx")

the function I defined in static lib is like:

extern "C"
void test() {}

Thanks in advance! -Jonny

+1  A: 

Have you tried --whole-archive?

John Weldon
A: 

Use ar to disassemble the static libraries into their constituent object files. Then link those objects together to make the shared library.

ar -x libstatic.a
(produces a bunch of *.o files)
gcc -shared -olibshared.so *.o # Linux
ld -dylib -olibshared.dylib *.o # Mac OSX

d3jones