Using C Libraries for C++ Programs

C++CRobotics

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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"

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

Solution 2 - C++

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.

Solution 3 - C++

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

Solution 4 - C++

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

Solution 5 - C++

Dont forget about extern "C" around the library headers. Read here. https://stackoverflow.com/questions/2238456/how-does-cs-extern-work

Solution 6 - C++

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;
}

Solution 7 - C++

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjohngreenView Question on Stackoverflow
Solution 1 - C++André OrianiView Answer on Stackoverflow
Solution 2 - C++JasonView Answer on Stackoverflow
Solution 3 - C++user966588View Answer on Stackoverflow
Solution 4 - C++Michael AndersonView Answer on Stackoverflow
Solution 5 - C++ssgregView Answer on Stackoverflow
Solution 6 - C++Adrian CornishView Answer on Stackoverflow
Solution 7 - C++JakobView Answer on Stackoverflow