CMake第三方库未定义参考

时间:2015-04-17 13:34:20

标签: c++ cmake

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

我想使用libsbp(https://github.com/swift-nav/libsbp)用C ++编写程序以与GPS模块通信。我克隆了存储库并安装了C-Library。所以现在在 / usr / local / lib 中有两个文件: libsbp.so libsbp-static.a ,标题位于的/ usr /本地/包含/ libsbp

在我自己的项目中,我将#include "libsbp/sbp.h"包含的标题也包含在内。

现在问题:如果我想使用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)

2 个答案:

答案 0 :(得分:4)

您的程序似乎使用C ++,而库是用C语言编写的。

C和C ++中的符号编码不同(受损)。从C ++中包含C头时,您需要告诉编译器。这可以通过声明符号extern "C"

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

有些库已在其标题中包含此内容,但不包括sbp。

答案 1 :(得分:0)

你有(至少)两种可能性:

  1. 安装库(这就是你所做的)
  2. 将库集成到CMake项目中
  3. 安装库时,需要稍微修改target_link_libraries命令:

    find_library(SBP_LIB sbp /usr/local/lib)
    target_link_libraries(main ${QT_LIBRARIES} ${catkin_LIBRARIES} ${SBP_LIB})
    

    将库集成到CMake项目中时,可以直接使用以下命令而不使用find_library。这是有效的,因为它是CMake已知的,因为它是在当前项目中构建的。

    target_link_libraries(main ${QT_LIBRARIES} ${catkin_LIBRARIES} sbp)