我正在尝试使用HDF5文件用法和pyTorch(C ++ API)在C ++中设置我的项目,并且存在一些链接问题。
所以...我运行Ubuntu 18.04 LTS。我已经安装了带有APT的HDF5,然后从pyTorch下载zip,并将其放入我的项目结构中的third_party目录中。
project/
--data/
----data.h5
--third_party/
----libtorch/
--main.cpp
--CMakeLists.txt
--run.sh
我的main.cpp看起来像:
#include <iostream>
#include <torch/torch.h>
#include "H5Cpp.h"
#define PRINT(x) std::cout<<x<<std::endl;
const H5std_string FILE_NAME("real/path/to/project/data/data.h5");
int main() {
// Create a vector of inputs.
std::vector<torch::jit::IValue> inputs;
inputs.push_back(torch::ones({1, 3, 3, 3}));
H5::H5File file(FILE_NAME, H5F_ACC_RDONLY);
}
然后CMakeLists:
project(cpptorch)
set(CMAKE_CXX_STANDARD 14)
# pyTorch
find_package(Torch REQUIRED)
# HDF5
find_package(HDF5 REQUIRED COMPONENTS C CXX HL REQUIRED)
include_directories( ${HDF5_INCLUDE_DIRS} )
link_directories( ${HDF5_LIBRARY_DIRS} )
add_executable(cpptorch main.cpp)
target_link_libraries(cpptorch ${HDF5_CXX_LIBRARIES})
target_link_libraries(cpptorch ${TORCH_LIBRARIES})
然后运行.sh
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
mkdir -p ${parent_path}/build
cd ${parent_path}/build
echo ${parent_path}
cmake -DCMAKE_PREFIX_PATH=${parent_path}/third_party/libtorch ..
make
./cpptorch
cd ..
现在我将描述这个问题。例如,当我在CMake中注释掉所有与HDF5连接的线时,然后手电筒就可以正常工作,就像我在CMake中注释掉Torch的线一样— HDF5运行良好。但是,当我尝试运行以上代码时,会收到错误消息:
CMakeFiles/cpptorch.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x5b): undefined reference to `H5::H5File::H5File(std::string const&, unsigned int, H5::FileCreatPropList const&, H5::FileAccPropList const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/cpptorch.dir/build.make:92: recipe for target 'cpptorch' failed
make[2]: *** [cpptorch] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/cpptorch.dir/all' failed
make[1]: *** [CMakeFiles/cpptorch.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
./run.sh: line 8: ./cpptorch: No such file or directory
如果有人知道如何解决?我发现它是链接问题,但不确定。