gpt4 book ai didi

c++ - CMake链接错误,找到库但 "undefined reference"

转载 作者:行者123 更新时间:2023-12-02 09:49:27 26 4
gpt4 key购买 nike

我正在尝试构建一个以 libtorch 和 opencv 作为依赖项的项目。我使用 cmake 作为我的构建系统,因为它被推荐用于这两个库。我目前被卡住了,我正在尝试使用 libtorch 和 opencv 来编译一个最小的程序。

我的程序是这样的

#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);
}

这是我的 CMakeLists.txt:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(ConvNet)

set(Torch_DIR /usr/local/libtorch/share/cmake/Torch)

find_package(OpenCV REQUIRED)
find_package(Torch REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
add_executable(main main.cpp)
target_link_libraries(main "${OpenCV_LIBS}" "${TORCH_LIBRARIES}")

这是 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

任何帮助将不胜感激!

最佳答案

我下载的libtorch版本不支持cxx11 abi,​​因此不兼容opencv。通过更改使用的 libtorch 版本修复。

我使用的版本是来自这里的 pre-cxx11 abi: https://pytorch.org/get-started/locally/

我切换到 cxx11 abi。

关于c++ - CMake链接错误,找到库但 "undefined reference",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61456607/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com