我初次使用clion写c++处理cv问题,然后想要调用opencv的库函数,然后我在cmake文档中将opencv和项目链接,然后打印opencv版本、地址、库文件的时候都可以正常输出,但是在项目文件中调用opencv函数的时候就报错undefined reference to `cv::Mat::~Mat()',不止cv::Mat、cv::imshow等等库函数都报错。下面是我的cmake代码和项目代码,恳请大佬们解惑究竟是什么原因。
cmake代码:
cmake_minimum_required(VERSION 3.28)
project(untitled4 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
# 查找OpenCV包
find_package(OpenCV REQUIRED)
# 打印OpenCV版本信息
message(STATUS "OpenCV version: ${OpenCV_VERSION}")
# 包含OpenCV头文件
include_directories(${OpenCV_INCLUDE_DIRS})
# 添加可执行文件
add_executable(untitled4 main.cpp)
# 链接OpenCV库
target_link_libraries(makecsv ${OpenCV_LIBS})
cmake代码输出:
-- OpenCV version: 4.6.0
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: D:/Study/CLion_project/untitled4/cmake-build-debug
项目测试代码:
#include <iostream>
#include <opencv2/opencv.hpp>
int main()
{
// 打印 OpenCV 版本信息
std::cout << "OpenCV version: " << CV_VERSION << std::endl;
// 创建一个空的图像
cv::Mat image = cv::Mat::zeros(100, 100, CV_8UC3);
// 显示图像窗口
cv::imshow("Test Image", image);
cv::waitKey(0);
return 0;
}
报错内容:
[1/2] Building CXX object CMakeFiles/untitled4.dir/main.cpp.obj
[2/2] Linking CXX executable untitled4.exe
FAILED: untitled4.exe
C:\Windows\system32\cmd.exe /C "cd . && "D:\Study\CLion 2024.1.4\bin\mingw\bin\g++.exe" -g CMakeFiles/untitled4.dir/main.cpp.obj -o untitled4.exe -Wl,--out-implib,libuntitled4.dll.a -Wl,--major-image-version,0,--minor-image-version,0 D:/Study/Anaconda/Library/lib/opencv_gapi460.lib D:/Study/Anaconda/Library/lib/opencv_stitching460.lib D:/Study/Anaconda/Library/lib/opencv_alphamat460.lib D:/Study/Anaconda/Library/lib/opencv_aruco460.lib D:/Study/Anaconda/Library/lib/opencv_barcode460.lib D:/Study/Anaconda/Library/lib/opencv_bgsegm460.lib D:/Study/Anaconda/Library/lib/opencv_ccalib460.lib D:/Study/Anaconda/Library/lib/opencv_cvv460.lib D:/Study/Anaconda/Library/lib/opencv_dnn_objdetect460.lib D:/Study/Anaconda/Library/lib/opencv_dnn_superres460.lib D:/Study/Anaconda/Library/lib/opencv_dpm460.lib D:/Study/Anaconda/Library/lib/opencv_face460.lib D:/Study/Anaconda/Library/lib/opencv_fuzzy460.lib D:/Study/Anaconda/Library/lib/opencv_hdf460.lib D:/Study/Anaconda/Library/lib/opencv_hfs460.lib D:/Study/Anaconda/Library/lib/opencv_img_hash460.lib D:/Study/Anaconda/Library/lib/opencv_intensity_transform460.lib D:/Study/Anaconda/Library/lib/opencv_line_descriptor460.lib D:/Study/Anaconda/Library/lib/opencv_mcc460.lib D:/Study/Anaconda/Library/lib/opencv_quality460.lib D:/Study/Anaconda/Library/lib/opencv_rapid460.lib D:/Study/Anaconda/Library/lib/opencv_reg460.lib D:/Study/Anaconda/Library/lib/opencv_rgbd460.lib D:/Study/Anaconda/Library/lib/opencv_saliency460.lib D:/Study/Anaconda/Library/lib/opencv_stereo460.lib D:/Study/Anaconda/Library/lib/opencv_structured_light460.lib D:/Study/Anaconda/Library/lib/opencv_superres460.lib D:/Study/Anaconda/Library/lib/opencv_surface_matching460.lib D:/Study/Anaconda/Library/lib/opencv_tracking460.lib D:/Study/Anaconda/Library/lib/opencv_videostab460.lib D:/Study/Anaconda/Library/lib/opencv_wechat_qrcode460.lib D:/Study/Anaconda/Library/lib/opencv_xfeatures2d460.lib D:/Study/Anaconda/Library/lib/opencv_xobjdetect460.lib D:/Study/Anaconda/Library/lib/opencv_xphoto460.lib D:/Study/Anaconda/Library/lib/opencv_shape460.lib D:/Study/Anaconda/Library/lib/opencv_highgui460.lib D:/Study/Anaconda/Library/lib/opencv_datasets460.lib D:/Study/Anaconda/Library/lib/opencv_plot460.lib D:/Study/Anaconda/Library/lib/opencv_text460.lib D:/Study/Anaconda/Library/lib/opencv_ml460.lib D:/Study/Anaconda/Library/lib/opencv_phase_unwrapping460.lib D:/Study/Anaconda/Library/lib/opencv_optflow460.lib D:/Study/Anaconda/Library/lib/opencv_ximgproc460.lib D:/Study/Anaconda/Library/lib/opencv_video460.lib D:/Study/Anaconda/Library/lib/opencv_videoio460.lib D:/Study/Anaconda/Library/lib/opencv_imgcodecs460.lib D:/Study/Anaconda/Library/lib/opencv_objdetect460.lib D:/Study/Anaconda/Library/lib/opencv_calib3d460.lib D:/Study/Anaconda/Library/lib/opencv_dnn460.lib D:/Study/Anaconda/Library/lib/opencv_features2d460.lib D:/Study/Anaconda/Library/lib/opencv_flann460.lib D:/Study/Anaconda/Library/lib/opencv_photo460.lib D:/Study/Anaconda/Library/lib/opencv_imgproc460.lib D:/Study/Anaconda/Library/lib/opencv_core460.lib -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
D:\Study\CLion 2024.1.4\bin\mingw\bin/ld.exe: CMakeFiles/untitled4.dir/main.cpp.obj: in function `main':
D:/Study/CLion_project/untitled4/main.cpp:9: undefined reference to `cv::Mat::zeros(int, int, int)'
D:\Study\CLion 2024.1.4\bin\mingw\bin/ld.exe: D:/Study/CLion_project/untitled4/main.cpp:12: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
D:\Study\CLion 2024.1.4\bin\mingw\bin/ld.exe: D:/Study/CLion_project/untitled4/main.cpp:13: undefined reference to `cv::waitKey(int)'
D:\Study\CLion 2024.1.4\bin\mingw\bin/ld.exe: D:/Study/CLion_project/untitled4/main.cpp:17: undefined reference to `cv::Mat::~Mat()'
D:\Study\CLion 2024.1.4\bin\mingw\bin/ld.exe: D:/Study/CLion_project/untitled4/main.cpp:17: undefined reference to `cv::Mat::~Mat()'
D:\Study\CLion 2024.1.4\bin\mingw\bin/ld.exe: CMakeFiles/untitled4.dir/main.cpp.obj: in function `cv::MatExpr::operator cv::Mat() const':
D:/Study/Anaconda/Library/include/opencv2/core/mat.inl.hpp:3077: undefined reference to `cv::Mat::Mat()'
D:\Study\CLion 2024.1.4\bin\mingw\bin/ld.exe: D:/Study/Anaconda/Library/include/opencv2/core/mat.inl.hpp:3080: undefined reference to `cv::Mat::~Mat()'
D:\Study\CLion 2024.1.4\bin\mingw\bin/ld.exe: CMakeFiles/untitled4.dir/main.cpp.obj: in function `cv::MatExpr::~MatExpr()':
D:/Study/Anaconda/Library/include/opencv2/core/mat.hpp:3563: undefined reference to `cv::Mat::~Mat()'
D:\Study\CLion 2024.1.4\bin\mingw\bin/ld.exe: D:/Study/Anaconda/Library/include/opencv2/core/mat.hpp:3563: undefined reference to `cv::Mat::~Mat()'
D:\Study\CLion 2024.1.4\bin\mingw\bin/ld.exe: D:/Study/Anaconda/Library/include/opencv2/core/mat.hpp:3563: undefined reference to `cv::Mat::~Mat()'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.