mjr
mjr

Reputation: 165

Use c library to c++ project in eclipse

I have a c library which by compiling it.It will produce a .so library which will be use in other c project with one of the header file(x.h) which is used to access the library function in .so file in your project. very simple in my project Include x.header and give -lx.so file and path to the library source directory(of .so file) to eclipse C linker and compile my project.

Question is that: How can I use this c library with my c++ project as in c project explained above in eclipse?

I did the same in my c++ code in eclipse and add .so file in my c++ linker library also Include library source path to it. thereafter I add header and tried to use the library function but eclipse give error "undefined reference to functions ..." and can not compile the code.

Thank you.

Upvotes: 0

Views: 466

Answers (1)

Mats Petersson
Mats Petersson

Reputation: 129364

To use any C compiled code in C++, you need to wrap it with extern "C" { ... } in the header [and if you plan on compiling it in C++ compiler, also the library content itself].

To make sure the code can be compiled with BOTH C and C++, you can use:

#idfef __cplusplus
extern "C" {
#endif

int func(double d); 

... 

#idfef __cplusplus
}  // end of extern "C"
#endif

That way, the extern "C" only happens when using a C++ compiler, and you don't get errors with the C compiler.

Upvotes: 2

Related Questions