1

I am trying to compile and link my project using g++. The source files in my project are written in c++, but the library I am trying to link against is written in c, and it doesn't have extern before the functions in the header files to define them as written in c language.

Here's my command for compiling and linking:

g++ socketd.cpp main.cpp -I src/common -L src/common -lwrappers -lerror -lpthread

I am getting the following errors:

socketd.cpp:(.text+0xc6): undefined reference to `error_sys(char const*, ...)'

And error_sys is already defined in liberror library.

So my question is, why am I getting this error? Is it because I have to re-write the functions and define extern before them for the libraries written in c language?

Thanks a lot.

IoT
  • 607
  • 1
  • 11
  • 23
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – πάντα ῥεῖ Mar 22 '14 at 13:13
  • Looks perhaps like a missing extern "C" {}. – bmargulies Mar 22 '14 at 13:16
  • @bmargulies it is, the question states that on the second sentence ;) – mah Mar 22 '14 at 13:17

1 Answers1

4

A library written in C is unlikely to have the extern "C" guard around its functions, but this is not a problem for C++ code. Simply provide it yourself around the include:

extern "C" {
    #include <my_c_header.h>
}
mah
  • 39,056
  • 9
  • 76
  • 93