c++crobotics

Using C Libraries for C++ Programs


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?


Solution

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