Monday, April 1, 2019

c++ - iostream linker error



I have some old C code that I would like to combine with some C++ code.



The C code used to have has the following includes:




#include 
#include
#include
#include "mysql.h"


Now I'm trying to make it use C++ with iostream like this:



#include 

#include
#include
#include
#include "mysql.h"


But I keep getting the following linker errors when I compile:




[Linker error] undefined reference to `std::string::size() const'




[Linker error] undefined reference to `std::string::operator[](unsigned int) const'



[Linker error] undefined reference to `std::string::operator[](unsigned int) const'



[Linker error] undefined reference to `std::string::operator[](unsigned int) const'



[Linker error] undefined reference to `std::ios_base::Init::Init()'



[Linker error] undefined reference to `std::ios_base::Init::~Init()'




ld returned 1 exit status




How do I resolve this?



Edit: My compiler is Dev-C++ 4.9.9.2


Answer



The C string.h header and the C++ string header are not interchangeable.




Overall, though, your problem is that the file is getting properly compiled, but the wrong runtime library is getting linked in.



Dev-C++ uses GCC. GCC can correctly determine the language in a file based on file extension, but won't link the right runtime library in unless you specifically ask it to (-lstdc++ at the command line). Calling GCC as "g++" (or, in your case, "mingwin32-g++") will also get the right language and will link the needed library.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...