Answer the question
In order to leave comments, you need to log in
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:
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)
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()'
#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;
}
Answer the question
In order to leave comments, you need to log in
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:
MinGW
to the environment variable Path
so that I can use CMake
it from the command line.MinGW
СMake
the 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.(папку с бинарниками из пункта 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.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)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question