R
R
Rag'n' Code Man2022-03-24 22:30:39
C++ / C#
Rag'n' Code Man, 2022-03-24 22:30:39

Why does an `undefined reference` error occur when compiling an application with OpenCV enabled?

There are only 3 libraries in the application: SFML, TinyFD and OpenCV. The first two work great, but for some reason OpenCV doesn't want to work.

I built the library from source, decided to build it from scratch when my previous application crashed for the same reason.

Here is the library itself in the project:

623cc709498e1783155213.png
Next, its connection in CMake:

cmake_minimum_required(VERSION 3.0)
project(ImagePixelizer VERSION 0.1)

set(CMAKE_CXX_STANDARD 20)

add_subdirectory(lib/tinyfd)

add_executable(ImagePixelizer src/main.cpp src/SelectFileDialog/FileDialog.cpp)

target_include_directories(ImagePixelizer PRIVATE
        lib/SFML/include/
        lib/OpenCV/include
        lib/NFD/include
        lib/wxWidgets/include
        lib/tinyfd/include)

target_link_libraries(
        ImagePixelizer
        ${PROJECT_SOURCE_DIR}/lib/SFML/lib/libsfml-graphics.a
        ${PROJECT_SOURCE_DIR}/lib/SFML/lib/libsfml-window.a
        ${PROJECT_SOURCE_DIR}/lib/SFML/lib/libsfml-system.a
        ${PROJECT_SOURCE_DIR}/lib/OpenCV/lib/opencv_core455.lib
        ${PROJECT_SOURCE_DIR}/lib/OpenCV/lib/opencv_gapi455.lib
        ${PROJECT_SOURCE_DIR}/lib/OpenCV/lib/opencv_photo455.lib
        ${PROJECT_SOURCE_DIR}/lib/OpenCV/lib/opencv_imgcodecs455.lib
        ${PROJECT_SOURCE_DIR}/lib/OpenCV/lib/opencv_imgproc455.lib
        ${PROJECT_SOURCE_DIR}/lib/OpenCV/lib/opencv_video455.lib
        ${PROJECT_SOURCE_DIR}/lib/OpenCV/lib/opencv_videoio455.lib
        tinyfd)

And the error itself:

cmd.exe /C "cd . && H:\CLION2~1.3\bin\mingw\bin\G__~1.EXE -g  CMakeFiles/ImagePixelizer.dir/src/main.cpp.obj CMakeFiles/ImagePixelizer.dir/src/SelectFileDialog/FileDialog.cpp.obj -o ImagePixelizer.exe -Wl,--out-implib,libImagePixelizer.dll.a -Wl,--major-image-version,0,--minor-image-version,0  ../lib/SFML/lib/libsfml-graphics.a  ../lib/SFML/lib/libsfml-window.a  ../lib/SFML/lib/libsfml-system.a  ../lib/OpenCV/lib/opencv_core455.lib  ../lib/OpenCV/lib/opencv_gapi455.lib  ../lib/OpenCV/lib/opencv_photo455.lib  ../lib/OpenCV/lib/opencv_imgcodecs455.lib  ../lib/OpenCV/lib/opencv_imgproc455.lib  ../lib/OpenCV/lib/opencv_video455.lib  ../lib/OpenCV/lib/opencv_videoio455.lib  lib/tinyfd/libtinyfd.a  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
H:\CLion 2021.3\bin\mingw\bin/ld.exe: CMakeFiles/ImagePixelizer.dir/src/main.cpp.obj:D:/Code/ImagePixelizer/src/main.cpp:14: undefined reference to `cv::VideoCapture::VideoCapture()'
H:\CLion 2021.3\bin\mingw\bin/ld.exe: CMakeFiles/ImagePixelizer.dir/src/main.cpp.obj: in function `main':
D:/Code/ImagePixelizer/src/main.cpp:29: undefined reference to `cv::VideoCapture::~VideoCapture()'
H:\CLion 2021.3\bin\mingw\bin/ld.exe: D:/Code/ImagePixelizer/src/main.cpp:29: undefined reference to `cv::VideoCapture::~VideoCapture()'

It didn't take much to get this error:

#include <SFML/Graphics.hpp>

#include <opencv2/videoio.hpp>

#include "SelectFileDialog/FileDialog.hpp"

int main() {
    sf::RenderWindow window(sf::VideoMode(800, 600), "Image Pixelizer");

    cv::VideoCapture capture; // Использование API OpenCV

    while (window.isOpen()) {
        sf::Event event{};

        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.display();
    }

    return 0;
}


An error is thrown on any use of the OpenCV API.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rag'n' Code Man, 2022-03-26
@iDmitriyWinX

It turned out that a library compiled by one compiler would not be read by another compiler.
This version of OpenCV was compiled with the Microsoft Visual C++ 2015 compiler (the library files are in the vc15 folder) and could not be read by my MinGW. And when I compiled the library myself, I also made this mistake when generating a project for Visual Studio 2022.
Building OpenCV with the MinGW compiler.
To compile the source code for MinGW, I followed the following procedure:

  1. Added the path to MinGWto the environment variable Pathso that I can use CMakeit from the command line.
  2. Download the source code of the library. (Version 4.5.5)
  3. Configured and generated project for compiler fromMinGW
  4. I opened the command line in the folder where СMakethe binaries are placed (Where to build the binares) and executed the command mingw32-make install, after which the rather long process of compiling the library began.
  5. Further, according to the instructions from the command line after the compilation was completed, I opened the (папку с бинарниками из пункта 4)/install. There are dynamic and static libraries (import libraries, as I understand it) in their respective folders. These files are transferred to the project. The header files are in /install/include.
  6. After that, respectively, you can link with all the static libraries you need, and either put the .dlls in the folder with the executable file, or add them to the Path.

target_link_libraries(
        Target
        ${PROJECT_SOURCE_DIR}/lib/OpenCV/lib/libopencv_imgcodecs455.dll.a
        ${PROJECT_SOURCE_DIR}/lib/OpenCV/lib/libopencv_imgproc455.dll.a
        ${PROJECT_SOURCE_DIR}/lib/OpenCV/lib/libopencv_core455.dll.a
        ${PROJECT_SOURCE_DIR}/lib/OpenCV/lib/libopencv_highgui455.dll.a
        ${PROJECT_SOURCE_DIR}/lib/OpenCV/lib/libopencv_gapi455.dll.a
        ${PROJECT_SOURCE_DIR}/lib/OpenCV/lib/libopencv_video455.dll.a
        ${PROJECT_SOURCE_DIR}/lib/OpenCV/lib/libopencv_videoio455.dll.a
        ${PROJECT_SOURCE_DIR}/lib/OpenCV/lib/libopencv_photo455.dll.a)

After the done machinations, the project is successfully launched without any errors.
This is a copy of my own answer on StackOverflow. Decided to add more here, in case it's useful to someone.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question