链接到boost_thread时出现问题

Problem linking to boost_thread

本文关键字:问题 thread boost 链接      更新时间:2023-10-16

我正在尝试在xubuntu上与gcc4.6和glibc 2.13连接boost 1.47。到目前为止,我还不能编译以下简单的程序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"移到链接器的最后一个参数。查看此链接