Ask Your Question

Linking openCV library in CMake ubuntu

asked 2018-11-30 00:56:11 -0500

blessme29 gravatar image

updated 2018-11-30 01:04:31 -0500

berak gravatar image

Hello, I am trying to link the octomap library with openCV library, but I have problem when I tried to generate the file using cmake. I confused about this problem, this is the error when I "make" my project file :

[ 47%] Linking CXX executable ../../../bin/simple_example
CMakeFiles/simple_example.dir/simple_example.cpp.o: In function `cv::Mat::~Mat()':
simple_example.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x15): undefined reference to `cv::Mat::deallocate()'
simple_example.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x71): undefined reference to `cv::fastFree(void*)'
collect2: error: ld returned 1 exit status
octomap/src/CMakeFiles/simple_example.dir/build.make:96: recipe for target '../bin/simple_example' failed
make[2]: *** [../bin/simple_example] Error 1
CMakeFiles/Makefile2:1450: recipe for target 'octomap/src/CMakeFiles/simple_example.dir/all' failed
make[1]: *** [octomap/src/CMakeFiles/simple_example.dir/all] Error 2
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2

And this is my CMakeList.txt file :

PROJECT( octomap-distribution )

find_package( OpenCV REQUIRED )
set(OpenCV_DIR "/usr/local/include/opencv")

ENABLE_TESTING()  # enable CTest environment of subprojects
set(CMAKE_POSITION_INDEPENDENT_CODE ON)  # enables -fPIC in applicable compilers (required to avoid link errors in some cases)

option(BUILD_OCTOVIS_SUBPROJECT "Build targets from subproject octovis" ON)
option(BUILD_DYNAMICETD3D_SUBPROJECT  "Build targets from subproject dynamicEDT3D" ON)
option(OCTOVIS_QT5 "Link Octovis against Qt5?" NO)

    # Compiling against QT5 requires C++11.


    ADD_SUBDIRECTORY( octovis )


I already try to link the openCV library into the cmakelist file but it totally failed, Is there anyone had experience solve this problem?? I am using opencv 3.3 and ubuntu 16.04

Thank you very much

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2018-11-30 01:08:45 -0500

berak gravatar image

have a look here , you never link the opencv libraries. it needs an additional line like:

target_link_libraries(octovis ${OpenCV_LIBS})

(replace octovis with whichever of your projects there uses opencv)

edit flag offensive delete link more


Hello, thanks for replying I have tried to add the line but it still give me this following error :

user@pc:~/octomap/build$ cmake ..
CMake Error at CMakeLists.txt:8 (target_link_libraries):
  Cannot specify link libraries for target "simple_example" which is not
  built by this project.

-- Configuring incomplete, errors occurred!
See also "/home/ferdyan/octomap/build/CMakeFiles/CMakeOutput.log".

Actually my file is simple_example.cpp and if I build it using cmake it will create simple_example executable file. I want to add opencv function in the simple_example.cpp and build it, but it failed when I build it. The original CMakeList.txt file is this file : I have tried try much way but it failed. Do

blessme29 gravatar imageblessme29 ( 2018-11-30 01:23:33 -0500 )edit

indeed, you cmake file never mentions simple_example

berak gravatar imageberak ( 2018-11-30 01:26:14 -0500 )edit

oh right, I just realize there is another CMakeList.txt file in the octomap sub folder, I will try it link the opencv library in the another CMakeList.txt file.

blessme29 gravatar imageblessme29 ( 2018-11-30 01:45:12 -0500 )edit

Hi, thanks for your answer, now I successfully connected the octomap with the openCV library, there are another cmakelist file in my project file Appreciate it, thank you very much

blessme29 gravatar imageblessme29 ( 2018-11-30 09:56:58 -0500 )edit

oh, cool ;)

berak gravatar imageberak ( 2018-11-30 10:07:20 -0500 )edit

Thx that's help me a lot

Aurelien_TT gravatar imageAurelien_TT ( 2018-12-26 07:23:24 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-11-30 00:56:11 -0500

Seen: 347 times

Last updated: Nov 30 '18