简体   繁体   中英

GCC How to export a function from static library

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

您是否尝试过--whole-archive

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM