将 C 库用于 C++ 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12066279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using C Libraries for C++ Programs
提问by johngreen
I am trying to control Dynamixel servos using a GUI made using Qt. Dynamixel provides a C set of C librariesto control the motors, while the only way of making GUI's I know is Qt, which is essentially C++. Will it be possible to use Dynamixel C libraries from Qt C++ code in any way?
我正在尝试使用使用 Qt 制作的 GUI 来控制 Dynamixel 伺服系统。Dynamixel 提供了一组C 库来控制电机,而我知道制作 GUI 的唯一方法是 Qt,它本质上是 C++。是否可以以任何方式使用 Qt C++ 代码中的 Dynamixel C 库?
回答by André Oriani
Yes, C++ can compile C with a C++ compiler and you can link C++ against C. Just be sure that any C function you call uses C linkage. This is made by enclosing the prototype of the C function by an extern "C"
是的,C++ 可以使用 C++ 编译器编译 C,并且您可以将 C++ 与 C 链接。只要确保您调用的任何 C 函数都使用 C 链接。这是通过将 C 函数的原型包含在一个extern "C"
#ifdef __cplusplus
extern "C"{
#endif
void c_function_prototype();
#ifdef __cplusplus
}
#endif
The headers for the library you are trying to use may already do that.
您尝试使用的库的标头可能已经这样做了。
回答by Jason
Sure ... C code is called from C++ all the time. For instance, most OS libraries are written in C rather than C++. So whenever you're making syscalls from your C++ code to perform tasks that are handed over to the OS kernel, those are going through C-code calls.
当然...... C 代码总是从 C++ 调用。例如,大多数操作系统库是用 C 编写的,而不是 C++。因此,每当您从 C++ 代码进行系统调用以执行移交给操作系统内核的任务时,这些都是通过 C 代码调用进行的。
Just be sure to include the proper headers and link against the C-libraries in question at compile time. Also remember to use extern "C"
to specify C-linkage for the C-library functions if the header files have not already declared them as such. Keep in mind that some libraries may not have declared their functions specifically using extern "C"
, but may have used a pre-processor token to-do so. So you'll want to check for that as well before you assume the library writers did not already define their library as having C-linkage.
只要确保在编译时包含正确的头文件并链接到有问题的 C 库。extern "C"
如果头文件还没有这样声明它们,还记得使用为 C 库函数指定 C 链接。请记住,某些库可能没有专门使用 声明它们的函数extern "C"
,但可能使用了预处理器令牌来这样做。因此,在您假设库编写者尚未将其库定义为具有 C 链接之前,您还需要检查一下。
linking custom libraries using gcc
can be done with the -l
switch. If you need to specify a custom directory for where the libraries are located, that can be done with the -L
switch. So for instance:
gcc
可以使用-l
switch来链接自定义库。如果您需要为库所在的位置指定自定义目录,则可以使用-L
switch来完成。所以例如:
g++ -std=c++11 my_code.cpp -lmy_library -L/custom_directory_path
Note that the -l
and -L
switches come afterthe code or object files you're compiling, and if you're library is something like libjpg
, or librobotics
, etc., drop the lib
part of the name when you append it to the -l
switch.
请注意,-l
和-L
开关来后,你编译代码或目标文件,如果你的库是一样的东西libjpg
,或者librobotics
,等放下lib
,当你把它添加到名称的一部分-l
开关。
回答by Jason
Yes. To use C library function use extern "C" as below in your .cpp program , myprog.cpp
是的。要使用 C 库函数,请在 .cpp 程序 myprog.cpp 中使用如下所示的 extern "C"
extern "C" {
// C Function call
cfunc();
}
int main()
{
cfunc();
return 0;
}
This cfunc should be defined in c library as below prog.c
这个 cfunc 应该在 c 库中定义如下 prog.c
#include <stdio.h>
void cfunc()
{
printf("This is from c library");
}
Then you need to create .o
object file and .so
shared object files for your C library as below
然后你需要为你的 C 库创建.o
目标文件和.so
共享目标文件,如下所示
$] gcc -c prog.c -o prog
$] gcc -shared -o libprog.so prog.o
$] export LD_LIBRARY_PATH=/path/to/clibrary:$LD_LIBRARY_PATH
$] g++ -L/path/to/clibrary myprog.cpp -o myprog.o -lprog
回答by Michael Anderson
You can use C libraries from C++... however there are some caveats.
您可以使用 C++ 中的 C 库...但是有一些注意事项。
One big thing to watch out when using third-party C libraries with C++ is error handling.
将第三方 C 库与 C++ 一起使用时需要注意的一件大事是错误处理。
Some C libraries use facilities like setjmp
/longjmp
for error handling. (lua is a notable example). This means that on error stack unwinding will not occur in the normal manner, and you may leak resources. Things like the usual C++ RAII style guards for resource protection fail dismally. (These calls are worse than goto
for C++ code).
一些 C 库使用setjmp
/ 之类的工具longjmp
进行错误处理。(lua 是一个显着的例子)。这意味着错误堆栈展开不会以正常方式发生,并且您可能会泄漏资源。像通常用于资源保护的 C++ RAII 风格的守卫之类的东西失败了。(这些调用比goto
C++ 代码更糟糕)。
Also exceptions can be a concern. If a C++ exception propagates to a C/C++ boundary then the application may terminate rather than propagating the exception. (Depending on how the C library was compiled and your OS etc.) (You might get this situation if you pass a C++ function into a C library as a callback.)
异常也可能是一个问题。如果 C++ 异常传播到 C/C++ 边界,则应用程序可能会终止而不是传播异常。(取决于 C 库的编译方式和您的操作系统等)(如果您将 C++ 函数作为回调传递给 C 库,您可能会遇到这种情况。)
回答by ssgreg
Dont forget about extern "C"
around the library headers.
Read here. How does C's "extern" work?
不要忘记extern "C"
围绕库标题。在这里阅读。C的“外部”如何工作?
回答by Adrian Cornish
Yes - C++ can use C libraries.
是的 - C++ 可以使用 C 库。
This is an example that uses libc the main C library
这是一个使用 libc 主 C 库的示例
#include <cstdio>
int main()
{
printf("%s\n", "Hello world");
return 0;
}
回答by Jakob
There is a C++ driverfor Dynamixel servos in the Rock Framework.
Rock Framework 中有一个用于 Dynamixel 舵机的C++ 驱动程序。