简体   繁体   中英

cmake: how to create library with statically linked boost (or embed boost)

I would like to build library (say "mylib", static or shared, doesn't really matter) that internally uses boost (boost::asio in particular). Library public interface doesn't depend on any boost types.

Library supposed be used by Qt application, and I don't want to introduce any boost dependencies into app's build scripts (it uses qmake currently) and I need the "mylib" to be only dependency. Library build system is CMake. I tried to build shared library with statically linked boost but I'm getting errors like:

/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_system.a(error_code.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/lib/x86_64-linux-gnu/libboost_system.a: error adding symbols: Bad value

Primary target target/toolchain is gcc x64 (Linux) but it would be good to have crossplatform solution (at least mingw64 and MacOS X).

Creating static library is an option too, but I couldn't figure out how to include static boost libs into "mylib"

You can't use code built for a static library inside a dynamic library. Shared libraries require position-independent code. You could take the object files that were used to build Boost shared libraries, and stuff them all into a .a file and use that. Or munge your Boost build to add -fPIC to the flags for compiling its static libraries.

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