'Can't bind OpenCV to my project via CMake in CLion

For an exercise at school I need to work with OpenCV, but I can't figure out how I can link this in CLion.

I have chosen to put the entire OpenCV library in my project so that I can easily zip it and share with classmates.

There are no errors in my code and autocomplete can find the files and I can use the content of those files (call functions and such), but when I want to compile, it fails and tells me it cant find the definition of those functions (LNK2019).

I have been trying this approach for several hours already, and have had this problem for about a week now, and what I could find on the internet is not what I am searching for.

My CMakeLists.txt:

cmake_minimum_required(VERSION 3.22)
project(opencv_template)

set(CMAKE_CXX_STANDARD 14)

add_executable(opencv_template main.cpp)

include_directories(lib/opencv/build/include)

set(MODULE_PATH lib/opencv/sources/modules)

file(GLOB sources_list ${MODULE_PATH}/*)
foreach(dir ${sources_list})
    if(IS_DIRECTORY ${dir})
        message(STATUS "path=${dir}")
        include_directories(${dir}/include)
        link_directories(${dir}/src)
    endif()
endforeach()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source