Using C Libraries for C++ Programs

后端 未结 7 2410
北海茫月
北海茫月 2020-11-27 14:50

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 kno

7条回答
  •  借酒劲吻你
    2020-11-27 15:01

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

提交回复
热议问题