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

undefined reference to `pthread_create' Error when making C++11 application with ASIO and std::thread

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

我还在C/C++ General Symbols中设置了ASIO_HAS_STD_THREAD, ASIO_STANDALONE等等,我希望ASIO header会使用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 的原因。

#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)中没有 -pthread 的情况下正确构建:

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

程序是用 C++11 编写的这一事实与是否或 不需要使用 pthread 库 linked。它需要 link 库,如果它需要 Posix 个线程。

C++11 提供了 std::thread class 和每个符合编译器的标准 库必须使用一些本机实现 class 的功能 目标系统托管的线程 API。 GCC 使用 pthreads 实现它, 所以你不能构建一个程序来创建 std::thread 个对象 GCC 除非你 link 它与 -pthread。这个事实与 asio.

无关

稍后

I actually built a program without pthread using std::std just using -std=c++11

我认为你误会了,因为 GCC 的一些 Windows 端口 默认为 link libpthread。例如。如果您的示例程序在 thread.cpp I 可以使用 TDM-GCC 4.9.2 在 Windows 中成功构建它,所以:

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

但是如果你在详细模式下构建它:

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

你可以看到很多库选项都传递给了 linker 在幕后,特别是 -lpthread:

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

而在 Linux 你不会 link 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

1。 std::thread 不需要 -pthread

这是我从ASIO上挖出来的,先举个简单的例子。在 CentOS 6.5 32 位和 Eclipse 中,使用 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;
}

一旦我在 main 中添加 tes2(),它就会失败并显示 undefined reference to `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。

更新

tss_ptr.hpp 声明如果定义了 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++ ssss(你的代号).cpp -std=c++11 -pthread

它会起作用