如何在C++中使用Boost :: regex.hpp库?

mal*_*yeb 5 c++ regex boost boost-regex

我试图使用Boost库但是我失败了,看到我的代码:

#include "listy.h"
#include <boost/regex.hpp>
using namespace boost;

ListyCheck::ListyCheck() {

}

ListyCheck::~ListyCheck() {

}

bool ListyCheck::isValidItem(std::string &__item) {
    regex e("(\\d{4}[- ]){3}\\d{4}");

    return regex_match(__item, e);
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它时,我得到了这些消息:

/usr/include/boost/regex/v4/regex_match.hpp:50:未定义引用`boost :: re_detail :: perl_matcher <__ gnu_cxx :: __ normal_iterator,std :: allocator >>,std :: allocator,std :: allocator >>>>>,boost :: regex_traits>

::比赛()'

/usr/include/boost/regex/v4/basic_regex.hpp:425:未定义的引用`boost :: basic_regex>

:: do_assign(char const*,char const*,unsigned int)'

/usr/include/boost/regex/v4/perl_matcher.hpp:366:未定义引用`boost :: re_detail :: perl_matcher <__ gnu_cxx :: __ normal_iterator,std :: allocator >>,std :: allocator,std :: allocator >>>>>,boost :: regex_traits>

:: construct_init(boost :: basic_regex >> const&,boost :: regex_constants :: _ match_flags)'

等等...

ken*_*ytm 11

你需要链接到libboost_regex.-lboost_regex如果您正在使用gcc,请添加到编译器开关.


小智 4

这些是链接器错误。Boost 正则表达式库不是像共享指针(例如)那样的仅标头库 - 您需要链接到 .a 或 .lib 或任何二进制库。