Linking with prebuilt opencv gives errors

target_link_libraries(my_app lib_opencv)

I get multiple errors like following

undefined reference to `cv::dnn::dnn4_v20200609::Net::Net()'
undefined reference to `cv::dnn::dnn4_v20200609::Net::getLayerNames[abi:cxx11]() const'

please edit and add some more information, like os, compiler, opencv version, and the whole cmakelists.txt you’re using to build your app, thank you !

please also compare to the cmake example

Thanks, I have added full details but the forum did not allow me to post the full message.

OS: Linux Ubuntu 18.04

I am building opencv 5.4.1

wget latest opencv
wget latest opencv contrib

so in a folder we must have opencv and opencv_contrib folders

cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=$cwd/install -DINSTALL_C_EXAMPLES=ON -DINSTALL_PYTHON_EXAMPLES=OFF -DWITH_TBB=OFF -DWITH_V4L=OFF -DWITH_QT=OFF -DWITH_OPENGL=ON -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules -DBUILD_EXAMPLES=OFF -DBUILD_opencv_world=ON -DOPENCV_DNN_OPENCL=ON ..

Once I have libopencv_world.so, I am linking in my cmakelists.txt file as

set(OPENCV_PATH ${LIB_DIR}/opencv)
add_library(lib_opencv SHARED IMPORTED)

# sets the location of the prebuilt opencv .so
set_target_properties( lib_opencv
        PROPERTIES IMPORTED_LOCATION
        ${LIB_DIR}/opencv/lib/libopencv_world.so )

include_directories(${LIB_DIR}/opencv/include)

target_link_libraries(smaple lib_opencv)

I get multiple errors like following

undefined reference to `cv::dnn::dnn4_v20200609::Net::Net()'
undefined reference to `cv::dnn::dnn4_v20200609::Net::getLayerNames[abi:cxx11]() const' ```

disable opencv_world

I did try without opencv_world but still face the same issue.
Isn’t the purpose of opencv_world is to combine all the modules?

use this sample
and if it’s you please close this issue

yes, sure. however, there are some known issues, when using the contrib modules, too, so, if you need the contrib stuff, rather try building single-module libs, not world

also, can it be, you still have a previous opencv version around ?

4.5.1 actually has this as version tag (in the namespace):

#define OPENCV_DNN_API_VERSION 20201117

while 20200609 (from error msg) would be 4.4.0

so, again, – can it be, you’re using (or cmake is finding) 4.4.0 includes, but 4.5.1 libs ?

(which would explain the mismatch)