linker error when using tr1::regex
Asked Answered
P

2

2

I've got a program that uses tr1::regex, and while it compiles, it gives me very verbose linker errors.

Here's my header file MapObject.hpp:

#include <iostream>
#include <string>
#include <tr1/regex>
#include "phBaseObject.hpp"
using std::string;

namespace phObject
{
    class MapObject: public phBaseObject
    {
        private:
            string color;  // must be a hex string represented as "#XXXXXX"
            static const std::tr1::regex colorRX;  // enforces the rule above
        public:
            void setColor(const string&);
        (...)
    };
}

Here's my implementation:

#include <iostream>
#include <string>
#include <tr1/regex>
#include "MapObject.hpp"
using namespace std;


namespace phObject
{
    const tr1::regex MapObject::colorRX("#[a-fA-F0-9]{6}");

    void MapObject::setColor(const string& c)
    {
        if(tr1::regex_match(c.begin(), c.end(), colorRX))
        {
            color = c;
        }
        else cerr << "Invalid color assignment (" << c << ")" << endl;
     }

     (...)
}

and now for the errors:

max@max-desktop:~/Desktop/Development/CppPartyHack/PartyHack/lib$ g++ -Wall -std=c++0x MapObject.cpp
/tmp/cce5gojG.o: In function std::tr1::basic_regex<char, std::tr1::regex_traits<char> >::basic_regex(char const*, unsigned int)': MapObject.cpp:(.text._ZNSt3tr111basic_regexIcNS_12regex_traitsIcEEEC1EPKcj[std::tr1::basic_regex<char, std::tr1::regex_traits<char> >::basic_regex(char const*, unsigned int)]+0x61): undefined reference tostd::tr1::basic_regex >::_M_compile()'
/tmp/cce5gojG.o: In function bool std::tr1::regex_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, char, std::tr1::regex_traits<char> >(__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::tr1::basic_regex<char, std::tr1::regex_traits<char> > const&, std::bitset<11u>)':
MapObject.cpp:(.text._ZNSt3tr111regex_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEEcNS_12regex_traitsIcEEEEbT_S8_RKNS_11basic_regexIT0_T1_EESt6bitsetILj11EE[bool std::tr1::regex_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, char, std::tr1::regex_traits<char> >(__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::tr1::basic_regex<char, std::tr1::regex_traits<char> > const&, std::bitset<11u>)]+0x53): undefined reference to
bool std::tr1::regex_match<__gnu_cxx::__normal_iterator, std::allocator > >, std::allocator, std::allocator > > > >, char, std::tr1::regex_traits >(__gnu_cxx::__normal_iterator, std::allocator > >, __gnu_cxx::__normal_iterator, std::allocator > >, std::tr1::match_results<__gnu_cxx::__normal_iterator, std::allocator > >, std::allocator, std::allocator > > > > >&, std::tr1::basic_regex > const&, std::bitset<11u>)'
collect2: ld returned 1 exit status

I can't really make heads or tails of this, except for the undefined reference to std::tr1::basic_regex near the beginning. Anyone know what's going on?

Pressurecook answered 18/5, 2010 at 20:6 Comment(3)
This comes up constantly. I really wish gcc would just stop distributing the regex header all together so people would get comprehensible compile time errors instead of incomprehensible link time errors.Dasyure
That, or at least put a descriptive #error in it.Josphinejoss
Possible duplicate of error while using regex_replace function from <tr1/regex>Imperfective
J
1

Regex support for C++0x is incomplete and wasn't there for TR1, see the implementation status page for C++0x/TR1.

Boost offers an alternative TR1 implementation as well as the original library it is based on.

Josphinejoss answered 18/5, 2010 at 20:26 Comment(0)
S
0

The answer is, even though the header is supplied, some of the methods are not supplied.

One could deduce this from Georg's answer, but after thinking up and coding a hundred lines or so based on the assumption that the nifty library was actually provided, one might be too tired for any further deductions.

Shirlshirlee answered 5/3, 2013 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.