OpenCV compilation error with CMake in C++

Hello there,

I have OpenCV from source installed but I don’t know why the compilation of my C++ code fails. Can someone who is familiar with C++ explain what is happening please?
My C++ code:
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main() {
	Mat frame;
	cout << "This is the main project." << endl;

My CMakeLists.txt file:
cmake_minimum_required (VERSION 3.5)

project (main)

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11")
set (source_dir "${PROJECT_SOURCE_DIR}/src/")
SET (OpenCV_DIR /home/installation/OpenCV-3.3.1/opencv/include/opencv2)

file (GLOB source_files "${source_dir}/*.cpp")

add_executable (main ${source_files})

Console output when trying to compile with CMake:
CMakeFiles/main.dir/src/Main.cpp.o: In function cv::String::~String()': /usr/include/opencv2/core/cvstd.hpp:648: undefined reference to cv::String::deallocate()’
CMakeFiles/main.dir/src/Main.cpp.o: In function cv::String::operator=(cv::String const&)': /usr/include/opencv2/core/cvstd.hpp:656: undefined reference to cv::String::deallocate()’
CMakeFiles/main.dir/src/Main.cpp.o: In function cv::Mat::~Mat()': /usr/include/opencv2/core/mat.inl.hpp:692: undefined reference to cv::fastFree(void*)’
CMakeFiles/main.dir/src/Main.cpp.o: In function cv::Mat::release()': /usr/include/opencv2/core/mat.inl.hpp:804: undefined reference to cv::Mat::deallocate()’
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:94: recipe for target ‘main’ failed
make[2]: *** [main] Error 1
CMakeFiles/Makefile2:67: recipe for target ‘CMakeFiles/main.dir/all’ failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:83: recipe for target ‘all’ failed
make: *** [all] Error 2

Thanks for the answers in advance!

Hi nemestomi2,

Thanks for reaching out!

“undefined reference” usually indicates that the compiler/linker cannot find the shared libraries used by your program.

I notice that you call

add_executable(main ${source_files})

You’ll likely need to also call

target_link_libraries(main <lib1> <lib2>)

To link the necessary libraries. I believe the OpenCV cmake package bundles the libraries into the variable OpenCV_LIBS, in which case you could do

target_link_libraries(main ${OpenCV_LIBS})

Please let me know if this helps or you run into any issues.


1 Like

thanks for the suggestion! However, it does not work, it outputs the same thing as before.
But I suppose the problem might be also in the set (OpenCV_DIR) command. I followed an online tutorial for installation and setting up my CMakeLists.txt, but the path entered by him in the command does not exist in my installation folder (OpenCV_installation/installation/OpenCV-master/lib/cmake/opencv4). The only lib folder I have does not contain a cmake folder.
So here’s my question: What should the directory I set here contain? .h, .hpp or .so files?

So by now, I have figured out what the problem was, even if I don’t understand why this solved it.
You just have to add following lines to CMakeLists.txt (before you call add_executable or target_link_libraries):

find_package(OpenCV REQUIRED)

Hope this helps.