libpthread 链接器错误

Libpthread linker error

本文关键字:错误 链接 libpthread      更新时间:2023-10-16

在编译一个简单的多线程程序时,我似乎无法链接 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

我设法通过本地安装gcc 5.3.0并用它生成的文件替换libphread.so文件来解决错误。

相关文章: