How can I use Boost::regex.hpp library in C++?
Asked Answered
B

4

6

I tried to use Boost library but I failed, see my code:

#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);
}

When I tried to compile it I get those messages:

/usr/include/boost/regex/v4/regex_match.hpp:50: undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator, std::allocator > >, std::allocator, std::allocator > > > >, boost::regex_traits >

::match()'

/usr/include/boost/regex/v4/basic_regex.hpp:425: undefined reference to `boost::basic_regex >

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

/usr/include/boost/regex/v4/perl_matcher.hpp:366: undefined reference to `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)'

etc...

Burkitt answered 11/6, 2010 at 8:40 Comment(0)
T
4

Those are linker errors. The Boost regex library is not a header-only library like shared_ptr (for example) - you need to link against the .a or .lib or whatever binary library.

Thirtytwo answered 11/6, 2010 at 8:43 Comment(0)
W
12

You need to link to libboost_regex. Add -lboost_regex to the compiler switch if you're using gcc.

Windproof answered 11/6, 2010 at 8:42 Comment(0)
T
4

Those are linker errors. The Boost regex library is not a header-only library like shared_ptr (for example) - you need to link against the .a or .lib or whatever binary library.

Thirtytwo answered 11/6, 2010 at 8:43 Comment(0)
D
2

You have to link against boost_regex.

Dolora answered 11/6, 2010 at 8:44 Comment(0)
L
0

had similar issue.

the solution was to link via cmake with the command target link library:

target_link_libraries(boostGraph Boost::regex Boost::date_time Boost::system Boost::filesystem Boost::thread Boost::graph Boost::program_options)

using the syntax -lboost_regex as offered here did not work (at least not with cmake)

the root issue could have been different versions of libraries, which cause the issue and even though the compiler and the linker do find the regex lib.

Lyrate answered 7/5, 2020 at 8:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.