hemihedral
hemihedral

Reputation: 1

Makefile: add library to another project

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?

Upvotes: 0

Views: 414

Answers (1)

fluffy
fluffy

Reputation: 5314

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"?

Upvotes: 1

Related Questions