简体   繁体   中英

Why does the compiler throw “undefined reference to…” error in this case?

I'm trying to run the code in CLion that worked perfectly in Visual Studio. The program has an external dependency - OpenCV 4.2.0 library. I've never worked wtih CMake before so i searched for the solution on how to link the library to the project. I am using MingW-w64 as my environment. CMake builds without a single warning, but when compiling the project it throws an error "undefined reference to (put whatever function from OpenCV here). I spent 5 hours trying to understand what's wrong, looking for solution on forums and, of course, here, but i've failed to find the root of the problem. I created another project just to simplify debugging. Both of the projects throw the same errors with the same CMakeLists.txt files. Here is my CMakeList.txt:

project(SOMETHINF)

set(CMAKE_CXX_STANDARD 20)
set(OpenCV_DIR "E:\\LIBS\\opencv\\build\\x64\\vc15\\lib")
find_package( OpenCV REQUIRED )

include_directories(${OpenCV_INCLUDE_DIRS})
message(${OpenCV_LIBS})
add_executable(SOMETHINF main.cpp)
target_link_libraries(SOMETHINF ${OpenCV_LIBS})

As you can see i put message() in there to see if it finds the files and it does:

opencv_calib3dopencv_coreopencv_dnnopencv_features2dopencv_flannopencv_gapiopencv_highguiopencv_imgcodecsopencv_imgprocopencv_mlopencv_objdetectopencv_photoopencv_stitchingopencv_videoopencv_videoioopencv_world
-- Configuring done
-- Generating done
-- Build files have been written to: E:/SOMETHINF/cmake-build-debug

[Finished]

Here is the code (there's other unnecessary stuff just because): 在此处输入图像描述

And what i get trying to compile it: 在此处输入图像描述

Has anyone faced something similar before?

Looks like you are using msvc libraries, but if you use MinGW compiler it will not work. You can read this link to understand why.

Providing the right libraries should fix your problem. Usually the owner of the project provide msvc 12/14/15/16 and mingw binaries. If you don't have them I found this unofficial build with a quick search.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM