Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用CMake时由于HDF5兼容性问题导致的链接错误_C++_Cmake_Centos7_Hdf5 - Fatal编程技术网

C++ 使用CMake时由于HDF5兼容性问题导致的链接错误

C++ 使用CMake时由于HDF5兼容性问题导致的链接错误,c++,cmake,centos7,hdf5,C++,Cmake,Centos7,Hdf5,快速提问:如何编写CMakeLists.txt以避免下面描述的HDF5链接问题 我用HDF5库(1.101)制作C++项目, 我通过源代码文件tarball和CMake安装了它。 (安装在/usr/local/) 我还使用CMake为我的项目配置生成环境。 (CentOS 7上的CMake 3.8.1、g++5.3.1) 最近,由于链接错误,我的应用程序无法编译。 (附在下面的错误日志) 发现HDF5 1.8.12也是在我的一位同事更新服务器和一些软件包的过程中安装的。 这些库文件位于/usr/

快速提问:如何编写CMakeLists.txt以避免下面描述的HDF5链接问题

我用HDF5库(1.101)制作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.12中没有定义),导致链接错误
    (我查了HDF5C++ API引用,它看起来是真的)
因此,我应该如何编写/修复CMakeLists.txt以避免此类链接错误?
i、 e.在这种情况下,如何正确链接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
[100%]链接CXX可执行运行程序
CMakeFiles/runner.dir/src/GociHdf5Handler.cpp.o:函数中
“CGociHdf5Handler::OpenRrs(CGociHdf5Handler::availableerrs)”:
GociHdf5Handler.cpp:(.text+0x166b):对的未定义引用
`H5::H5Location::openDataSet(std::string const&)const'
GociHdf5Handler.cpp:(.text+0x18ad):对的未定义引用
`H5::H5对象::属性存在(std::string const&)const'
GociHdf5Handler.cpp:(.text+0x18e1):对的未定义引用
`H5::H5Object::openAttribute(std::string const&)const'
CMakeFiles/runner.dir/src/GociHdf5Handler.cpp.o:函数中
`CGociHdf5Handler::OpenFlags()':
GociHdf5Handler.cpp:(.text+0x1c72):对的未定义引用
`H5::H5Location::openDataSet(std::string const&)const'
CMakeFiles/runner.dir/src/GociHdf5Handler.cpp.o:函数中
`CGociHdf5Handler::WriteOutput(std::unique_ptr,std::string)“”:
GociHdf5Handler.cpp:(.text+0x20af):对的未定义引用
`H5::H5Location::createGroup(字符常量*,无符号长)常量'
GociHdf5Handler.cpp:(.text+0x20cc):对的未定义引用
`H5::H5Location::createGroup(字符常量*,无符号长)常量'
GociHdf5Handler.cpp:(.text+0x20e9):对的未定义引用
`H5::H5Location::createGroup(字符常量*,无符号长)常量'
GociHdf5Handler.cpp:(.text+0x2109):对的未定义引用
`H5::H5Location::createGroup(字符常量*,无符号长)常量'
GociHdf5Handler.cpp:(.text+0x2179):对的未定义引用
`H5::H5Location::createDataSet(标准::字符串常量&,H5::数据类型常量&,
H5::数据空间常数&,H5::DSetCreatPropList常数&)常数'
GociHdf5Handler.cpp:(.text+0x2229):对的未定义引用
`H5::H5Object::createAttribute(std::string const&,H5::DataType const&,
H5::数据空间常数&,H5::PropList常数&)常数'
collect2:错误:ld返回了1个退出状态
make[2]:***[runner]오류 1.
make[1]:***[CMakeFiles/runner.dir/all]오류 2.
制作:**[全部]오류 2.

使用
find_-package
意味着您的代码将在
包含目录和
目标链接库中使用它在中定义的变量

根据,您的案例的正确用法为:

# Explicitely list required HDF5 components
find_package(HDF5 COMPONENTS CXX HL)

include_directories(${HDF5_INCLUDE_DIRS})
target_link_libraries(
    runner
    ${HDF5_LIBRARIES} # This should list all libraries.
)
HDF5的cmake“finder”可能受环境变量
HDF5\u ROOT
的影响,以确定在多个安装的情况下要使用的HDF5安装

  • 创建新的生成目录以删除任何缓存设置

  • 定义
    HDF5\u根

    export HDF5_ROOT=/usr/local
    
  • (如果HDF5位于子目录中,则导出HDF5_ROOT=/usr/local/HDF5-1.10.1

  • 再次运行cmake
  • 创建新版本或删除缓存设置非常重要,否则将无法获取该设置


    如果这不起作用,我建议删除HDF的
    /usr
    安装。

    我仍然没有消除错误:-(就在
    target\u link\u libraries
    之前,我添加了
    消息(状态为“HDF5\u libraries:${HDF5\u libraries}”)
    $HDF5_库的位置上没有显示任何内容。我应该调查什么?我添加了当前CmakeList的更多详细信息。TXT奇怪的是,变量HDF5_库就是其中之一。CMake应该将变量的空性视为“未找到包”。顺便问一句,您确定
    查找_包(HDF5)吗
    是否实际找到了软件包?在配置过程中,它应该反映在CMake消息中。