C++ CMake链接错误,查找库但;“未定义引用”;
我正在尝试以libtorch和opencv作为依赖项来构建一个项目。我使用cmake作为我的构建系统,因为这两个库都建议使用它。我现在陷入困境,我正试图用libtorch和opencv来编译一个最小的程序 我的程序是这样的C++ CMake链接错误,查找库但;“未定义引用”;,c++,opencv,cmake,C++,Opencv,Cmake,我正在尝试以libtorch和opencv作为依赖项来构建一个项目。我使用cmake作为我的构建系统,因为这两个库都建议使用它。我现在陷入困境,我正试图用libtorch和opencv来编译一个最小的程序 我的程序是这样的 #include <opencv2/opencv.hpp> #include <torch/torch.h> void showImage(cv::Mat); at::Tensor imgToTensor(std::string img_path);
#include <opencv2/opencv.hpp>
#include <torch/torch.h>
void showImage(cv::Mat);
at::Tensor imgToTensor(std::string img_path);
using namespace cv;
using std::cout;
using std::endl;
int main() {
std::string img_path = "./images/01 HEAVENLY STAR080.png";
auto tensor = imgToTensor(img_path);
cout << tensor << endl;
}
at::Tensor imgToTensor(std::string img_path){
Mat origImage;
Mat normalizedImage;
Mat sizedImage(500, 200, CV_32FC3);
origImage = imread(img_path, 1);
origImage.convertTo(normalizedImage, CV_32FC3);
resize(normalizedImage, sizedImage, sizedImage.size(), 0, 0, INTER_LINEAR);
auto input = torch::from_blob(sizedImage.data, {sizedImage.rows, sizedImage.cols, 3});
return input;
}
void showImage(Mat image){
namedWindow("Display window", WINDOW_AUTOSIZE);
imshow("Display window", image);
waitKey(0);
}
这是cmake的输出,因此我知道可以找到这些库:
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /usr/local (found version "4.3.0")
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Found Torch: /usr/local/libtorch/lib/libtorch.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jacob/Documents/KTH/KEX/codeEnvironment/ML_Classification_Toolkit/ML_tool/ConvNet/build
这就是我得到的错误:
/usr/bin/ld: CMakeFiles/main.dir/main.cpp.o: in function `imgToTensor(std::string)':
main.cpp:(.text+0x8d9): undefined reference to `cv::imread(std::string const&, int)'
/usr/bin/ld: CMakeFiles/main.dir/main.cpp.o: in function `showImage(cv::Mat)':
main.cpp:(.text+0xbac): undefined reference to `cv::namedWindow(std::string const&, int)'
/usr/bin/ld: main.cpp:(.text+0xc0d): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:122: main] Error 1
make[1]: *** [CMakeFiles/Makefile2:96: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:104: all] Error 2
任何帮助都将不胜感激 不要使用传统的cmake工具 CMake大大简化了构建和链接库的过程 不必手动告诉库路径和链接器选项,只需创建目标与库的依赖关系即可 下面是一个关于它应该是什么样子的简短片段:
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(ConvNet)
find_package(OpenCV REQUIRED COMPONENTS opencv_highgui)
find_package(Torch REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main PRIVATE opencv_highgui torch)
观察结果
target\u link\u库
!因为现在我们不是告诉编译器该做什么,而是将目标main
链接到目标opencv\u highgui
和torch
opencv\u highgui
和torch
与main类似,都是目标。使用添加库
和添加可执行文件
创建目标
目标具有公共和私有接口。为了设置目标属性,我们调用函数
target.*
(例如target\u compile\u功能(mytarget PUBLIC cxx\u std\u 20)
说targetmytarget
将使用C++20功能,如果有人链接到我们,他将自动使用相同的编译器选项`我下载的libtorch版本不支持cxx11 abi,因此与opencv不兼容。通过更改使用的libtorch版本修复
我使用的版本是cxx11之前的abi,如下所示:
我切换到了cxx11 abi。这是我第一次使用cmake,我不知道我在做什么。非常感谢你的建设性批评!你确定
OpenCV_LIBS
变量确实包含OpenCV库吗?请参阅(及其注释)关于检查这个事实。是的,确实如此。当我只编译opencv.Puting消息(状态“${Open_CV_LIBS}”)时,同样的语法也会起作用打印库列表。因此,它是包含库列表的OpenCV_LIBS
或Open_CV_LIBS
变量?您最后的评论似乎与问题帖子中的代码相矛盾。在任何地方,您都可以使用make VERBOSE=1
构建项目,并检查用于链接的确切命令行。好的,我更新了我的cmak根据你的建议,我仍然定义了Torch_DIR,因为它不起作用。但是,我也有同样的错误。你是说这个问题可能是因为opencv和libtorch之间的兼容性问题吗?我将尝试在我的笔记本电脑上编译…也许我输入了一些错误,让我下载opencv和Torch:)这可能是因为两个库使用不同的ABI?我想问题是我下载了一个预编译的libtorch库,它是用_GLIBCXX_USE_CXX11_ABI=0标志编译的,这破坏了与opencv的兼容性。我将从源代码下载它,自己构建libtorch,看看它是否有效。
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(ConvNet)
find_package(OpenCV REQUIRED COMPONENTS opencv_highgui)
find_package(Torch REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main PRIVATE opencv_highgui torch)