使用ASIO和std::thread制作C++11应用程序时出现“undefined reference to `pthread_create'”错误

10

我设置了Eclipse(实际上是基于Eclipse的Xilinx SDK)和g++4.9.2来编译一个使用独立ASIO的项目,并在Properties->C/C++ Build->Settings->Tool Settings->Other flags中使用了-std=c++11,以便可以使用所有的C++11特性进行编译。

我还在C/C++ General Symbols中设置了ASIO_HAS_STD_THREAD、ASIO_STANDALONE等等,我希望ASIO头文件将使用std::thread而不是pthread。然而,我仍然看到来自make的错误:

undefined reference to pthread_create, 
..asio-1.10.6\include\asio\detail\impl\posix_thread.ipp
and posix_tss_ptr.hpp
因为我正在使用C++11,并指定了 ASIO_HAS_STD_THREAD 但没有 ASIO_HAS_PTHREADS ,所以问题是 posix_thread.ipp 不应该被包括在内(通过posix_thread.hpp),根据ASIO中thread.hpp的描述:
#if !defined(ASIO_HAS_THREADS)
# include "asio/detail/null_thread.hpp"
#elif defined(ASIO_WINDOWS)
# if defined(UNDER_CE)
#  include "asio/detail/wince_thread.hpp"
# else
#  include "asio/detail/win_thread.hpp"
# endif
#elif defined(ASIO_HAS_PTHREADS)
# include "asio/detail/posix_thread.hpp"
#elif defined(ASIO_HAS_STD_THREAD)
# include "asio/detail/std_thread.hpp"
#else
# error Only Windows, POSIX and std::thread are supported!
#endif

嫌疑人1 - pthread

与大多数人的认为相反,C++11不需要 -pthread。我曾尝试在Eclipse中编译一个简单项目而没有使用 -pthread。但是如果我错了,请纠正我。当我将 -pthread 放入链接器选项时,它确实可以编译,但我觉得如果不必要,我不想使用pthread。

嫌疑人2 - ASIO makefile

当我搜索posix_tss_ptr.hpp时,我还找到了Makefile.am。我想知道这是否会影响错误?

那么问题的原因是什么?如果不是以上两个嫌疑人呢?如果我的推理正确,我希望解决方案仍然可以使用纯C++11方式,而不必使用pthread。

更新

我发现ASIO_HAS_PTHREADS并非由我定义,这就是为什么ASIO在某些地方使用POSIX线程,然后链接器需要选项 -pthread的原因。然后我通过使用 #error 指令跟踪到asio/detail/signal_blocker.hpp。它只被定义在ASIO config.hpp 中的两个位置。

#  if defined(ASIO_HAS_BOOST_CONFIG) && defined(BOOST_HAS_PTHREADS)
#   define ASIO_HAS_PTHREADS 1
#  elif defined(_POSIX_THREADS)
#   define ASIO_HAS_PTHREADS 1

ASIO仍然依赖于 POSIX THREADS 或 Windows 来实现 signal_blocker.hpp(如下所示)的功能。这就是为什么 ASIO 仍然需要 pthread 的原因。

ASIO仍然使用 POSIX THREADS 或 Windows 来实现 signal_blocker.hpp 功能,因此仍然需要 pthread。

#if !defined(ASIO_HAS_THREADS) || defined(ASIO_WINDOWS) \
  || defined(ASIO_WINDOWS_RUNTIME) \
  || defined(__CYGWIN__) || defined(__SYMBIAN32__)
typedef null_signal_blocker signal_blocker;
#elif defined(ASIO_HAS_PTHREADS)
typedef posix_signal_blocker signal_blocker;
#endif

而 _PTHREADS 是从 GNU 交叉编译器(arm-xilinx-linux-gnueabi)的包含文件中定义的,如 features.h、posix_opt.h 等等。我不会去追踪到底是哪个文件真正定义了该宏,但 ASIO 是使用 _POSIX_THREADS 的源代码,因此链接器选项 -pthread 应该存在。

同样,对于 g++ 4.9.2,非 ASIO C++11 线程不需要 -pthread,但独立的 ASIO 需要它。以下代码在 g++ 4.9.2 中可以正确构建(基于 Eclipse 的 Xilinx SDK):

#include <thread>
void test() {
    for(int i=0;i<100;i++);
}
int main()
{
    std::thread thread1(test);
    thread1.join();
    return 0;
}
3个回答

26

程序使用C++11编写与否与是否需要链接pthread库没有关系。如果它需要Posix线程,则需要链接该库。

C++11提供了std::thread类,每个符合标准的编译器标准库必须使用一些由目标系统托管的本机线程API实现该类的功能。GCC使用pthreads来实现它,因此,除非您使用-pthread链接它,否则您不能使用GCC构建创建std::thread对象的程序。这个事实与asio无关。

LATER

我实际上使用-std=c++11而不是pthread构建了一个不使用pthread的程序

我认为你错了,并被一些Windows端口默认链接libpthread所困惑。例如,如果你的示例程序在thread.cpp中,我可以在Windows上成功使用TDM-GCC 4.9.2构建它,所以:

>g++ -std=c++11 -o test_thread thread.cpp

但是如果您使用详细模式构建它:

>g++ -v -std=c++11 -o test_thread thread.cpp

你可以看到,许多库选项在幕后传递给链接器,特别是-lpthread

>g++ -v -std=c++11 -o test_thread thread.cpp 2>&1 | grep -Po 'pass-through=-lpthread' -
pass-through=-lpthread

在Linux上,如果你不主动要求的话,是不会链接libpthread的:
$ g++ -std=c++11 -o test_thread thread.cpp
/tmp/ccpyEles.o: In function `std::thread::thread<void (&)()>(void (&)())':
thread.cpp:(.text._ZNSt6threadC2IRFvvEJEEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEEEOT_DpOT0_]+0x7d): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status

我实际上使用std::std构建了一个程序,而没有使用pthread,只是使用了-std=c++11。 - Splash
@Splash: 如果我在没有使用-pthread的情况下尝试使用std::thread,会出现“undefined reference to `pthread_create'”的错误。由于glibc具有无锁存根实现,当您链接libpthread.so时,它将被替换为真正的实现,因此您可以考虑使用锁来解决问题。 - Zan Lynx
那么为什么使用ASIO的应用程序在没有-lpthread的情况下无法构建,而没有ASIO的应用程序可以呢?同样的编译器、同样的IDE、同样的链接器选项。 - Splash

7

在代码执行期间使用 g++ ssss(你的代码名称).cpp -std=c++11 -pthread。

这样就可以运行了。


2

1. std::thread 不需要 -pthread

以下是我从 ASIO 挖掘出来的,先让我展示一个简单的例子。在 CentOS 6.5 32 位操作系统中,使用 g++4.9.3 和 -std=c++11 编译此代码时,无需加 -lpthread。

std::string make_greeting_string()
{
    auto now = std::chrono::system_clock::now();
    const auto in_time_t = std::chrono::system_clock::to_time_t(now);

    std::stringstream ss;
    //ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
    ss << "Greetings"<<45;
    return ss.str();
}
int main() {
    std::thread thread1(make_greeting_string);
    //test2();
    thread1.join();
    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    return 0;
}

当我在主函数中添加tes2()后,出现了“未定义对`pthread_create’的引用”错误。

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   std::cout<<"Hello World! It's me, thread #%ld!"<<tid<<std::endl;
   pthread_exit(NULL);
}


void test2()
{
    pthread_t threads[NUM_THREADS];
    int rc;
    long t;
    for(t=0;t<NUM_THREADS;t++){
        std::cout<<"In main: creating thread %ld"<<t<<std::endl;
        rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
        if (rc){
            std::cout<<"ERROR; return code from pthread_create() is "<<rc<<std::endl;
            exit(-1);
        }
    }
}

2. ASIO对-pthread依赖的来源

首先,独立的ASIO代码中有一些使用posix线程的代码,但也可以使用std。当两者都被定义时,应该重新排列顺序,优先使用std。代码示例来自static_mutex.hpp第27行。

#elif defined(ASIO_HAS_PTHREADS)  
#include "asio/detail/posix_static_mutex.hpp"
#elif defined(ASIO_HAS_STD_MUTEX_AND_CONDVAR)
#include "asio/detail/std_static_mutex.hpp"

第二,还有两个文件需要posix:signal_blocker.hpp和tss_ptr.hpp。make将失败,因为只支持Windows和POSIX。
我不知道是否可能完全重写这两个文件以使用c++11,但是这两个文件是对-lpthread依赖的源,而不是std::thread。
更新 说明如果定义了ASIO_HAS_THREAD_KEYWORD_EXTENSION,则不需要pthread。在我在符号中定义了它之后,make错误减半,表明只有signal_blocker.hpp是该依赖的源。
影响signal_set_service.ipp。
case asio::io_service::fork_child:
    if (state->fork_prepared_)
    {
      asio::detail::signal_blocker blocker;
      close_descriptors();
      open_descriptors();
      int read_descriptor = state->read_descriptor_;
      state->fork_prepared_ = false;
      lock.unlock();
      reactor_.register_internal_descriptor(reactor::read_op,
          read_descriptor, reactor_data_, new pipe_read_op);
    }
    break;

并选择select_reactor.ipp文件

{
#if defined(ASIO_HAS_IOCP)
  asio::detail::signal_blocker sb;
  thread_ = new asio::detail::thread(
      bind_handler(&select_reactor::call_run_thread, this));
#endif // defined(ASIO_HAS_IOCP)
}

你说你使用g++ 4.5.3编译了你的程序,并加上了-std=c++11选项。然而,-std=c++11选项只在g++ 4.7及以上版本中引入,因此在g++ 4.5.3中会被拒绝。实时示例:https://goo.gl/7Gmmlx。如所发布的“main”程序将无法通过任何C++编译器或任何选项进行编译。为了证明你的观点,你需要发布能够按照你指定的操作系统、编译器版本和编译链接命令行编译和链接(或失败)的示例程序。 - Mike Kinghan
谢谢,这很有帮助。尝试注释掉所有与pthreads_create(3,13,14,15)相关的行。编译链接并运行。你会看到:terminate called after throwing an instance of 'std::system_error' what(): Enable multithreading to use std::thread: Operation not permitted Aborted (core dumped)。为了解决这个问题,请使用-pthread重新链接。 - Mike Kinghan
谢谢。所以它可以编译,但没有-pthread就无法运行。 - Splash

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接