在Linux中安装和构建Boost库

Hon*_*oon 6 linux boost

我正在关注Boost入门文章.我已经用Bjam安装了它,我可以看到包含文件和库文件(.a,.so).

#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main()
{
    std::string line;
    boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
}
Run Code Online (Sandbox Code Playgroud)

如果我使用此命令构建上面的代码

g++ -I./boost/include -L./boost/lib -lboost_regex-gcc43-mt -static -o test_boost2 test_boost2.cc
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

/tmp/ccJFVVid.o: In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
    test_boost2.cc:(.text._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)]+0x22): undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)'
    collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

它在我的Windows机器下运行正常.

怎么了?

gcc版本4.3.2(Debian 4.3.2-1.1)

Linux xxxxxxxx 2.6.26-1-686#1 SMP Fri Mar 13 18:08:45 UTC 2009 i686 GNU/Linux

Lir*_*una 16

sudo apt-get install libboost-regex-dev
Run Code Online (Sandbox Code Playgroud)

不要在更智能的系统可以做得更好的源头安装.

要编译,只需使用

g++ -lboost_regex boost.cpp -o boost
Run Code Online (Sandbox Code Playgroud)

获取可用包名列表(更多部分的提升):

apt-cache search libboost | grep -- -dev
Run Code Online (Sandbox Code Playgroud)

免责声明:我假设您使用Debian,因为这是您的GCC版本报告的内容.


Hon*_*oon 8

我发现了问题!g ++命令问题!:(抱歉.如果我将-l选项移回到行尾,它不会抱怨更多:)