【问题标题】:undefined reference to `pthread_create' Error when making C++11 application with ASIO and std::thread使用 ASIO 和 std::thread 制作 C++11 应用程序时对“pthread_create”的未定义引用错误
【发布时间】:2016-01-21 19:52:43
【问题描述】:

我设置了 Eclipse(实际上是 Xilinx SDK,但基于 Eclipse)和 g++4.9.2,来编译一个使用独立 ASIO 的项目,我在 Properties -> C/C++ 中使用了 -std=c++11构建 -> 设置 -> 工具设置 -> 其他标志,以便它可以使用所有 C++11 功能进行编译。

我还在 C/C++ 通用符号中设置了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),根据 thread.hpp in ASIO:

#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 生成文件

当我搜索 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++ c++11 pthreads stdthread


    【解决方案1】:

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

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

    稍后

    我实际上使用 std::std 仅使用 -std=c++11 构建了一个没有 pthread 的程序

    我认为您对 GCC 的某些 Windows 端口的事实感到错误和困惑 默认链接libpthread。例如。如果您的示例程序在thread.cpp 我 可以使用 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
    

    你可以看到很多库选项被传递给链接器 在幕后,尤其是-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:如果我尝试在没有 -pthread 的情况下使用 std::thread,我会得到 undefined reference to `pthread_create'。您可能能够摆脱使用锁,因为 glibc 有一个无锁存根实现,当您链接 libpthread.so 时,它会被替换为真正的存根实现
    • 那为什么没有-lpthread就不能构建使用ASIO的应用程序,而没有ASIO的应用程序可以呢?相同的编译器、相同的 IDE、相同的链接器选项。
    【解决方案2】:

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

    会有用的

    【讨论】:

      【解决方案3】:

      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() 后,它会因 对 `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++ 4.5.3 -std=c++11 编译了您的程序。选项 -std=c++11 仅在 g++ 4.7 中引入。它被 g++ 4.5.3 拒绝。实时示例:goo.gl/7Gmmlx 正如所发布的 main 程序不会使用任何 C++ 编译器或任何选项进行编译。为了说明您的观点,您需要发布示例程序,这些程序将在您指定的操作系统上,使用您指定的编译器版本,以及您指定的逐字编译和链接命令行,按书面方式编译和链接(或失败)。
      • 谢谢,这很有帮助。通过注释掉与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 重新链接。
      • 谢谢。所以它可以编译但没有-pthread就不能运行。
      猜你喜欢
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多