2

I am working on a project with someone and we have the exact same code however his code compiles while mine continues to throw an error. Here is my code:

bool r_parser::parseMake(string name, string command, string type, string val)
{
    regex regName("^\\w+$");
    if(std::regex_match(name, regName) == true)
    {
        cout << "We have a match!" << endl;
    }
    return false;
}

And here are the errors I am getting:

'undefined reference to std::basic_regex<char, std::regex_traits<char> >::_M_compile()'

'undefined reference to bool std::regex_match<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<std::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, char, std::regex_traits<char> >(__gnu_cxx::__normal_iterator<char const*, std::string>, __gnu_cxx::__normal_iterator<char const*, std::string>, std::match_results<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<std::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > > >&, std::basic_regex<char, std::regex_traits<char> > const&, std::bitset<11u>)'

Since my partner's code compiled and we couldn't find anything different about my code I was wondering if anyone could shed some light on this situation.

Thanks for your time.

EDIT: I forgot to mention I have:

#include <regex>
#include <string>
#include <iostream>
using namespace std;

in my header file.

3
  • 1
    What compiler and version are you using? Not all compiler versions support std::regex. Commented Nov 14, 2014 at 2:18
  • for starters, your snippet is missing #inlcude <regex>. It's also missing #inlcude <string>, #inlcude <iostream>, and using namespace std, all of which are needed for that function to compile. Commented Nov 14, 2014 at 2:29
  • Sorry I forgot to mention that I had those included in my header file. I added that to the main post. I am running gcc 4.8.1 Commented Nov 14, 2014 at 5:19

3 Answers 3

3

I did some more research, Michael, and it would appear that #include <regex> is only a placeholder until GCC version 4.9.0.

If you're on a Windows system (which I seem to remember you are), you're using GCC via MinGW, so you'll need to update to the latest version manually. I'm on 4.9.1, and that's why it works for me. You can download that from Sourceforge.

If you're on Linux, you should be able to update gcc and g++ via the repositories.

If you don't want to update, you can also use the Boost libraries. However, I recommend updating your compiler, mainly because it is just a generally good idea to be using the latest version of whatever compiler you're implementing, unless you have an explicit reason not to.

Sorry I couldn't answer that earlier, but I hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

1

In Windows following code works to remove Regex Complilation Errors.

#include <regex>
using namespace std;

1 Comment

this is the best answer for Ubuntu users as well. Some software relies on <tr1/regex> when the best solution is to use <regex> because it is now a part of the standard. This problem is misleading because /tr1 and /tr2 are both included in the upgrade utilities for gcc.
0

Try using g++ as compiler. As it would not require to link libstdc++ explicitly. Whereas gcc, requires one to explicitly link libstdc++.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.