Iostream linker error

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

The C code used has the following meanings:

#include <windows.h> #include <stdio.h> #include <string.h> #include "mysql.h" 

Now I am trying to use C ++ with iostream as follows:

 #include <windows.h> #include <stdio.h> #include <string> #include <iostream> #include "mysql.h" 

But I keep getting the following linker errors when compiling:

[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 to resolve this?

Edit: My compiler is Dev-C ++ 4.9.9.2

+13
c ++ c iostream linker
Nov 05 '08 at 0:58
source share
3 answers

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

In general, however, your problem is that the file is compiling correctly, but the wrong runtime library is getting a link.

Dev-C ++ uses GCC. GCC can correctly determine the language in the file based on the file extension, but will not link the desired runtime library unless you specifically ask for it (-lstdC ++ on the command line). Calling GCC as "g ++" (or, in your case, "mingwin32-g ++") will also get the right language and link the right library.

+26
Nov 05 '08 at 1:07
source share
β€” -

You need to associate yourself with the C ++ runtime. It depends on your platform and compiler, but adding -lC to your link can do this.

This way you can link using your C ++ compiler rather than ld.

In any case, you will probably have to reference using the C ++ compiler rather than ld if you want your C ++ code to work correctly - it is often required that exceptions and static initializers work correctly ...

+3
Nov 05 '08 at 1:06
source share

I got the exact same error when I tried to compile Cygwin (g ++).

just add -L/usr/local/bin -L/usr/lib to the compilation rules and it should work.

This may be specific to Cygwin, but it may help solve your problem.

+1
Dec 07 '15 at 9:58
source share



All Articles