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?
7 Answers
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.

- 3,553
- 22
- 29
-
1By C function I meant one compiled with a C compiler, so the its signature is not mangled as it would happen for C++ compiled functions. Then in that case, as you said, we need to use `extern "C"` to preventing the C++ from mangling the function signature when calling it, so the symbol can be properly found in the C library. – André Oriani Aug 22 '12 at 13:41
-
@LokiAstari Not a problem, you contributed to clarify things on my answer. – André Oriani Aug 22 '12 at 13:57
-
2You can compile most C *declarations* with a C++ compiler by specifying C linkage as you show, but the same is not true of general C code, unless it has been carefully designed for that or you get extremely lucky. If you try, you're likely to get compilation errors, but if you're not that lucky then you might get a successful compilation of a broken program. – John Bollinger Jul 30 '18 at 16:22
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.
-
-
I didn't assume that ... I said "if the header files have not already declared them as such" – Jason Aug 22 '12 at 04:45
-
-
4All major libraries have been properly wrapped AFAIK. Custom libraries from third-party vendors, such as a robotics manufacturer may not have though. It just depends. I've seen some older commercial code that wasn't wrapped that way. – Jason Aug 22 '12 at 04:49
-
1Thanks alot for the hint that the library switches/flags come after "my" objects. – TKrewSE Jan 02 '20 at 23:12
-
I have a C lib with a header file with this: ```#ifdef __cplusplus #define ANSI_C #define EXTERN extern "C" #else #define EXTERN extern #endif``` Where each function in the header file starts its definition with "EXTERN". My question is, if I compile using this header file in VS 2019, will __cplusplus automatically be defined, or do I do it myself? If I do it myself, do I do it in the header file, in my own source code, or in the project properties somewhere? Thanks. – inhahe Jun 11 '20 at 05:03
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

- 13,345
- 8
- 56
- 105
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.)

- 70,661
- 7
- 134
- 187
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;
}

- 23,227
- 13
- 61
- 77
-
4Except of course that you're using the C++ version of `cstdio`. To use the C one you need to `#include
`. – Michael Anderson Aug 22 '12 at 04:32 -
The header file has nothing to do with the library linked - the example was to prove C++ code could be compiled using a C library function – Adrian Cornish Aug 22 '12 at 04:35
-
Yes, header files are not the library. The header will just have the prototypes for the function so the linker will be able to find then in the C runtime library (libc,BTW). – André Oriani Aug 22 '12 at 04:35
-
1If you want to be pedantic - so will I ;) . If you're using `printf` instead of `std::printf` when using `
` then you get undefined begaviour as (17.6.1.2) "It is unspecified whether these names are first declared within the global namespace scope and are then injected into namespace std by explicit using-declarations". If you're using `std::printf` then you're only indirectly using the C std library and may be passing through `libstdc++` instead. – Michael Anderson Aug 22 '12 at 04:51