Boost C++ 示例代码 - 静态编译错误

Posted

技术标签:

【中文标题】Boost C++ 示例代码 - 静态编译错误【英文标题】:Boost C++ Sample Code - Static Compile Error 【发布时间】:2011-04-19 10:35:27 【问题描述】:

我是使用 Boost C++ 库的新手,我正在尝试编译此处给出的正则表达式示例代码 (http://www.boost.org/doc/libs/1_46_1/more/getting_started/unix- variables.html#link-your-program-to-a-boost-library)

这是我得到的输出:

     make
g++ -I /boost/include/ src/main.cpp -o main -L/boost/lib/ -lboost_regex -static
/boost/lib//libboost_regex.a(static_mutex.o): In function `boost::scoped_static_mutex_lock::scoped_static_mutex_lock(boost::static_mutex&, bool)':
static_mutex.cpp:(.text+0x1d): undefined reference to `pthread_mutex_lock'
/boost/lib//libboost_regex.a(static_mutex.o): In function `boost::scoped_static_mutex_lock::~scoped_static_mutex_lock()':
static_mutex.cpp:(.text+0x4e): undefined reference to `pthread_mutex_unlock'
/boost/lib//libboost_regex.a(static_mutex.o): In function `boost::scoped_static_mutex_lock::lock()':
static_mutex.cpp:(.text+0x76): undefined reference to `pthread_mutex_lock'
/boost/lib//libboost_regex.a(static_mutex.o): In function `boost::scoped_static_mutex_lock::unlock()':
static_mutex.cpp:(.text+0xa6): undefined reference to `pthread_mutex_unlock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libstdc++.a(locale.o): In function `__gnu_cxx::__scoped_lock::~__scoped_lock()':
(.text._ZN9__gnu_cxx13__scoped_lockD2Ev[_ZN9__gnu_cxx13__scoped_lockD5Ev]+0x18): undefined reference to `pthread_mutex_unlock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libstdc++.a(locale.o): In function `std::locale::_Impl::_M_install_cache(std::locale::facet const*, unsigned int)':
(.text._ZNSt6locale5_Impl16_M_install_cacheEPKNS_5facetEj+0x4c): undefined reference to `pthread_mutex_lock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libstdc++.a(locale.o): In function `std::locale::_Impl::_M_install_cache(std::locale::facet const*, unsigned int)':
(.text._ZNSt6locale5_Impl16_M_install_cacheEPKNS_5facetEj+0x80): undefined reference to `pthread_mutex_unlock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libstdc++.a(locale_init.o): In function `std::locale::locale()':
(.text._ZNSt6localeC2Ev+0x41): undefined reference to `pthread_mutex_lock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libstdc++.a(locale_init.o): In function `std::locale::locale()':
(.text._ZNSt6localeC2Ev+0x5d): undefined reference to `pthread_mutex_unlock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libstdc++.a(locale_init.o): In function `std::locale::global(std::locale const&)':
(.text._ZNSt6locale6globalERKS_+0x2d): undefined reference to `pthread_mutex_lock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libstdc++.a(locale_init.o): In function `std::locale::global(std::locale const&)':
(.text._ZNSt6locale6globalERKS_+0x94): undefined reference to `pthread_mutex_unlock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libstdc++.a(eh_alloc.o): In function `__cxa_allocate_exception':
(.text.__cxa_allocate_exception+0xbc): undefined reference to `pthread_mutex_lock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libstdc++.a(eh_alloc.o): In function `__cxa_allocate_exception':
(.text.__cxa_allocate_exception+0x11a): undefined reference to `pthread_mutex_unlock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libstdc++.a(eh_alloc.o): In function `__cxa_free_exception':
(.text.__cxa_free_exception+0x6d): undefined reference to `pthread_mutex_lock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libstdc++.a(eh_alloc.o): In function `__cxa_free_exception':
(.text.__cxa_free_exception+0x88): undefined reference to `pthread_mutex_unlock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libstdc++.a(eh_alloc.o): In function `__cxa_allocate_dependent_exception':
(.text.__cxa_allocate_dependent_exception+0x64): undefined reference to `pthread_mutex_lock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libstdc++.a(eh_alloc.o): In function `__cxa_allocate_dependent_exception':
(.text.__cxa_allocate_dependent_exception+0xc0): undefined reference to `pthread_mutex_unlock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libstdc++.a(eh_alloc.o): In function `__cxa_free_dependent_exception':
(.text.__cxa_free_dependent_exception+0x70): undefined reference to `pthread_mutex_lock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libstdc++.a(eh_alloc.o): In function `__cxa_free_dependent_exception':
(.text.__cxa_free_dependent_exception+0x8b): undefined reference to `pthread_mutex_unlock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__register_frame_info_bases':
(.text+0x166e): undefined reference to `pthread_mutex_lock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__register_frame_info_bases':
(.text+0x1685): undefined reference to `pthread_mutex_unlock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__register_frame_info_table_bases':
(.text+0x178a): undefined reference to `pthread_mutex_lock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__register_frame_info_table_bases':
(.text+0x17a1): undefined reference to `pthread_mutex_unlock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__deregister_frame_info_bases':
(.text+0x188a): undefined reference to `pthread_mutex_lock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `__deregister_frame_info_bases':
(.text+0x1904): undefined reference to `pthread_mutex_unlock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `_Unwind_Find_FDE':
(.text+0x19cb): undefined reference to `pthread_mutex_lock'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/libgcc_eh.a(unwind-dw2-fde-glibc.o): In function `_Unwind_Find_FDE':
(.text+0x1a1a): undefined reference to `pthread_mutex_unlock'
collect2: ld returned 1 exit status
make: *** [main] Error 1

不幸的是,我有限的知识意味着我不太明白这一切意味着什么。有人可以解释一下吗?非常感谢 :) -技术

【问题讨论】:

【参考方案1】:
g++ -I /boost/include/ src/main.cpp -o main -L/boost/lib/ -lboost_regex -lpthread -static
                                                                        ^^^^^^^^^

【讨论】:

哦,谢谢!对于使用 Boost 库的任何应用程序,我是否需要 libpthread?我觉得我必须多读一些文档...感谢您的帮助:) @Technosites:boost 的某些部分依赖于 pthread,所以这取决于你使用什么。

以上是关于Boost C++ 示例代码 - 静态编译错误的主要内容,如果未能解决你的问题,请参考以下文章

Boost Multiindex:示例在 cpp 文件中编译,但不在标头中

C++ 唯一指针;为啥这个示例代码会出现编译错误?错误代码太长了,我无法指定

如何为 python C++ 嵌入构建 boost 示例

编译 boost c++ 代码错误

使用转换器提升 Python 问题 - 静态链接

C++ 使用 Boost.asio 和 Beast 库在正文中发送数据