关于 ubuntu 下使用 C++ boost::regex 库的问题

2016-05-09 08:12:31 +08:00
 niuoh

我在 ubuntu 下 使用命令 apt-get install libboost-dev 安装了 boost 库
使用 boost:cast_lexical 正常
但使用 boost:regex 库时 光引用#include <boost/regex.hpp> 这条语句正常
但使用 regex reg("([a-z]+)");这条语句时 编译报错

/tmp/ccpdAoXx.o :在函数‘ boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)’中: learn.cpp:(.text._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j]+0x2a):对‘ boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)’未定义的引用 collect2: error: ld returned 1 exit status

编译语句就是 g++ test.cpp -o test
在网上搜索了一下 说是 boost:regex 需要另外编译安装?
ubuntu 下能通过包的方式安装吗
我已经通过包的方式安装了 boost 库 再编译安装 boost 的 regex 库会不会出问题呢?
还是编译命令应该是怎样的?

2223 次点击
所在节点    C
14 条回复
niuoh
2016-05-09 08:15:53 +08:00
忘了说了 我已经用了 using namespace boost;
MCVector
2016-05-09 08:18:34 +08:00
编译命令最后加一个 -lost 试试
MCVector
2016-05-09 08:19:10 +08:00
* -lboost
niuoh
2016-05-09 08:19:32 +08:00
@MCVector <be>
root@system:/home/c++# g++ test.cpp -o test -lost
/usr/bin/ld: 找不到 -lost
collect2: error: ld returned 1 exit status
niuoh
2016-05-09 08:20:15 +08:00
@MCVector <br>
root@system:/home/c++# g++ test.cpp -o test -lboost
/usr/bin/ld: 找不到 -lboost
collect2: error: ld returned 1 exit status
yuyang1110
2016-05-09 08:50:32 +08:00
这是连接错呀,不是编译错。。。你找一下那个符号对应的连接库是啥,加到连接参数里。。。
rogerchen
2016-05-09 08:51:49 +08:00
sudo apt-get install libboost-all-dev

libboost-dev 只是推荐安装所有库,不会默认安装的
rogerchen
2016-05-09 08:53:27 +08:00
araraloren
2016-05-09 08:55:48 +08:00
你需要链接`boost`的`regex`库:`-lboost_regex`
niuoh
2016-05-09 08:59:15 +08:00
感谢 其实我在网上看到的 但我是 ubuntu16.04 因为依赖关系 运行不了这条命令 我去 docker 下试试把!
MCVector
2016-05-09 12:20:10 +08:00
试试 `libboost_regex-mt`

http://stackoverflow.com/questions/12446471/linker-error-in-boost-regex

当然一切的前提是你要装了 libboost 。
hitmanx
2016-05-09 15:23:42 +08:00
记得 regex 库不是 header-only 的,你需要 link 它的
hitmanx
2016-05-09 15:26:01 +08:00
另外 16.04 自带的 gcc 估计支持 c++11 的 regex 没啥问题了(老版本的记得是定义了,但是部分未实现),所以你直接用 std::regex 替换 boost::regex 应该也可以。
stormpeach
2016-06-16 14:53:09 +08:00
有人测过匹配速度是 std::regex>boost::regex>"regex"

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/277243

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX