在编译一个简单的多线程程序时,我似乎无法将pthread的共享对象链接起来。我收到以下错误:
/data/hunyadi/usr/bin/../lib/gcc/x86_64-pc-linux-gnu/7.1.0/../../../../lib64/libpthread.a(libpthread.o): In function `sem_open':
(.text+0x77cd): warning: the use of `mktemp' is dangerous, better use `mkstemp'
/cvmfs/cms.cern.ch/slc6_amd64_gcc491/external/gcc/4.9.1-cms/bin/ld: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality in `/data/hunyadi/usr/bin/../lib/gcc/x86_64-pc-linux-gnu/7.1.0/../../../../lib64/libc.a(strcmp.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie
collect2: error: ld returned 1 exit status
我尝试重新安装glibc
,但错误仍然存在。 (使用CentOs 6
)。
请求的最小示例:
#include <future>
#include <iostream>
#include <string>
#include <vector>
int main()
{
try
{
auto test_lambda = [] (int a, int b, int c) -> void { std::cout << "a: " << a << " b: " << b << " c: " << c << "." << std::endl; };
std::future<void> test_future = std::async(test_lambda, 1, 2, 3);
std::chrono::milliseconds milliseconds_100(100);
while(test_future.wait_for(milliseconds_100) == std::future_status::timeout)
{
std::cout << "." << std::endl;
}
}
catch(const std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
使用gcc 7.1.0
进行编译器调用:
g++ src/test.cc -std=c++14 -pthread
上面的错误信息就是我得到的。
如果我添加-fPIE和-pie,这是我得到此错误的输出:
g++ src/test.cc -std=c++14 -pthread -fPIE -pie
/cvmfs/cms.cern.ch/slc6_amd64_gcc491/external/gcc/4.9.1-cms/bin/ld: /data/hunyadi/usr/bin/../lib/gcc/x86_64-pc-linux-gnu/7.1.0/../../../../lib64/libpthread.a(libpthread.o): relocation R_X86_64_32S against `__stack_user' can not be used when making a shared object; recompile with -fPIC
/data/hunyadi/usr/bin/../lib/gcc/x86_64-pc-linux-gnu/7.1.0/../../../../lib64/libpthread.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
答案 0 :(得分:0)
我设法通过本地安装gcc 5.3.0
并将libphread.so
文件替换为由其生成的文件来解决错误。