gpt4 book ai didi

c++ - CMake第三方库 undefined reference

转载 作者:搜寻专家 更新时间:2023-10-31 01:01:08 24 4
gpt4 key购买 nike

我已经阅读和搜索了很多(例如 1 2 3 ,一些 CMake 文档,类似项目等)以找到解决方案,但我一直无法解决我的问题。我相对Cmake 和 Linux (Ubuntu 14.04) 的新手。

我想使用 libsbp ( https://github.com/swift-nav/libsbp ) 用 C++ 编写一个程序来与 GPS 模块通信。我克隆了存储库并安装了 C-Library。所以现在在 /usr/local/lib 中有两个文件:libsbp.solibsbp-static.a 并且 header 在 < strong>/usr/local/include/libsbp

在我自己的项目中,我将 header 包含在 #include "libsbp/sbp.h" 中,这也有效。

现在的问题是:如果我想使用 libsbp 中的方法,例如sbp_state_init(&s); 我得到undefined reference to "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 头文件时,您需要告诉编译器。这可以通过声明符号 extern "C" 来完成。

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

一些图书馆已经在他们的标题中包含了这个,但不是 sbp。

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com