简体   繁体   English

在eclipse中使用c库到c ++项目

[英]Use c library to c++ project in eclipse

I have ac 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(xh) which is used to access the library function in .so file in your project. 我有一个通过编译它的ac库。它将生成一个.so库,它将在其他c项目中使用,其中一个头文件(xh)用于访问项目中.so文件中的库函数。 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. 在我的项目中非常简单包括x.header并给出-lx.so文件和库源目录(.so文件)的路径来eclipse C链接器并编译我的项目。

Question is that: How can I use this c library with my c++ project as in c project explained above in eclipse? 问题是:我如何将这个c库与我的c ++项目一起使用,就像上面在eclipse中解释的c项目一样?

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. 我在eclipse中的c ++代码中做了同样的事情,并在我的c ++链接器库中添加.so文件也包含库源路径。 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. 此后我添加标头并尝试使用库函数,但eclipse给出错误“未定义的函数引用...”,并且无法编译代码。

Thank you. 谢谢。

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]. 要在C ++中使用任何C编译代码,您需要在头文件中使用extern "C" { ... }包装它[如果您计划在C ++编译器中编译它,那么库内容本身也是如此]。

To make sure the code can be compiled with BOTH C and C++, you can use: 要确保可以使用BOTH C和C ++编译代码,您可以使用:

#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. 这样, extern "C"只在使用C ++编译器时发生,并且您不会在C编译器中出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM