0

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.

mjr
  • 165
  • 1
  • 9
  • Please show the actual errors and your linker command line. Did you make sure to use `extern "C"` when including the header? – Carl Norum Jun 08 '13 at 22:01
  • Take a look at the top answer here https://stackoverflow.com/questions/24715864/problems-importing-libraries-to-my-c-project-how-to-fix-this – QueenBee Dec 18 '20 at 21:06

1 Answers1

2

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.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Also, if you can't or shouldn't modify the headers of the other project, you can just use extern "C" to include the header in C++ files. – John Chadwick Jun 08 '13 at 22:05
  • Thanks @Mats I also find out that it is called linkag between languages and here is a link [link] (http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr020.htm) – mjr Jun 08 '13 at 22:34