find-package to a development library using CMAKE - c++

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.

Related

OpenCV c++: Undefined symbols for architecture arm64: [duplicate]

I consider this a fundamental step for creating projects that use OpenCV libraries so you don't need to manually include all the libraries. There is not detailed information on this topic, at least for a newbie that just wants to use OpenCV as soon as posible, so:
Which is the easiest and scalable way to create a multiplatform c++ OpenCV with Cmake?
First: create a folder Project containing two subfolders src and include, and a file called CMakeLists.txt.
Second: Put your cpp inside the src folder and your headers in the include folders.
Third: Your CMakeLists.txt should look like this:
cmake_minimum_required(VERSION 2.8)
PROJECT (name)
find_package(OpenCV REQUIRED )
set( NAME_SRC
src/main.cpp
)
set( NAME_HEADERS
include/header.h
)
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
link_directories( ${CMAKE_BINARY_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable( name ${NAME_SRC} ${NAME_HEADERS} )
target_link_libraries( sample_pcTest ${OpenCV_LIBS} )
Fourth: Open CMake GUI and select the root folder as input and create a build folder for the output. Click configure, then generate, and choose the generator (VisualStudio, Eclipse, ...)
I am using opencv3.0 and cmake3.8,
config below work for me!
######## A simple cmakelists.txt file for OpenCV() #############
cmake_minimum_required(VERSION 2.8)
PROJECT(word)
FIND_PACKAGE( OpenCV REQUIRED )
INCLUDE_DIRECTORIES( ${OpenCV_INCLUDE_DIRS} )
ADD_EXECUTABLE(word main.c)
TARGET_LINK_LIBRARIES (word ${OpenCV_LIBS})
########### end ####################################

Encounter issue with `find_package` command in Visual Studio CMake

I'm operating under a new learning curve here with c++ and using CMake in Visual Studio. Here is the partial code up until the point where I receive the error:
project(libfranka
VERSION 0.8.0
LANGUAGES CXX
)
list(INSERT CMAKE_MODULE_PATH 0 ${CMAKE_SOURCE_DIR}/cmake)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(MSVC)
add_compile_options(/W0)
else()
add_compile_options(-Wall -Wextra)
endif()
set(THIRDPARTY_SOURCES_DIR "${CMAKE_SOURCE_DIR}/3rdparty" CACHE PATH
"Directory for third-party sources")
## Dependencies
find_package(Poco REQUIRED COMPONENTS Net Foundation)
find_package(Eigen3 REQUIRED)
Once it hits the first find_package is where I encounter the error:
Here is the code within FindPoco.cmake.
find_package(Poco COMPONENTS ${Poco_FIND_COMPONENTS} CONFIG QUIET)
if(Poco_FOUND)
return()
endif()
find_path(Poco_INCLUDE_DIR Poco/Poco.h)
mark_as_advanced(FORCE Poco_INCLUDE_DIR)
foreach(component ${Poco_FIND_COMPONENTS})
set(component_var "Poco_${component}_LIBRARY")
find_library(${component_var} Poco${component})
mark_as_advanced(FORCE ${component_var})
if(${component_var})
set(Poco_${component}_FOUND TRUE)
list(APPEND Poco_LIBRARIES ${component})
if(NOT TARGET Poco::${component})
add_library(Poco::${component} SHARED IMPORTED)
set_target_properties(Poco::${component} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${Poco_INCLUDE_DIR}
IMPORTED_LOCATION ${${component_var}}
)
endif()
endif()
endforeach()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Poco
FOUND_VAR Poco_FOUND
REQUIRED_VARS Poco_INCLUDE_DIR Poco_LIBRARIES
VERSION_VAR Poco_VERSION
HANDLE_COMPONENTS
)
I installed poco using vcpkg in a directory titled vcpkg. Within the vcpkg directory is the libfranka directory, which houses the CMakeLists.txt file that I compile in Visual Studio. Here is an image of that directory:
Finally, here is the tutorial that I am using: https://frankaemika.github.io/docs/installation_windows.html#building-from-source
EDIT:
Per the link I followed the instructions for solving the build dependencies and here is an image of that:
Then I ran the CMakeLists.txt again and in the CMake Settings this is what I see:
Note also that I ran through the install of poco again and I noticed this and am unsure if it could be the source of the problem or if it means nothing (again, this was the out put after running vcpkg install poco):
After this I still receive the same error.
Does anyone see what it is that I am doing incorrectly?
Thank you!

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.

Building a MacOS executable for distribution using cmake including OpenCV

I have a cmakelists.txt file that compiles my MacOS application using OpenCV, C++, OpenGL, based on information I found on SO. When I try copying the folder of the executable to another Mac, I get an error due to missing OpenCV libs, expected in a different folder from my executable.
Is there a change I can make to the file so that it will include the OpenCV compiled code in the executable, or its folder for distribution? For what it's worth I am using CMake because I could not figure out a simpler way to get it to build.
cmake_minimum_required( VERSION 3.1 )
project( go)
set (CMAKE_CXX_STANDARD 11)
find_package( OpenCV REQUIRED )
find_library( COCOA_FW Cocoa )
find_library( OPENGL_FW OpenGL )
find_library( IOKIT_FW IOKit )
add_executable( go main.cpp gl_utils.cpp user_interface.cpp geometry_2d.cpp)
target_include_directories( go PRIVATE ${CMAKE_SOURCE_DIR}/include )
target_link_libraries( go ${OpenCV_LIBS} ${COCOA_FW} ${OPENGL_FW} ${IOKIT_FW} ${CMAKE_SOURCE_DIR}/common/libGLEW.a ${CMAKE_SOURCE_DIR}/common/libglfw3.a)
On another question related: Static OpenCV compile
set(OpenCV_STATIC ON)
find_package(OpenCV REQUIRED)
Now for the find_library( OPENGL_FW OpenGL ), you will need to be more explicit:
# This could change based on the OpenGL library you have installed.
find_library( OPENGL_FW libGL.a )

ROS with QtCreator: autocompletion

I'm using and like QtCreator to code and build my ROS projects written in c++.
Unfortunately the auto-completion for my own header files is not working: e.g. #include "LineTracker.hh"
Building the project works perfectly. And also the auto-completion for other external packages like ros or opencv is working.
Update 2.0: With QtCreator 3.6 the solution is not working
Update 1.0: Found a solution, see bottom!
Thats how my CMakeLists.txt looks:
cmake_minimum_required(VERSION 2.8.3)
project(line_tracking)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(catkin REQUIRED COMPONENTS
roscpp
tf
sensor_msgs
image_transport
cv_bridge
)
catkin_package()
include_directories(
include
${catkin_INCLUDE_DIRS}
${PROJECT_SOURCE_DIR}
)
add_executable(${PROJECT_NAME}
src/line_tracking.cpp
src/EDLineDetector.cpp
src/LineTracker.cpp
)
target_link_libraries(${PROJECT_NAME}
${catkin_LIBRARIES}
)
# --- QT CREATOR STUFF ---
#Add all files in subdirectories of the project in
# a dummy_target so qtcreator has access to all files
FILE(GLOB children ${CMAKE_SOURCE_DIR}/*)
FOREACH(child ${children})
IF(IS_DIRECTORY ${child})
file(GLOB_RECURSE dir_files "${child}/*")
LIST(APPEND extra_files ${dir_files})
ENDIF()
ENDFOREACH()
add_custom_target(dummy_${PROJECT_NAME} SOURCES ${extra_files})
#
The file/package structure looks standard like this:
CMakeLists.txt
|
+ -- src
|
+ -- include
How do I have to adapt my CMakeLists.txt that QtCreator finds my headers for autocompletion?
Thank you very much for your help!
Sidenote:
When I use the top CMakeLists.txt file of the catkin workspace in QtCeator and I include the header files under their package path like this: #include <packageName/include/headerFile.h> the auto-completion is working but the build is not working anymore. So this is only a bad and not userfriendly hack to get auto-completion during coding.
Update 1.0:
I found a solution which is working:
I create a library from all the (class) files which have header files, and link the library to the main file, instead of adding the files as executables. I posted it here as answer.
But I don't know why it is working like this and not without the way over the library. Any explanations?
Update 2.0:
I just upgraded to QtCreator 3.6 and there my solution with the library in not working anymore.
Does anybody know another solution?!
Update: This solution does NOT work with QtCreator 3.6
I found a solution to my own question. Not so much fun, but anyway, I spent a lot of time with that issue so here the solution, which is hopefully useful for others:
Auto-completion with CMakeLists.txt in QtCreator 3.5.1 for your own classes:
Create a library with all your classes: ADD_LIBRARY(myFilesLib src/class1.cpp ...)
Add your executable (main function): add_executable(${PROJECT_NAME} src/main.cpp)
Link your library to your executable: target_link_libraries(${PROJECT_NAME} myFilesLib)
With this way over the library, auto-completion is working in QtCreator!
For ROS (catkin) don't forget to link the ${catkin_LIBRARIES}.
Here the whole CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.3)
project(example_project)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(catkin REQUIRED COMPONENTS
roscpp
)
catkin_package()
include_directories(
include
${catkin_INCLUDE_DIRS}
${PROJECT_SOURCE_DIR}
)
# Create a library with all your classes
add_library(myFilesLib
src/class1.cpp
src/class2.cpp
src/class3.cpp
)
target_link_libraries(myFilesLib
${catkin_LIBRARIES}
)
# add your executable
add_executable(${PROJECT_NAME}
src/main.cpp
)
# link the library with your classes to the executable
target_link_libraries(${PROJECT_NAME}
${catkin_LIBRARIES}
myFilesLib
)
I don't know why it is working only with the way over the library but it is working. Maybe somebody has an explanation?!
This is really annoying. Finally I figured out a solution, that works at Qt Creator 4.1.0, and probably works at other versions.
Write your include_directories correctly, so that compilation is OK.
Below the include_directories, add the following line. Note that the sequence matters, the following line must locate below the include_directories.
file(GLOB_RECURSE HEADERS */*.hpp */*.h)
Add ${HEADERS} # for qtcreator... in one of your add_executable. like:
add_executable(slam_pp_node
${HEADERS} # for qtcreator...
src/***.cpp
)
The above should be enough to solve the autocompletion problem. If not, add the following lines at the end of your CMakeList.txt:
file(GLOB_RECURSE EXTRA_FILES */*)
add_custom_target(${PROJECT_NAME}_OTHER_FILES ALL WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} SOURCES ${EXTRA_FILES})
Good luck!