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 )
Related
I want to link glfw and glew to my project for graphics programming.
Adding glfw was pretty straight forward, I followed the instructions on their website. Creating a window with glfw worked perfectly.
However, I can't see what's wrong with my CMakeLists.txt for adding GLEW. The program gives the error: "GL/glew.h: No such file or directory".
My CMakeLists.txt:
cmake_minimum_required( VERSION 3.5 )
project(Starting)
find_package( OpenGL REQUIRED )
set( GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE )
set( GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE )
set( GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE )
add_subdirectory( ${PROJECT_SOURCE_DIR}/GLEW/build/cmake )
add_subdirectory( ${PROJECT_SOURCE_DIR}/GLFW )
add_executable( Starting ${PROJECT_SOURCE_DIR}/src/main.cxx )
target_link_libraries( Starting glew32s glfw )
I've tried giving it the names GLEW, glew, glew32 instead but nothing changed.
The library is downloaded from here: https://github.com/Perlmint/glew-cmake
If it has any importance, this is the batch file with which I run my CMakeLists.txt (located in a build folder inside my project source directory):
#echo off
cmake -G"Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug ..
make all
Looking at OpenGL projects on github didn't help since almost all of them are using visual studio.
It would be great if someone could tell me what I got wrong.
While Julia's suggestion will likely work, there is a find script included with CMake for GLEW, assuming you are using a new enough version, so you should be using that instead of including paths manually. Just add the following:
find_package(GLEW 2.0 REQUIRED)
target_link_libraries(Starting GLEW::GLEW)
This will find GLEW on your system then both link with the necessary libraries and add the necessary include directories.
Your issue is you're forgetting to add the GLEW include directories to your project. You can use target_include_directories or include_directories, the only difference being where you put it in your CMakeLists.txt and the syntax.
I prefer target_include_directories so your CMakeLists.txt after adding it would look like this:
cmake_minimum_required( VERSION 3.5 )
project(Starting)
find_package( OpenGL REQUIRED )
set( GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE )
set( GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE )
set( GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE )
add_subdirectory( ${PROJECT_SOURCE_DIR}/GLEW/build/cmake )
add_subdirectory( ${PROJECT_SOURCE_DIR}/GLFW )
add_executable( Starting ${PROJECT_SOURCE_DIR}/src/main.cxx )
target_include_directories(Starting PRIVATE
${PROJECT_SOURCE_DIR}/GLEW/include
)
target_link_libraries( Starting glew32s glfw )
I have installed opencv2.4.9(With No CUDA) by unzip opencv2.4.9.zip in home.
Many successful codes are using this library.
Now I wanna rebuild opencv2.4.9(with CUDA) in another folder.
I don't wanna delete the previous folder because I don't wanna face any problem later on and make my older code can't function.
So, the question is how to change the name of the directory? Seems like we link the package with library in CMake like below:
include_directories( ${catkin_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} )
find_package( OpenCV REQUIRED )
find_package(catkin REQUIRED COMPONENTS cv_bridge image_transport
OpenCV roscpp rospy std_msgs )
the name of directory is just OpenCV.
So if I got more than one OpenCV library in home, how could I link them separately?
And how to make c++ link to the library if we could change the name?
add_executable(xxx src/xxx.cpp)
target_link_libraries(xxx ${catkin_LIBRARIES} ${OpenCV_LIBRARIES})
Libraries can be included and linked with include_directories() and link_directories(), like this:
cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
...
include_directories(${PROJECT_SOURCE_DIR})
include_directories(/path/to/opencv/OpenCV-2.4.1/include)
link_directories(/path/to/opencv/OpenCV-2.4.1/lib)
...
# Set the build type. Options are:
# Coverage : w/ debug symbols, w/o op ...
So you should not forget the linking in the CMakeLists.txt and maybe remove the standard linking to your current OpenCV libs.
I'm trying to use Clion IDE to compile a simple program using Qt library, but I can't figure out how to configure CMakeLists.txt file. (I'm not familiar with cmake and toolchain)
this is my current CMakeLists.txt file:
cmake_minimum_required(VERSION 3.2)
project(MyTest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(MyTest ${SOURCE_FILES})
# Define sources and executable
set(EXECUTABLE_NAME "MySFML")
add_executable(${EXECUTABLE_NAME} main.cpp)
# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()
It's configured to use SFML library with a "FindSFML.cmake" file and it works fine. (I have copied these files from some tutorial) now I want some help regarding proper CMakeLists.txt configuration to compile programs that are using Qt library (it's more helpful if the files and explanations are provided).
P.S: my current OS is manjaro 0.8.13 and all I could find was explaining configurations in windows environment so I was unable to implement those tutorials.
In addition to #tomvodi's answer, you can use a simpler syntax :
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui).
Then, you don't call qt5_use_modules but instead use the standard command to link :
target_link_libraries(MyTest Qt5::Core Qt5::Widgets Qt5::Gui)
Your CMake project file is missing the Qt packages. You have to add:
find_package( Qt5Core REQUIRED )
find_package( Qt5Widgets REQUIRED )
find_package( Qt5Gui REQUIRED )
and then
qt5_use_modules( MyTest Core Widgets Gui )
I use DCMTK in my application and for compilation use cmake file. cmake finds all libraries (at least headers, because in compiles source files to .o files) the only problem is that during linking it tries to find dynamic libraries for DCMTK. I compiled one as static, so I do not have .so files. As a result it gives me error :No rule to make target /usr/lib/libdcmdata.so, needed by dcm_seg. Stop.
I use Ubuntu 14.04 x64.
It confuses me pretty much. So, what's the problems?
cmake file:
cmake_minimum_required(VERSION 2.6)
project(dcm_segm)
set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs")
set(Boost_USE_STATIC_LIBS ON)
set(OpenCV_USE_STATIC_LIBS ON)
set(DCMTK_USE_STATIC_LIBS ON)
set(OpenCV_STATIC ON)
find_package( VTK REQUIRED )
find_package( OpenCV REQUIRED )
find_package( Boost COMPONENTS system filesystem REQUIRED )
find_package( DCMTK REQUIRED )
include(${VTK_USE_FILE} )
link_directories(${OpenCV_LIB_DIR})
add_executable(dcm_seg main.cpp DICOMin.cpp Ensemble.cpp Ensemble3dExtension.cpp point_3d.cpp RegionGrow.cpp)
target_link_libraries(dcm_seg ${VTK_LIBRARIES} ${OpenCV_LIBS} ${DCMTK_LIBRARIES} ${Boost_LIBRARIES})
Can you check the content of ${DCMTK_LIBRARIES} (it should be a list of paths to DCMTK static libraries) ?
you can also check the following CMake entries during the CMake configuration:
DCMTK_DIR /path/to/DCMTK/install
DCMTK_config_INCLUDE_DIR /path/to/DCMTK/install/include/dcmtk/config
DCMTK_dcmdata_INCLUDE_DIR /path/to/DCMTK/install/dcmdata/include/dcmtk/dcmdata
DCMTK_dcmdata_LIBRARY_DEBUG /path/to/DCMTK/install/dcmdata/libsrc/libdcmdata.a
DCMTK_dcmdata_LIBRARY_RELEASE /path/to/DCMTK/install/dcmdata/libsrc/libdcmdata.a
[...]
Another hint: I noted in the past that find DCMTK from a build instead of an install not always works properly.
If you have trouble finding DCMTK with the script provided with CMake
(${DCMTK_LIBRARIES} doesn not content the path to you static DCMTK libs for example) you can try to use this alternative script
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.