1
votes

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.

1
Looks perhaps like a missing extern "C" {}.bmargulies
@bmargulies it is, the question states that on the second sentence ;)mah

1 Answers

4
votes

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>
}