I'm sure this has been asked a bunch of times before, but for whatever reason none of the prior answers have resolved my specific issue.
I was following along with this video, attempting to get started with an OpenCV C++ project in Visual Studio Code using CMake.
The problem is having is that when I attempt to build my code, the OpenCV libraries aren't being linked correctly. I'm getting a bunch of "undefined reference to cv::" errors during the linking stage. An example output when I attempt to build is as follows:
[build] [ 50%] Linking CXX executable OpenCVProject.exe
[build] c:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\OpenCVProject.dir/objects.a(main.cpp.obj): in function `main':
[build] C:/Users/Michael/Dropbox/Computer Science/C, C++/TestProjects/OpenCVProject/main.cpp:6: undefined reference to `cv::Mat::Mat()'
[build] c:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/Michael/Dropbox/Computer Science/C, C++/TestProjects/OpenCVProject/main.cpp:7: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
[build] c:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/Michael/Dropbox/Computer Science/C, C++/TestProjects/OpenCVProject/main.cpp:7: undefined reference to `cv::Mat::operator=(cv::Mat&&)'
[build] c:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/Michael/Dropbox/Computer Science/C, C++/TestProjects/OpenCVProject/main.cpp:7: undefined reference to `cv::Mat::~Mat()'
Some clarifications:
- I downloaded and extracted pre-built OpenCV binaries directly from the OpenCV website. They're located in "C:\Users\Michael\Documents\Development Libraries\opencv".
- I've added the OpenCV lib and bin files to my Path environment variable (system-wide).
- CMake is able to find the OpenCV package. CMake can configure and generate a makefile from my CMakeLists.txt.
- The 'include directories' are also correct. Moreover, VSCode isn't giving me any errors when I include OpenCV headers in my project - IntelliSense can detect the names.
My CMakeLists.txt file looks as follows:
cmake_minimum_required(VERSION 3.14.0)
project(OpenCVProject VERSION 1.0.0)
set(CMAKE_CXX_STANDARD 17)
# set(CMAKE_VERBOSE_MAKEFILE ON)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
# This is just to confirm that the include directory is correct, which it is
message(STATUS "Looking for OpenCV includes in: ${OpenCV_INCLUDE_DIRS}")
add_executable(OpenCVProject main.cpp)
target_link_libraries(OpenCVProject ${OpenCV_LIBS})
To be perfectly clear, CMake can configure and generate the makefile for this project.
When I uncomment the set(CMAKE_VERBOSE_MAKEFILE ON)
line, the build output is as follows:
[build] [ 50%] Linking CXX executable OpenCVProject.exe
[build] "C:\Program Files\CMake\bin\cmake.exe" -E cmake_link_script CMakeFiles\OpenCVProject.dir\link.txt --verbose=1
[build] "C:\Program Files\CMake\bin\cmake.exe" -E rm -f CMakeFiles\OpenCVProject.dir/objects.a
[build] C:\mingw64\bin\ar.exe qc CMakeFiles\OpenCVProject.dir/objects.a @CMakeFiles\OpenCVProject.dir\objects1.rsp
[build] C:\mingw64\bin\g++.exe -g -Wl,--whole-archive CMakeFiles\OpenCVProject.dir/objects.a -Wl,--no-whole-archive -o OpenCVProject.exe -Wl,--out-implib,libOpenCVProject.dll.a -Wl,--major-image-version,0,--minor-image-version,0 @CMakeFiles\OpenCVProject.dir\linkLibs.rsp
[build] c:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\OpenCVProject.dir/objects.a(main.cpp.obj): in function `main':
(the same undefined reference errors)
The CMakeFiles\OpenCVProject.dir\linkLibs.rsp
file also seems to contain the correct directory to the .lib
file:
Contents of linkLibs.rsp:
C:/Users/Michael/Documents/Development Libraries/opencv/build/x64/vc16/lib/opencv_world470d.lib
I'm not sure what I can try to fix this.
I'd prefer to stick with this setup as it seems I'm really close to getting this to work - I don't want to switch to Linux, or switch to Visual Studio 2022 at this point.