Calling C code from C++ in a CMake project. Undefined symbol. Have extern C

前端 未结 1 1431
花落未央
花落未央 2021-01-28 14:58

I\'m trying to build a CMake project that calls C code from C++, and I\'m getting undefined symbols, even though I\'m (AFAIK) properly using \"extern C\".

CMakeLists.txt

相关标签:
1条回答
  • 2021-01-28 15:42

    extern "C" is C++ syntax. Your header lib.h therefore cannot be used from C. If you change it as follows it can be used from C++ and C as well.

    #ifndef LIB_H_HEADER
    #define LIB_H_HEADER
    
    #ifdef __cplusplus
    extern "C" 
    {
    #endif
    
    int printit();
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif /* LIB_H_HEADER */
    

    As you have both C and CXX sources your project call should enable C as well project(CTest LANGUAGES C CXX) in your CMakeLists.txt.

    0 讨论(0)
提交回复
热议问题