我已经阅读并搜索了很多内容(例如1 2 3,有关CMake的多个文档,类似项目等),以找到解决方案,但我无法解决自己的问题。我对Cmake和Linux相对较新(Ubuntu 14.04) 。

我想使用libsbp(https://github.com/swift-nav/libsbp)用C++编写一个程序来与GPS模块通信。我克隆了存储库并安装了C库。因此,现在 / usr / local / lib 中有两个文件: libsbp.so libsbp-static.a , header 位于 / usr / local / include / libsbp

在我自己的项目中,我包括带有#include "libsbp/sbp.h"的 header ,该 header 也起作用。

现在的问题是:如果我想使用libsbp中的方法,例如sbp_state_init(&s);我得到未定义的对“sbp_state_init(sbp_state_t *)”的引用

我的Cmake与我自己的项目有关的部分:

link_directories(/usr/local/lib)

add_executable(main ${QT_SOURCES} ${QT_HEADER_HPP})
target_link_libraries(main ${QT_LIBRARIES} ${catkin_LIBRARIES} sbp)

如前所述,我尝试了一些方法:
  • find_library(SBP_LIB sbp /usr/local/lib)->相同错误
  • 在target_link_libraries中使用 libsbp 或搜索
  • 同样适用
  • link_directory(/usr/local/lib)
  • 尝试不同的路径,甚至将 libsbp.so 移到项目目录中,并使用${CMAKE_CURRENT_SOURCE_DIR}
  • “查找”它

    也许你可以帮我!

    编辑:

    这是 libsbp / c / src 目录中的CMakeList.txt
    if (NOT DEFINED BUILD_SHARED_LIBS)
      set(BUILD_SHARED_LIBS ON)
    endif (NOT DEFINED BUILD_SHARED_LIBS)
    
    file(GLOB libsbp_HEADERS "${PROJECT_SOURCE_DIR}/include/libsbp/*.h")
    
    include_directories("${PROJECT_SOURCE_DIR}/CBLAS/include")
    include_directories("${PROJECT_SOURCE_DIR}/clapack-3.2.1-CMAKE/INCLUDE")
    include_directories("${PROJECT_SOURCE_DIR}/lapacke/include")
    include_directories("${PROJECT_SOURCE_DIR}/include/libsbp")
    
    set(libsbp_SRCS
      edc.c
      sbp.c
    )
    
    add_library(sbp-static STATIC ${libsbp_SRCS})
    install(TARGETS sbp-static DESTINATION lib${LIB_SUFFIX})
    
    if(BUILD_SHARED_LIBS)
      add_library(sbp SHARED ${libsbp_SRCS})
      install(TARGETS sbp DESTINATION lib${LIB_SUFFIX})
    else(BUILD_SHARED_LIBS)
      message(STATUS "Not building shared libraries")
    endif(BUILD_SHARED_LIBS)
    
    install(FILES ${libsbp_HEADERS} DESTINATION include/libsbp)
    

    这是/ libsbp / c /中的CMakeList.txt
    cmake_minimum_required(VERSION 2.8.9)
    project(libsbp)
    
    # Setup flags for Code Coverage build mode
    set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG} --coverage" CACHE STRING
        "Flags used by the C++ compiler for building with code coverage."
        FORCE )
    set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_DEBUG} --coverage" CACHE STRING
        "Flags used by the C compiler for building with code coverage."
        FORCE )
    SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE
        "${CMAKE_EXE_LINKER_FLAGS_DEBUG} --coverage" CACHE STRING
        "Flags used for linking binaries with code coverage."
        FORCE )
    set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
        "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} --coverage" CACHE STRING
        "Flags used by the shared libraries linker during builds with code coverage."
        FORCE )
    mark_as_advanced(
        CMAKE_CXX_FLAGS_COVERAGE
        CMAKE_C_FLAGS_COVERAGE
        CMAKE_EXE_LINKER_FLAGS_COVERAGE
        CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
    # Update the documentation string of CMAKE_BUILD_TYPE for GUIs
    set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
      "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage."
       FORCE )
    
    # Set project version using Git tag and hash.
    execute_process(
      COMMAND git describe --dirty --tags --always
      WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
      RESULT_VARIABLE GIT_VERSION_FOUND
      ERROR_QUIET
      OUTPUT_VARIABLE GIT_VERSION
      OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    if (GIT_VERSION_FOUND)
      set(VERSION "unknown")
    else (GIT_VERSION_FOUND)
      set(VERSION ${GIT_VERSION})
    endif (GIT_VERSION_FOUND)
    
    # Set project version explicitly for release tarballs.
    #set(VERSION foo)
    
    message(STATUS "libsbp version: ${VERSION}")
    
    cmake_minimum_required(VERSION 2.8)
    set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
    
    # Some compiler options used globally
    set(CMAKE_C_FLAGS "-Wall -Wextra -Wno-strict-prototypes -Wno-unknown-warning-option -Werror -std=gnu99 ${CMAKE_C_FLAGS}")
    
    add_subdirectory(src)
    add_subdirectory(docs)
    add_subdirectory(test)
    

    最佳答案

    看来您的程序使用C++,并且库是用C编写的。

    C和C++中的符号编码方式不同(混杂)。当包含来自C++的C header 时,您需要告诉编译器。这可以通过声明符号extern "C"来完成。

    extern "C" {
    #include <libsbp/sbp.h>
    }
    

    一些库已经在其 header 中包含了此内容,但不包含sbp。

    关于c++ - CMake第三方库未定义引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29700746/

    10-12 18:42