Using C Libraries for C++ Programs

老子叫甜甜 提交于 2019-12-17 04:54:32

问题


I am trying to control Dynamixel servos using a GUI made using Qt. Dynamixel provides a C set of C libraries to 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?


回答1:


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 my enclosing the prototype of by an extern "C"

#ifdef __cplusplus
extern "C"{
#endif 

void c_function_prototype();

#ifdef __cplusplus
}
#endif

The headers for the library may already do that, BTW.




回答2:


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.

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.

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:

g++ -std=c++11 my_code.cpp -lmy_library -L/custom_directory_path

Note that the -l and -L switches come after the 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.




回答3:


You can use C libraries from C++... however there are some caveats.

One big thing to watch out when using third-party C libraries with C++ is error handling.

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).

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.)




回答4:


Yes. To use C library function use extern "C" as below in your .cpp program , myprog.cpp

extern "C" {
    // C Function call
    cfunc();
}

int main()
{
    cfunc();
    return 0;
}

This cfunc should be defined in c library as below 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

$] 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



回答5:


Dont forget about extern "C" around the library headers. Read here. How does C's "extern" work?




回答6:


Yes - C++ can use C libraries.

This is an example that uses libc the main C library

#include <cstdio>

int main()
{
   printf("%s\n", "Hello world");
   return 0;
}



回答7:


There is a C++ driver for Dynamixel servos in the Rock Framework.



来源:https://stackoverflow.com/questions/12066279/using-c-libraries-for-c-programs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!