c++ - 链接到 boost_thread 的问题

标签 c++ gcc boost linker

我正在尝试将 xubuntu 上的 boost 1.47 与 gcc4.6 和 glibc 2.13 链接起来。到目前为止,我无法编译以下简单程序 main.cpp:

#include <cstdlib>
#include <boost/ref.hpp>
#include <boost/thread.hpp>
int main() {
    size_t n_threads = boost::thread::hardware_concurrency();
    return 0;
}

当我编译时:

    g++ -lboost_thread -lboost_regex -o mc main.cpp -static -lpthread /usr/local/lib/libboost_regex.a /usr/local/lib/libboost_thread.a

我从 boost 中得到了一堆类似于以下的错误:

/usr/local/lib/libboost_thread.a(thread.o): In function `_ZN5boost9call_onceIPFvvEEEvRNS_9once_flagET_.constprop.100':
thread.cpp:(.text+0x47): undefined reference to `pthread_mutex_lock'
thread.cpp:(.text+0x73): undefined reference to `pthread_cond_wait'
thread.cpp:(.text+0xb9): undefined reference to `pthread_mutex_unlock'
thread.cpp:(.text+0xc8): undefined reference to `pthread_key_create'
thread.cpp:(.text+0xd2): undefined reference to `pthread_mutex_lock'
thread.cpp:(.text+0xf5): undefined reference to `pthread_cond_broadcast'
thread.cpp:(.text+0x10e): undefined reference to `pthread_mutex_unlock'

还有:

/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.6.1/libgcc_eh.a(unwind-dw2.o): In function `uw_init_context_1':
(.text+0x20bd): undefined reference to `pthread_once'
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.6.1/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__register_frame_info_bases':
(.text+0x16d4): undefined reference to `pthread_mutex_lock'
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.6.1/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__register_frame_info_table_bases':
(.text+0x179b): undefined reference to `pthread_mutex_lock'
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.6.1/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__deregister_frame_info_bases':
(.text+0x183e): undefined reference to `pthread_mutex_lock'
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.6.1/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__deregister_frame_info_bases':
(.text+0x18c6): undefined reference to `pthread_mutex_unlock'
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.6.1/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `_Unwind_Find_FDE':
(.text+0x1976): undefined reference to `pthread_mutex_lock'
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.6.1/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `_Unwind_Find_FDE':
(.text+0x19c7): undefined reference to `pthread_mutex_unlock'
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.6.1/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__register_frame_info_bases':
(.text+0x16f3): undefined reference to `pthread_mutex_unlock'
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.6.1/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__register_frame_info_table_bases':
(.text+0x17ba): undefined reference to `pthread_mutex_unlock'
collect2: ld returned 1 exit status

什么会导致这样的问题? 我在这里提供了足够的信息吗? 非常感谢任何解决此问题的建议!

最佳答案

尝试将“-lpthread”移动到链接器的最后一个参数。 See this link

关于c++ - 链接到 boost_thread 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6919534/

相关文章:

logging - 什么是 Boost 日志 "debug output window"?

c++ - Qt,创建非常自定义的表

c++ - 调整网格布局内的标签大小?

C++:跨类结构

c - Linux gcc (5.4.0) 中的 float 是否遵守 IEEE754 规则?

linux - 在 Fedora 25 上使用 LD_PRELOAD 导致 Segmentation Fault

linux - 编译 GCC 并安装到 DESTDIR

c++ - 在 C++ 中使用 lambda 的简单自定义迭代器

c++ - boost::hana 如何检查类型是否为 std::optional?

c++ - 我如何遍历元组