我目前正在尝试在一个共享库中使用C++ 11多线程,该库被加载到Linux上的主程序(用C语言编写)中.这是大型模拟程序的一部分,我无法改变有关库加载或更改主程序的任何内容.
主程序是用gcc 4.1.2编译的,我没有它的源代码(我不能用gcc 4.8.2重新编译它).
共享库使用gcc 4.8.2编译,以便使用C++ 11多线程.我正在传递编译器命令
-pthread -lpthread -std=c++11
Run Code Online (Sandbox Code Playgroud)
正如在Linux下的GCC中使用std :: thread的正确链接选项中所解释的那样?
使用此配置(" -pthread -std=c++11"和gcc 4.8)编译独立测试程序在我的系统上正常工作.但是当我启动加载共享库的程序时,我得到一个异常:
Caught std::exception!
Exception Message: Enable multithreading to use std::thread: Operation not permitted
Terminating...
Run Code Online (Sandbox Code Playgroud)
使用-pthread和-lpthread(编辑:也只是-pthread没有-lpthread)编译参数不起作用.编译器参数是(我正在使用cook构建系统):
-pthread -std=c++11 -fmessage-length=0 -fPIC -Wchar-subscripts ...(lots of -W* here)
... -Wunused-variable -m64 -D__64BIT__ -pthread -lpthread
Run Code Online (Sandbox Code Playgroud)
和链接器参数(由于构建系统而产生的重复参数):
-pthread -lpthread -std=c++11 -pthread -lpthread -std=c++11 -shared -fPIC -Wl,-Bsymbolic -Wl,--allow-shlib-undefined -pthread -lpthread
Run Code Online (Sandbox Code Playgroud)
在我的库上调用ldd会给出以下输出
$ ldd calc3/build/amd64_linux26_RH5/library.so
linux-vdso.so.1 => …Run Code Online (Sandbox Code Playgroud)