Undefined reference to `std::basic string(char, std::char traits(char), std::allocator(char))::basic string()'

From Cppsyntax

Jump to: navigation, search

You can get the error

 undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'

if you have a problem with your makefile such that it uses cc instead of g++. I'm not sure why, and would appreciate some clarification. (Does cc try to process it as C instead of C++?)

Note that in Makefiles, you have to have a completely blank line separating rules, but if there is a blank line here, MediaWiki will split the file into two.

WRONG

Makefile:

 foo: foo.o
    cc testLatLngToPixel.o -o testLatLngToPixel      # cc doesn't work for some reason
 (blank line)       
 .cc.o:
    g++ -v -g -c $< 

foo.cc:

  #include <iostream>
  #include <string>
  using namespace std;
  int main ()
  {
    string bar;
  }

RIGHT

Makefile:

 foo: foo.o
   g++ testLatLngToPixel.o -o testLatLngToPixel         # g++
 (blank line)       
 .cc.o:
   g++ -v -g -c $< 

foo.cc:

  #include <iostream>
  #include <string>
  using namespace std;
  int main ()
  {
    string bar;
  }
Personal tools