【发布时间】:2018-03-03 12:14:33
【问题描述】:
快速提问:如何编写 CMakeLists.txt 以避免下面描述的 HDF5 链接问题?
我正在使用 HDF5 库 (1.10.1) 制作 C++ 项目,
我通过源代码文件 tarball 和 CMake 安装的。
(安装在/usr/local/)
我还使用 CMake 为我的项目配置构建环境。
(CentOS 7 上的 CMake 3.8.1、g++ 5.3.1)
最近由于链接错误,我的应用程序无法编译。
(下面附上错误日志)
发现在我的一位同事更新服务器和一些软件包期间也安装了 HDF5 1.8.12。
这些库文件位于/usr/lib64/。
所以,我认为,
- 从 1.10.1 引用的包含文件
- 链接时从 1.8.12 引用库
- 出现兼容性问题(即这些 API 未在 HDF5 1.8.12 中定义),导致链接错误
(我查找了 HDF5 C++ API 参考,它看起来是真的)
那么,我应该如何编写/修复 CMakeLists.txt 以避免此类链接错误?
即在这种情况下如何正确链接 HDF5 1.10.1?
编辑: 添加了有关 CMakeLists.txt 的更多详细信息
以下是我项目的 CMakeLists.txt 的一些详细信息:
cmake_minimum_required(VERSION 3.8.1)
# Reference: https://github.com/dmonopoly/gtest-cmake-example/blob/master/CMakeLists.txt
project (adl-boilerplate)
enable_language(CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.64.0 COMPONENTS program_options)
option(ENABLE_HDF5 "Enable HDF5 support" ON)
if(ENABLE_HDF5)
set(HDF5_ROOT /usr/local/hdf5/)
find_package(HDF5 "1.10.1" REQUIRED)
if(HDF5_FOUND)
include_directories(${HDF5_INCLUDE_DIR})
set(_hdf5_libs hdf5 hdf5_cpp hdf5_hl hdf5_hl_cpp)
message(STATUS "HDF5 root: ${HDF5_ROOT}")
message(STATUS "HDF5 version: ${HDF5_VERSION}")
message(STATUS "HDF5 include dir: ${HDF5_INCLUDE_DIRS}")
message(STATUS "HDF5 CXX lib: ${HDF5_LIBRARIES}")
message(STATUS "CMake library path: " ${CMAKE_LIBRARY_PATH})
else()
# Download HDF5 library and define hdf5_local
# ...
endif()
endif()
# Get includes/srcs
file(GLOB_RECURSE adl-boilerplate_SOURCES "src/*.cpp")
file(GLOB_RECURSE adl-boilerplate_HEADERS "src/*.h")
set(adl-boilerplate_INCLUDE_DIRS "include")
include_directories(${adl-boilerplate_INCLUDE_DIRS})
if(ENABLE_HDF5)
add_executable(
runner
${adl-boilerplate_SOURCES}
)
target_link_libraries(
runner
${_hdf5_libs}
)
endif()
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(runner ${Boost_LIBRARIES})
endif()
这是我执行make时的错误日志
[100%] Linking CXX executable runner
CMakeFiles/runner.dir/src/GociHdf5Handler.cpp.o: In function
'CGociHdf5Handler::OpenRrs(CGociHdf5Handler::AvailableRrs)':
GociHdf5Handler.cpp:(.text+0x166b): undefined reference to
`H5::H5Location::openDataSet(std::string const&) const'
GociHdf5Handler.cpp:(.text+0x18ad): undefined reference to
`H5::H5Object::attrExists(std::string const&) const'
GociHdf5Handler.cpp:(.text+0x18e1): undefined reference to
`H5::H5Object::openAttribute(std::string const&) const'
CMakeFiles/runner.dir/src/GociHdf5Handler.cpp.o: In function
`CGociHdf5Handler::OpenFlags()':
GociHdf5Handler.cpp:(.text+0x1c72): undefined reference to
`H5::H5Location::openDataSet(std::string const&) const'
CMakeFiles/runner.dir/src/GociHdf5Handler.cpp.o: In function
`CGociHdf5Handler::WriteOutput(std::unique_ptr<float [],
std::default_delete<float []> >, std::string)':
GociHdf5Handler.cpp:(.text+0x20af): undefined reference to
`H5::H5Location::createGroup(char const*, unsigned long) const'
GociHdf5Handler.cpp:(.text+0x20cc): undefined reference to
`H5::H5Location::createGroup(char const*, unsigned long) const'
GociHdf5Handler.cpp:(.text+0x20e9): undefined reference to
`H5::H5Location::createGroup(char const*, unsigned long) const'
GociHdf5Handler.cpp:(.text+0x2109): undefined reference to
`H5::H5Location::createGroup(char const*, unsigned long) const'
GociHdf5Handler.cpp:(.text+0x2179): undefined reference to
`H5::H5Location::createDataSet(std::string const&, H5::DataType const&,
H5::DataSpace const&, H5::DSetCreatPropList const&) const'
GociHdf5Handler.cpp:(.text+0x2229): undefined reference to
`H5::H5Object::createAttribute(std::string const&, H5::DataType const&,
H5::DataSpace const&, H5::PropList const&) const'
collect2: error: ld returned 1 exit status
make[2]: *** [runner] 오류 1
make[1]: *** [CMakeFiles/runner.dir/all] 오류 2
make: *** [all] 오류 2
【问题讨论】: