Newbie Question - Errors if i want to compile using g++

Hi all, I'm a newbie in c++ and in boost too, but i want to learn ;) I've installed boost via apt and also with "hand-compile" but it doesn't run anyway.. here my code: -------------------- #include <iostream> #include <boost/regex.hpp> using namespace boost; using namespace std; int main() { const boost::regex e("PING\\s:\\S+"); cmatch match; if(boost::regex_match("PING :jgWEjg",match,e)) { cout << "PONG :" << match[1]; } return 0; } -------------------- then i want to compile it with ""g++ main.cc -o test" and these errors appeared: -------------------- /tmp/ccb8hTb3.o(.gnu.linkonce.t._ZN5boost11basic_regexIcNS_12regex_traitsIcEESaIcEED1Ev+0xd): In function `boost::basic_regex<char, boost::regex_traits<char>, std::allocator<char> >::~basic_regex [in-charge]()': : undefined reference to `boost::reg_expression<char, boost::regex_traits<char>, std::allocator<char> >::~reg_expression [not-in-charge]()' /tmp/ccb8hTb3.o(.gnu.linkonce.t._ZN5boost11basic_regexIcNS_12regex_traitsIcEESaIcEEC1EPKcjRKS3_+0x22): In function `boost::basic_regex<char, boost::regex_traits<char>, std::allocator<char> >::basic_regex[in-charge](char const*, unsigned, std::allocator<char> const&)': : undefined reference to `boost::reg_expression<char, boost::regex_traits<char>, std::allocator<char>
::reg_expression[not-in-charge](char const*, unsigned, std::allocator<char> const&)' /tmp/ccb8hTb3.o(.gnu.linkonce.t. _ZN5boost9re_detail12perl_matcherIPKcSaINS_9sub_matchIS3_EEENS_12regex_traitsIcEESaIcEEC1ES3_S3_RNS_ 13match_resultsIS3_S6_EERKNS_14reg_expressionIcS8_S9_EENS_15regex_constants12_match_flagsE+0x53): In function `boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char>, std::allocator<char> ::perl_matcher[in-charge](char const*, char const*, boost::match_results<char const*, std::allocator<boost::sub_match<char const*> > >&, boost::reg_expression<char, boost::regex_traits<char>, std::allocator<char> > const&, boost::regex_constants::_match_flags)': : undefined reference to `boost::reg_expression<char, boost::regex_traits<char>, std::allocator<char> >::get_traits() const' /tmp/ccb8hTb3.o(.gnu.linkonce.t. _ZN5boost9re_detail12perl_matcherIPKcSaINS_9sub_matchIS3_EEENS_12regex_traitsIcEESaIcEEC1ES3_S3_RNS_ 13match_resultsIS3_S6_EERKNS_14reg_expressionIcS8_S9_EENS_15regex_constants12_match_flagsE+0x85): In function `boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char>, std::allocator<char> ::perl_matcher[in-charge](char const*, char const*, boost::match_results<char const*, std::allocator<boost::sub_match<char const*> > >&, boost::reg_expression<char, boost::regex_traits<char>, std::allocator<char> > const&, boost::regex_constants::_match_flags)': : undefined reference to `boost::reg_expression<char, boost::regex_traits<char>, std::allocator<char> >::empty() const' /tmp/ccb8hTb3.o(.gnu.linkonce.t. _ZN5boost9re_detail12perl_matcherIPKcSaINS_9sub_matchIS3_EEENS_12regex_traitsIcEESaIcEE24estimate_ma x_state_countEPSt26random_access_iterator_tag+0x2b): In function `boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char>, std::allocator<char> ::estimate_max_state_count(std::random_access_iterator_tag*)': : undefined reference to `boost::reg_expression<char, boost::regex_traits<char>, std::allocator<char> >::size() const' /tmp/ccb8hTb3.o(.gnu.linkonce.t. _ZN5boost9re_detail12perl_matcherIPKcSaINS_9sub_matchIS3_EEENS_12regex_traitsIcEESaIcEE9match_impEv+ 0x8c): In function `boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char>, std::allocator<char> >::match_imp()': : undefined reference to `boost::reg_expression<char, boost::regex_traits<char>, std::allocator<char> >::mark_count() const' /tmp/ccb8hTb3.o(.gnu.linkonce.t. _ZN5boost9re_detail12perl_matcherIPKcSaINS_9sub_matchIS3_EEENS_12regex_traitsIcEESaIcEE9match_impEv+ 0x12c): In function `boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char>, std::allocator<char> >::match_imp()': : undefined reference to `boost::re_detail::verify_options(unsigned, boost::regex_constants::_match_flags)' /tmp/ccb8hTb3.o(.gnu.linkonce.t._ZN5boost9re_detail15save_state_initC1EPPNS0_11saved_stateES4_+0x13): In function `boost::re_detail::save_state_init::save_state_init[in-charge](boost::re_detail::saved_state**, boost::re_detail::saved_state**)': : undefined reference to `boost::re_detail::get_mem_block()' /tmp/ccb8hTb3.o(.gnu.linkonce.t._ZN5boost9re_detail15save_state_initD1Ev+0x11): In function `boost::re_detail::save_state_init::~save_state_init [in-charge]()': : undefined reference to `boost::re_detail::put_mem_block(void*)' /tmp/ccb8hTb3.o(.gnu.linkonce.t. _ZN5boost9re_detail8access_tIcNS_12regex_traitsIcEESaIcEE5firstERKNS_14reg_expressionIcS3_S4_EE+0xd) : In function `boost::re_detail::access_t<char, boost::regex_traits<char>, std::allocator<char> ::first(boost::reg_expression<char, boost::regex_traits<char>, std::allocator<char> > const&)': : undefined reference to `boost::reg_expression<char, boost::regex_traits<char>, std::allocator<char> ::first(boost::reg_expression<char, boost::regex_traits<char>, std::allocator<char> > const&)' /tmp/ccb8hTb3.o(.gnu.linkonce.t. _ZN5boost9re_detail12perl_matcherIPKcSaINS_9sub_matchIS3_EEENS_12regex_traitsIcEESaIcEE18unwind_extr a_blockEb+0x48): In function `boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char>, std::allocator<char> ::unwind_extra_block(bool)': : undefined reference to `boost::re_detail::put_mem_block(void*)' /tmp/ccb8hTb3.o(.gnu.linkonce.t._ZN5boost9re_detail11raise_errorINS_12regex_traitsIcEEEEvRKT_j+0x15): In function `void boost::re_detail::raise_error<boost::regex_traits<char> (boost::regex_traits<char> const&, unsigned)': : undefined reference to `boost::re_detail::c_traits_base::error_string(unsigned)' /tmp/ccb8hTb3.o(.gnu.linkonce.t._ZN5boost9re_detail11raise_errorINS_12regex_traitsIcEEEEvRKT_j+0x23): In function `void boost::re_detail::raise_error<boost::regex_traits<char> (boost::regex_traits<char> const&, unsigned)': : undefined reference to `boost::re_detail::raise_regex_exception(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' /tmp/ccb8hTb3.o(.gnu.linkonce.t._ZN5boost14c_regex_traitsIcE9translateEcb+0x1f): In function `boost::c_regex_traits<char>::translate(char, bool)': : undefined reference to `boost::re_detail::c_traits_base::lower_case_map' /tmp/ccb8hTb3.o(.gnu.linkonce.t. _ZN5boost9re_detail16re_is_set_memberIPKccNS_12regex_traitsIcEESaIcEEET_S7_S7_PKNS0_11re_set_longERK NS_14reg_expressionIT0_T1_T2_EE+0x3d): In function `char const* boost::re_detail::re_is_set_member<char const*, char, boost::regex_traits<char>, std::allocator<char> >(char const*, char const*, boost::re_detail::re_set_long const*, boost::reg_expression<char, boost::regex_traits<char>, std::allocator<char> > const&)': : undefined reference to `boost::reg_expression<char, boost::regex_traits<char>, std::allocator<char> >::get_traits() const' /tmp/ccb8hTb3.o(.gnu.linkonce.t. _ZN5boost9re_detail16re_is_set_memberIPKccNS_12regex_traitsIcEESaIcEEET_S7_S7_PKNS0_11re_set_longERK NS_14reg_expressionIT0_T1_T2_EE+0x244): In function `char const* boost::re_detail::re_is_set_member<char const*, char, boost::regex_traits<char>, std::allocator<char> >(char const*, char const*, boost::re_detail::re_set_long const*, boost::reg_expression<char, boost::regex_traits<char>, std::allocator<char> > const&)': : undefined reference to `boost::c_regex_traits<char>::transform(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' /tmp/ccb8hTb3.o(.gnu.linkonce.t. _ZN5boost9re_detail16re_is_set_memberIPKccNS_12regex_traitsIcEESaIcEEET_S7_S7_PKNS0_11re_set_longERK NS_14reg_expressionIT0_T1_T2_EE+0x32e): In function `char const* boost::re_detail::re_is_set_member<char const*, char, boost::regex_traits<char>, std::allocator<char> >(char const*, char const*, boost::re_detail::re_set_long const*, boost::reg_expression<char, boost::regex_traits<char>, std::allocator<char> > const&)': : undefined reference to `boost::c_regex_traits<char>::transform_primary(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' /tmp/ccb8hTb3.o(.gnu.linkonce.t._ZN5boost9re_detail8access_tIcNS_12regex_traitsIcEESaIcEE9can_startEcPKhh+0x30): In function `boost::re_detail::access_t<char, boost::regex_traits<char>, std::allocator<char> >::can_start(char, unsigned char const*, unsigned char)': : undefined reference to `boost::reg_expression<char, boost::regex_traits<char>, std::allocator<char> >::can_start(char, unsigned char const*, unsigned char, boost::re_detail::_narrow_type const&)' /tmp/ccb8hTb3.o(.gnu.linkonce.t._ZN5boost14c_regex_traitsIcE8is_classEcj+0x14): In function `boost::c_regex_traits<char>::is_class(char, unsigned)': : undefined reference to `boost::re_detail::c_traits_base::class_map' /tmp/ccb8hTb3.o(.gnu.linkonce.t. _ZN5boost9re_detail12perl_matcherIPKcSaINS_9sub_matchIS3_EEENS_12regex_traitsIcEESaIcEE12extend_stac kEv+0x16): In function `boost::re_detail::perl_matcher<char const*, std::allocator<boost::sub_match<char const*> >, boost::regex_traits<char>, std::allocator<char> >::extend_stack()': : undefined reference to `boost::re_detail::get_mem_block()'
collect2: ld returned 1 exit status --------------------
so .. what i make wrong? *g* i know, bad question :) thx mix

mix@openflex.net wrote:
Hi all,
I'm a newbie in c++ and in boost too, but i want to learn ;) I've installed boost via apt and also with "hand-compile" but it doesn't run anyway..
here my code:
-------------------- #include <iostream> #include <boost/regex.hpp>
using namespace boost; using namespace std;
int main() { const boost::regex e("PING\\s:\\S+"); cmatch match;
if(boost::regex_match("PING :jgWEjg",match,e)) { cout << "PONG :" << match[1]; }
return 0; } --------------------
then i want to compile it with ""g++ main.cc -o test"
This is your problem. The boost regex library is not a header only library like some of the others, so you must link against it! This is what causes "undefined reference to ____" errors - these are linker errors generated because it dosn't know where to find the implementation of this function. The exact line used will depend on your system. On my windows box using MinGW I'd link against it like so: g++ main.cc -o test -lboost_regex-mgw Note that the -mgw postfix refers to MinGW, so obviously your milage will vary.
participants (2)
-
Michael B. Edwin Rickert
-
mix@openflex.net