在CMake中链接外部HDFql库 [英] Linking external HDFql library in CMake
问题描述
我已经下载了 HDFql 库,并将全部内容放在了Linux的/ usr / local /中系统。现在,我希望在ROS应用程序中使用此库,因此我尝试将其链接到我的CMakeList中,如下所示:
I have downloaded the HDFql library and put the whole lot in /usr/local/ in my linux system. I now wish to use this library in a ROS application, so I have tried to link it in my CMakeList, which looks as following:
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()
当我构建它时,它可以正常工作并输出以下消息:
When I build this, it works fine and outputs the message:
Library(HDFQL_LIB) found in /usr/local/hdfql-2.1.0/lib/libHDFql.so
在我的main.cpp文件中包含以下内容:
with the following in my main.cpp file:
#include <HDFql.hpp>
....
std::cout <<"HDFql version: " << HDFql::Version << std::endl;
输出为: HDFql版本:2.1.0
。
但是,一旦我尝试实际使用库的功能-例如:
However, as soon as I try to actually use functions of the library - for example:
#include <HDFql.hpp>
....
std::cout <<"HDFql version: " << HDFql::Version << std::endl;
HDFql::execute("CREATE FILE /home/user/test.h5");
我收到错误:
main.cpp :(。 text + 0x1858):未定义对`HDFql :: execute(char const *)'的引用
这对我来说虽然CMake没有包含的问题,它在链接实际库(即包括libHDFql.a / libHDFql.so文件)时遇到麻烦。有人可以告诉我我在这里做错什么吗?
This suggests to me that while CMake has no issue with the includes, it is having trouble linking the actual library (ie including the libHDFql.a/libHDFql.so files). Can anyone tell me what I'm doing wrong here?
非常感谢!
推荐答案
问题是我需要包含库 /usr/local/hdfql-2.1.0/wrapper/cpp/libHDFql.so
使用 /usr/local/hdfql-2.1.0/lib/libHDFql.so
。这非常令人发疯,因为参考手册没有对此进行提及,我花了太长时间才弄清楚这一点。哦,我希望这对其他人有帮助。
The problem was that I needed to include the library /usr/local/hdfql-2.1.0/wrapper/cpp/libHDFql.so
, where I was using /usr/local/hdfql-2.1.0/lib/libHDFql.so
. It's pretty maddening, since the reference manual doesn't make any mention of this and I spent way too long figuring this out. Ohwell, I hope this helps anyone else with this problem.
作为参考,下面是一个最小的柔cat风格的CMakeList,可以使用:
For reference, here is a minimal catkin-style CMakeLists that will work:
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
)。
Of course the absolute paths aren't very pretty, the alternative is to add /usr/local/hdfql-2.1.0/lib
to the environment variable CMAKE_PREFIX_PATH
(eg export CMAKE_PREFIX_PATH="/usr/local/hdfql-2.1.0/lib:$CMAKE_PREFIX_PATH"
).
这篇关于在CMake中链接外部HDFql库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!