Trying to statically link Boost

后端 未结 3 1932
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 08:22

I am working in Linux, Eclipse CDT, g++, with Boost library. Having existing program which uses Boost thread, I try to link it statically instead of dynamically. /usr/local/lib

3条回答
  •  迷失自我
    2021-02-04 08:54

    For pthread_mutex_init, you want to compile/link with -pthread option:

    g++ -static -pthread -o"MyProgram"  ./main.o   -lboost_thread
    

    The problem is that functions like pthread_mutex_init are in a separate library. Dynamic libraries can include the metadata for the fact that it needs the separate library (so libboost_thread.so includes the fact that it needs libpthread).

    But static libraries don't have that information. So you need to provide the reference to any necessary libraries when you link statically.

    As for using -pthread instead of -lpthread, it's slightly preferable because it not only links the necessary library, but provides any other options that should be used (such a -D_REENTRANT to the compiler).

提交回复
热议问题