0

I want to change an existing makefile to include another static library that I made myself. I followed some instructions to make the library; it currently holds all the .o files except for main.o. Let's call my library libABC.a. If it makes a difference, the package I'm modifying is written in C++ and the library I'm including is written in C.

So far I've added -lABC to my library list and placed the library in the same directory as the other libraries so that I don't have to add another -L command. I've moved all the header files to the /include directory of the package (not sure if I had to do this) so I can avoid adding another -I command. Compiling as it is gives me no errors, but if I try to add a #include command for one of the header files from the library and call a function, I get a undefined reference to function() error.

Any ideas on what I can do?

Darth Hunterix
  • 1,484
  • 5
  • 27
  • 31
  • 1
    Are headers written in C wrapped in `extern "C" { }`? http://stackoverflow.com/questions/1041866/in-c-source-what-is-the-effect-of-extern-c – Darth Hunterix Aug 10 '15 at 19:26

1 Answers1

1

Since the package is written in C++ and your library is written in C, you probably need to use extern "C" linkage, since C++ is probably expecting to name-mangle your symbols.

The easiest way is to wrap your C function definitions in the header in an extern "C" block, like:

#ifdef __cplusplus
extern "C" {
#endif

int myFunction();
int someOtherFunction(char *);

#ifdef __cplusplus
} // extern "C"
#endif

Make sure to only wrap your own function definitions within the block; any external libraries that you are #includeing from your header should be outside the block.

See also: In C++ source, what is the effect of extern "C"?

Community
  • 1
  • 1
fluffy
  • 5,212
  • 2
  • 37
  • 67