在 CMake 中链接外部 HDFql 库

Linking external HDFql library in CMake

我已经下载了 HDFql 库并将全部内容放在 /usr/local/ 我的 linux 系统中。我现在希望在 ROS 应用程序中使用这个库,所以我尝试在我的 CMakeList 中 link 它,如下所示:

cmake_minimum_required(VERSION 3.2)
project(data_generator)

add_compile_options(-std=c++11)
set(CMAKE_BUILD_TYPE Release) # Release, RelWithDebInfo
set(CMAKE_CXX_FLAGS "-std=c++11 -pthread ${CMAKE_CXX_FLAGS}")

find_package(catkin_simple REQUIRED)
catkin_simple(ALL_DEPS_REQUIRED)

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

set(HDFQL_ROOT "/usr/local/hdfql-2.1.0")

include_directories(${HDFQL_ROOT}/include)
find_library(HDFQL_LIB HDFql)

if (HDFQL_LIB)
  message("Library(HDFQL_LIB) found in ${HDFQL_LIB}")
else()
  message(FATAL_ERROR "Library (HDFQL_LIB) not found")
endif()

cs_add_executable(
        ${PROJECT_NAME}
        src/main.cpp
        src/data_create.cpp
        src/event_definitions.cpp
)

target_include_directories(${PROJECT_NAME}
    PUBLIC "${HDFQL_ROOT}/include"
)

target_link_libraries(${PROJECT_NAME}
   ${OpenCV_LIBRARIES}
   ${HDFQL_LIB}
)

# CMake Indexing
FILE(GLOB_RECURSE LibFiles "include/*")
add_custom_target(headers SOURCES ${LibFiles})

cs_install()
cs_export()

当我构建它时,它工作正常并输出消息:

Library(HDFQL_LIB) found in /usr/local/hdfql-2.1.0/lib/libHDFql.so

在我的 main.cpp 文件中包含以下内容:

#include <HDFql.hpp>
....
std::cout <<"HDFql version: " << HDFql::Version << std::endl;

输出为:HDFql version: 2.1.0.

但是,一旦我尝试实际使用库的功能 - 例如:

#include <HDFql.hpp>
....
std::cout <<"HDFql version: " << HDFql::Version << std::endl;
HDFql::execute("CREATE FILE /home/user/test.h5");

我收到错误: main.cpp:(.text+0x1858): undefined reference to `HDFql::execute(char const*)'

这向我表明,虽然 CMake 对包含没有问题,但在 link 实际库(即包括 libHDFql.a/libHDFql.so 文件)时遇到问题。谁能告诉我我做错了什么?

非常感谢!

问题是我需要包含我使用 /usr/local/hdfql-2.1.0/lib/libHDFql.so 的库 /usr/local/hdfql-2.1.0/wrapper/cpp/libHDFql.so。这非常令人抓狂,因为 reference manual 没有提及这一点,我花了太长时间才弄清楚这一点。哦,我希望这可以帮助其他人解决这个问题。

作为参考,这里有一个最小的 catkin 风格的 CMakeLists,可以工作:

cmake_minimum_required(VERSION 3.2)
project(project_name)

add_compile_options(-std=c++11)

find_package(catkin_simple REQUIRED)
catkin_simple(ALL_DEPS_REQUIRED)

set(HDFQL_ROOT "path/to/hdfql-2.1.0")

include_directories(${HDFQL_ROOT}/include)

set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_FLAGS "-std=c++11 -pthread ${CMAKE_CXX_FLAGS}")

cs_add_executable(
    ${PROJECT_NAME}
    # your source file 1
    # your source file 2 
    # ...
)

target_include_directories(${PROJECT_NAME}
    PUBLIC "${HDFQL_ROOT}/include"
)

target_link_libraries(
   ${PROJECT_NAME}
   ${OpenCV_LIBRARIES}
)

target_link_libraries(
   ${PROJECT_NAME}
   "${HDFQL_ROOT}/wrapper/cpp/libHDFql.so"
)

当然绝对路径不是很好,替代方法是将/usr/local/hdfql-2.1.0/lib添加到环境变量CMAKE_PREFIX_PATH(例如export CMAKE_PREFIX_PATH="/usr/local/hdfql-2.1.0/lib:$CMAKE_PREFIX_PATH")。