【发布时间】:2017-06-11 16:19:27
【问题描述】:
在编译一个简单的多线程程序时,我似乎无法链接 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
【问题讨论】:
-
您尝试过错误消息告诉您的操作吗?
-
请发布 1) minimal reproducible example。 2)您的确切编译器命令行。 3) 完整的错误信息(如果还有更多)。
-
现在按照编译器告诉你的做:
recompile with -fPIE and relink with -pie -
在 gcc 7.1 中还需要使用 -pthread 吗?
标签: c++ linux linker static-libraries