0

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.

user3002473
  • 4,835
  • 8
  • 35
  • 61
  • Related: [What is an undefined reference/unresolved external symbol error and how do I fix it?](/q/12573816) – starball Feb 07 '23 at 03:07
  • 1
    `C:/Users/Michael/Documents/Development Libraries/opencv/build/x64/vc16/lib/opencv_world470d.lib` file is not the proper binary for MinGW. That is instead a debug msvc 2022 library. – drescherjm Feb 07 '23 at 03:11
  • ***I'm not sure what I can try to fix this*** My advice is to start over and install MinGW using msys2 like the documentation tells you: [https://code.visualstudio.com/docs/cpp/config-mingw](https://code.visualstudio.com/docs/cpp/config-mingw) then use msys2 package manager pacman to install opencv from the mingw64 terminal. Then your compiler and opencv will be compatible and installation of other open source libraries will be simpler since msys has many opensource libraries in its repository. – drescherjm Feb 07 '23 at 03:26
  • @drescherjm Ok, I've completely uninstalled MinGW and reinstalled it using msys2, according to the instructions listed on that page. I also ran `pacman -S mingw-w64-x86_64-opencv`. From here, I'm not really sure what to do. I can successfully generate my make file with cmake once I select the right kit in VSCode, but I'm still getting a linker error (error LNK2019). – user3002473 Feb 07 '23 at 04:26
  • "but I'm still getting a linker error (error LNK2019)" - OpenCV binaries and your project should be built by the same compiler (same vendor). If you build your project with MinGW and gcc (as in the question post), then you need OpenCV binaries from gcc. If you build your project with MSVC (which emits the errors like LNK2019), then you need OpenCV binaries from MSVC. – Tsyvarev Feb 07 '23 at 08:35
  • @Tsyvarev I see, thanks for explaining - how can I determine which compiler built my OpenCV binaries? When downloading from the OpenCV website, all I get is an exe to extract opencv to a folder, containing the `build` directory, `bin` and `include` subdirectories, etc. I can't tell what "type" of binaries are included. – user3002473 Feb 07 '23 at 15:31
  • @drescherjm How did you determine that `opencv_world470d.lib` is for msvc 2022? Just the `.lib` extension? Sorry for asking so many questions, I'm having trouble finding good documentation on this stuff as a beginner. – user3002473 Feb 07 '23 at 17:11
  • The `d` before the `.lib` means debug. The `vc16/lib` means the msvc compiler 2022. MinGW does have some compatibility with msvc for `c` based libraries but generally not for c++ based libraries. – drescherjm Feb 07 '23 at 17:17
  • You should be able to solve this using CMake, MinGW and the library installed using msys2. I can't test or help at the moment because of work. – drescherjm Feb 07 '23 at 17:18
  • @drescherjm Ah that makes sense, thanks! Well I'll keep toying with msys2 and see if I can get it. I was able to get it working on WSL, but I'd also like to continue learning about what I'm doing wrong here. – user3002473 Feb 07 '23 at 17:34
  • I will try to look at this in the evening (New York time zone) if I am not too tired from work. – drescherjm Feb 07 '23 at 18:15
  • The files in the [https://github.com/niconielsen32/ComputerVision/tree/master/VSCodeOpenCV](https://github.com/niconielsen32/ComputerVision/tree/master/VSCodeOpenCV) link compiled without any issue at all in the mingw64 terminal. The path to the `lenna.png` needs edited because it will most likely point to a wrong location. – drescherjm Feb 08 '23 at 05:15
  • Here is a pic of the application working: [https://ibb.co/dBJvLnj](https://ibb.co/dBJvLnj) I did not change any file other than main.cpp to fix the path. – drescherjm Feb 08 '23 at 05:23
  • To get it to run I needed to install [https://packages.msys2.org/groups/mingw-w64-x86_64-qt6](https://packages.msys2.org/groups/mingw-w64-x86_64-qt6) as there was (at least on my system) a Qt6 run time dependency. The code built and linked successfully without qt6 – drescherjm Feb 08 '23 at 05:30
  • 1
    @drescherjm wow thanks for doing all that extra work to help figure it out! Using the msys2 mingw compiler I was also able to get it to link properly! I had to install https://packages.msys2.org/package/mingw-w64-x86_64-ninja as well as the Qt6 dependency, but all seems to be working properly. Thanks again! I might type up what I learned in a community wiki answer. – user3002473 Feb 08 '23 at 22:30
  • BTW, I found the Qt6 dependency using `strace.exe ./opencvtest.exe` and then `ldd ./opencvtest.exe` in the folder containing opencvtest.exe – drescherjm Feb 08 '23 at 22:55

0 Answers0