20

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 <windows.h>
#include <stdio.h>
#include <string.h>
#include "mysql.h"

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

#include <windows.h>
#include <stdio.h>
#include <string>
#include <iostream>
#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

Community
  • 1
  • 1
Steve
  • 1,955
  • 9
  • 28
  • 30

3 Answers3

36

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.

Max Lybbert
  • 19,717
  • 4
  • 46
  • 69
3

You need to link against your C++ runtime. It depends on your platform and compiler, but adding -lC to your linkline might do it.

So might linking using your C++ compiler rather than ld.

In any case, you probably have to link using the C++ compiler rather than ld if you want your C++ code to work correctly -- it's often required for exceptions and static initializers to work correctly...

Mike G.
  • 1,670
  • 11
  • 17
  • Won't `-lc` link with `libc` which is a C runtime library, not C++? For C++, one needs to link with C++ Standard Library, `libstdc++`, using `-lstdc++` parameter to `gcc` (or just using `g++` which does it automatically). Don't try to help if you don't know how, because you may equally well do a mischief. – SasQ Aug 04 '12 at 04:36
  • I specified -lC, which is the C++ runtime on some unixes, not -lc. My 2nd paragraph also stated that the OP could link using the C++ compiler, which would provide the correct libraries and settings for the platform. – Mike G. Aug 05 '12 at 13:11
  • I was not able to use -lC. Google interpreted my search without caps but the question I found about libc had a response that said to check for libc.a. That was there, no libC anything, and no libC even mentioned in the repo apart from libconfuse... but there was libc++.a and the packages libc++1-13, etc. So I tried `-lc++` and it worked. Where did you guys find `-lC`? Am I using the wrong library? Is this strictly a dev-cpp thing? – John P Oct 06 '22 at 04:03
2

I got the same exact error when i was trying to compile with Cygwin (g++).

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

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

Reeno
  • 5,720
  • 11
  • 37
  • 50
Amaresh Kumar
  • 586
  • 1
  • 8
  • 20
  • Had this issue and this fix worked in cygwin. Thanks so much! Note - I only had to add `-L/usr/lib` as `/usr/local/bin` was empty for me. Although I'd probably still recommend using both as it won't hurt – Chris Mar 22 '18 at 14:41