Cmake OpenCV Can not specify link libraries - c++

i wanted to run this c++/OpenCV code that i found online:
http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html
I have a problem with CMake, it returned this error:
CMake Error at CMakeLists.txt:5 (target_link_libraries):
Cannot specify link libraries for target "SURF_Homography" which is not
built by this project.
I searched on the web the solution, and i found this
CMake OpenCV Cannot Specify Link Libraries
but i can't understand why mine don't work because it looks like the same, this is my CMakeLists file:
cmake_minimum_required(VERSION 2.8)
project( SURF_Homography )
find_package( OpenCV REQUIRED )
add_executable( SURF_Homography.cpp SURF_Homography.cpp )
target_link_libraries( SURF_Homography ${OpenCV_LIBS} )
hope you can help me
thanks
Luca

Change the add_executable line:
add_executable( SURF_Homography SURF_Homography.cpp )
Otherwise, the target you are specifying in the target_link_libraries line won't exist :)

Related

Use CGAL and PCL in same project

I am trying to use CGAL and PCL in the same project. Both PCL and CGAL should be installed correctly on my computer, since the examples work.
I created a CMakeList.txt file which references both PCL and CGAL I am able to configure it without the CMake GUI showing any problems, but when I open the project .sln the CGAL includes have errors
for example: Cannot open include file: 'CGAL/Simple_cartesian.h': No such file or directory
All the PCL includes work fine.
If I delete all the PCL references from the CMakeList.txt then the CGAL includes work. I am suspecting that I am doing something wrong in my CMakeList.txt.
Does anyone know what I am doing wrong?
Thank you!
Below is my CMakeList.txt
cmake_minimum_required(VERSION 3.1...3.15)
project(PC_Svr2)
find_package(CGAL QUIET)
if ( CGAL_FOUND )
# create a target per cppfile
file(GLOB cppfiles RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
foreach(cppfile ${cppfiles})
create_single_source_cgal_program( "${cppfile}" )
endforeach()
else()
message(STATUS "This program requires the CGAL library, and will not be compiled.")
endif()
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable (PC_Svr2 cloud_viewer.cpp)
target_link_libraries (PC_Svr2 ${PCL_LIBRARIES})
message("PCL_IO_LIBRARIES - ${PCL_IO_LIBRARIES}")
file(GLOB cppfiles RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
foreach(cppfile ${cppfiles})
create_single_source_cgal_program( "${cppfile}" )
endforeach()
That is creating a single target from each of the cpp files in CMAKE_CURRENT_SOURCE_DIR. I don't think that is what you want.
If I understand things correctly, your code is in cloud_viewer.cpp, then you should remove the loop and just link with cgal with your target_link_libraries:
target_link_libraries(PCS_Svr2 CGAL::CGAL ${PCL_LIBRARIES})
You can have a look at that page for details.

C++ OpenCV include error 'file not found'

I have a problem using OpenCV 4.1.2 in C++. I have this CMakelists.txt:
cmake_minimum_required(VERSION 2.8.12)
project( Barcode-cpp )
find_package( OpenCV REQUIRED )
add_compile_options(-std=c++11)
add_library( src
src/VideoVeed.h
src/VideoVeed.cpp
)
add_executable( program
program/main.cpp
)
target_link_libraries( program
src
${OpenCV_LIBS}
)
As you can see I have two folders with source code:
program contains main.cpp
src contains VideoVeed.h & VideoVeed.cpp
When I include OpenCV in main.cpp like this: <opencv2/opencv.hpp>, it works fine. But when I include OpenCV (the same way) it gives the error fatal error: 'opencv2/opencv.hpp' file not found.
I think I'm doing something wrong in my CMakelists.txt, but I can't figure out what exactly.
I hope someone is able to help me. Thanks in advance!
You should add the line, target_include_directories(), so that the OpenCV include directories are included in your executable:
add_executable( program
program/main.cpp
)
target_include_directories(program PRIVATE ${OpenCV_INCLUDE_DIR})
Depending on the version of OpenCV you are using, you may need
to use OpenCV_INCLUDE_DIRS instead:
add_executable( program
program/main.cpp
)
target_include_directories(program PRIVATE ${OpenCV_INCLUDE_DIRS})
EDIT: OpenCV 4.1.2 populates the variable OpenCV_INCLUDE_DIRS, so this is the variable you should use. See this tutorial.

CMake not wanting to link with libGL.so

I was coding in KDevelop, ant I set up my CMakeLists.txt to include and link against libGL and freeglut.
However, it gave me the following error:
make[2]: *** No rule to make target '/usr/lib/libGL.so', needed by 'opengl'. Stop.
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(opengl)
add_executable(opengl main.cpp)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories( ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} )
target_link_libraries(opengl ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )
First, what I am about to suggest might depend on CMake version, but I don't think (I'm using cmake version 3.1.0).
Looking at the installed FindOpenGL.cmake file, it looks like the variable you want in target_link_libraries is ${OPENGL_glu_LIBRARY}. Case does matter.
The best way to test an issue like this one is to use message("var: ${var}"), then you would be able to clearly see what your variable is set to during configuration.
Hope this helps!
FYI, I found the find module in the ${cmake_install_dir}/Modules/Find* where cmake_install_dir=/usr/share/cmake on my linux machine.
EDIT**
Sorry, was tired this morning and missed that you wanted GLUT, not GLU. There is a FindGLUT.cmake Module. You should call find_package(GLUT REQUIRED) and use ${GLUT_LIBRARIES} and ${GLUT_INCLUDE_DIR}. Sorry about the confusion!

find-package to a development library using CMAKE

This is a total cmake noob question I'm sure.
I'm working on an OpenCV project and wish to test something using the latest beta release. How can I specify the beta libraries without installing them onto my system? My beta opencv has been successfully built in:
/Users/paul/hacking/robotics/opencv/build/lib/
My current cmake has basically been lifted from the opencv samples and looks like this:
# cmake for Stereo Vision App
# your opencv/build directory should be in your system PATH
# set minimum required version for cmake
cmake_minimum_required(VERSION 2.8)
# define the project name
set(project_name "Stereo")
# set the project namee
project("${project_name}")
# add opencv package to the project
find_package( OpenCV REQUIRED )
MESSAGE("OpenCV version : ${OpenCV_VERSION}")
# add opencv include directories to the project
include_directories( ${OpenCV_INCLUDE_DIRS} )
# add include directory
include_directories (${Stereo_SOURCE_DIR})
# add library
add_library( CameraCalibrator CameraCalibrator.cpp)
# add executable
#add_executable( videoprocessing videoprocessing.cpp)
#add_executable( tracking tracking.cpp)
#add_executable( foreground foreground.cpp)
add_executable( calibrate calibrate.cpp)
add_executable( rightsideup rightsideup.cpp)
add_executable( live live.cpp)
add_executable( live2 live2.cpp)
add_executable( stereo-tune stereo-tune.cpp)
add_executable( project project.cpp)
add_executable( stereo_calibrate stereo_calibrate.cpp)
add_executable( capture_two_camera_chessboards capture_two_camera_chessboards.cpp)
add_executable( capture_stereo_chessboards capture_stereo_chessboards.cpp)
# link libraries
#target_link_libraries( videoprocessing ${OpenCV_LIBS})
#target_link_libraries( tracking ${OpenCV_LIBS})
#target_link_libraries( foreground ${OpenCV_LIBS})
target_link_libraries( rightsideup ${OpenCV_LIBS})
target_link_libraries( live ${OpenCV_LIBS})
target_link_libraries( live2 ${OpenCV_LIBS})
target_link_libraries( stereo-tune ${OpenCV_LIBS})
target_link_libraries( project ${OpenCV_LIBS})
target_link_libraries( stereo_calibrate ${OpenCV_LIBS})
target_link_libraries( capture_two_camera_chessboards ${OpenCV_LIBS})
target_link_libraries( capture_stereo_chessboards ${OpenCV_LIBS})
target_link_libraries( calibrate ${OpenCV_LIBS} CameraCalibrator)
I've tried using link_libraries and setting the version on the find package line to 3.0, however it always finds the system installed libraries 2.4.10
Edit 1:
cmake -DPCL_DIR:PATH="../../pcl/build" -DOpenCV_DIR:PATH="../../opencv/build" ..
Is not working for me for some reason. Likewise when I try to set these variables inside the CMake script it also does not work.
set(PCL_DIR "../../pcl/build" CACHE PATH "")
set(OpenCV_DIR "../../opencv/build" CACHE PATH "")
Many thanks!
Paul
You're going in the right direction. cmake's Find scripts look in the standard system paths first unless told otherwise. Each FIND script has its own set of cmake variables which you can set to alter the behaviour.
For FindOpenCV.cmake it seems to be OPENCV_BASE_DIR.
Here's a link to some source code:
https://github.com/rpavlik/cmake-modules/blob/master/FindOpenCV.cmake
Edit:
I don't fully understand, but the following seems to work with absolute paths
set(OpenCV_DIR "/Users/paul/hacking/robotics/opencv/build" CACHE PATH "")
set(PCL_DIR "/Users/paul/hacking/robotics/pcl/build" CACHE PATH "")
set(VTK_DIR "/Users/paul/hacking/robotics/VTK/build" CACHE PATH "")
At least less things to go wrong if I get the command line wrong, I no longer need to remember to do the following:
OpenCV_DIR=../../opencv/build cmake ..
In the PCL documentation it says to use:
set(PCL_DIR "/path/to/PCLConfig.cmake")
But this doesn't work for me.

CMake OpenCV Cannot Specify Link Libraries

I am trying to run an OpenCV program written in C++ on Ubuntu. I followed this tutorial to install OpenCV on my system.
I then followed this tutorial to run my code with the following Cmake commands as specified in the tutorial:
cmake_minimum_required(VERSION 2.8)
project( PedestrianDetection )
find_package( OpenCV REQUIRED )
add_executable( PedestrianDetection PedestrianDetection.cpp )
target_link_libraries( ${OpenCV_LIBS} )
However, Cmake gives me the following output:
CMake Error at CMakeLists.txt:5 (target_link_libraries):
Cannot specify link libraries for target "opencv_videostab" which is not
built by this project.
Can someone point me in the right direction to link the libraries?
By the way, I am using OpenCV2.4.8
from the documentation
target_link_libraries: Link a target to given libraries.
target_link_libraries(<target> [item1 [item2 [...]]]
[[debug|optimized|general] <item>] ...)
Specify libraries or flags to use when linking a given target. The
named must have been created in the current directory by a
command such as add_executable or add_library. The remaining arguments
specify library names or flags.
try instead
target_link_libraries(PedestrianDetection ${OpenCV_LIBS})