1.错误描述
- opencv 4.0.0
- libtorch1.0.0 (官网下载,非源码安装)
- gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.11)
- cmake version 3.5.1
Scanning dependencies of target frameworks
[ 50%] Building CXX object CMakeFiles/frameworks.dir/main.cpp.o
[100%] Linking CXX executable frameworks
CMakeFiles/frameworks.dir/main.cpp.o:在函数‘main’中:
/media/xx/data/cpps/frameworks/main.cpp:26:对‘cv::imread(std::string const&, int)’未定义的引用
collect2: error: ld returned 1 exit status
CMakeFiles/frameworks.dir/build.make:159: recipe for target 'frameworks' failed
make[3]: *** [frameworks] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/frameworks.dir/all' failed
make[2]: *** [CMakeFiles/frameworks.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/frameworks.dir/rule' failed
make[1]: *** [CMakeFiles/frameworks.dir/rule] Error 2
Makefile:118: recipe for target 'frameworks' failed
make: *** [frameworks] Error 2
2.错误原因
类似的参考资料
原因如下:
# TorchConfig.cmake line 71
# When we build libtorch with the old GCC ABI, dependent libraries must too.
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=0")
endif()
(经过诸位大神的分析)TorchConfig.cmake文件中,让ABI=0,具体意思我也不大懂,貌似就关闭了CXX11_ABI,而opencv的东东就在这个里面呀,所以冲突,导致"对‘cv::imread(std::string const&, int)’未定义的引用",上面的第三个参考网址同样提到了这个问题.
3.解决办法
目前还在测试,主要考虑以下两个方面(opencv降版本,libtorch源码安装),测试结果我也会稍后告知.
- 使用opencv3.4版本,(不想测试了,我同事说OK!),还是要装这个版本;
wget -c https://codeload.github.com/opencv/opencv/zip/3.4.3
wget -c https://codeload.github.com/opencv/opencv_contrib/tar.gz/3.4.3
cmake -D CMAKE_BUILD_TYPE=Releas \
-D OPENCV_EXTRA_MODULES_PATH=/home/download/opencv_contrib-master/modules \
-D WITH_TBB=ON \
-D WITH_CUDA=OFF \
-D WITH_CUBLAS=OFF \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D WITH_V4L=ON \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D PYTHON2_EXECUTABLE=/usr/bin/python2 \
-D PYTHON2_LIBRARY=/usr/lib/python2.7 \
-D PYTHON2_INCLUDE_DIR=/usr/include/python2.7 \
-D PYTHON2_NUMPY_INCLUDE_DIRS=/usr/lib/python2.7/dist-packages/numpy/core/include/ \
-D PYTHON3_EXECUTABLE=/usr/bin/python3 \
-D PYTHON3_LIBRARY=/usr/lib/python3.5 \
-D PYTHON3_INCLUDE_DIR=/usr/include/python3.5 \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3.5/dist-packages/numpy/core/include/ \
-D BUILD_EXAMPLES=ON ..
- 有人说添加这样的头文件,来源OpenCV4.0+VS2017下运行程序总是出现未定义标识符, 我发现这个方法的时候已经,转战到opencv3.4版本了,所以没有测试. 这种办法后面实测了,行不通呀!
#include "opencv2/imgproc/imgproc_c.h"
- 使用源码安装libtorch(推荐),测试
cv::imread
和cv::imwrite
成功.(划掉的部分应该是我搞错了)但是还是有未定义的引用问题发生在其他函数上面,cv::imshow
,cv::namewindow
,cv::waitkey
等.所以我在重新源码安装libtorch的同时,opencv源码安装3.4.3版本.
官方源码安装指南 https://github.com/pytorch/pytorch#from-source
法1.适合1.0rc版本,目前版本见法2
# libtorch的源码安装方法
PYTORCH_COMMIT_ID="8619230"
git clone https://github.com/pytorch/pytorch.git
cd pytorch && git checkout ${PYTORCH_COMMIT_ID}
mkdir build && cd ./build
python3 ../tools/build_libtorch.py # 输出目录在./pytorch/pytorch/torch/lib/tmp_install
法2.源码安装pytorch1.2,@2019-06-10
conda activate torch # your conda env
conda install numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing #python的依赖项,不行就把conda换pip
# Add LAPACK support for the GPU if needed
conda install -c pytorch magma-cuda100 # or [magma-cuda92 | magma-cuda100 ] depending on your cuda version
git clone --recursive https://github.com/pytorch/pytorch # 下载源码
cd pytorch
# if you are updating an existing checkout
# 一直执行下面两行,直到没有错误发生,我的渣渣网,搞了几个小时。
git submodule sync
git submodule update --init --recursive #正常等待半小时
# 编译安装
python setup.py install #等待半小时
torchvision的源码安装 Torchvision 源码安装[Ubuntu]
git clone https://github.com/pytorch/vision.git
cd vision
python setup.py install
参考网
1.Building PyTorch with LibTorch From Source with CUDA Support
2.github的一些说明
3.利用Pytorch的C++前端(libtorch)读取预训练权重并进行预测
4.https://github.com/pytorch/pytorch#from-source
网友评论