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 )
Related
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 ####################################
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 )
I wanted to use boost::filesystem for my C++ code so I decided to add boost libraries into my CMakeLists.txt but I keep getting this message and it's not working right.
ipo: warning #11012: unable to find #loader_path/libboost_system-mt.dylib
Here is my CMakeLists.txt file.
cmake_minimum_required ( VERSION 3.10 )
set ( CMAKE_CXX_COMPILER icpc )
set ( CMAKE_CXX_STANDARD 17 )
set ( CMAKE_CXX_FLAGS "-fast -qopenmp -Wall" )
project( FLattice CXX )
# Add include files (-I option)
include_directories ( ${PROJECT_SOURCE_DIR}/include )
include_directories ( /opt/fftw/include )
# Specify the Library directory (-L option)
link_directories ( /opt/fftw/lib )
# Add executing files
file ( GLOB lib_codes ${PROJECT_SOURCE_DIR}/lib/*.cpp )
add_executable ( ${PROJECT_NAME} main.cpp ${lib_codes} )
# Boost
set(boost_min_ver 1.69.0)
set(boost_libs system filesystem)
find_package(Boost ${boost_min_ver})
if(Boost_FOUND)
find_package(Boost ${boost_min_ver} COMPONENTS ${boost_libs})
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
endif()
# Link external libraries
target_link_libraries ( ${PROJECT_NAME} fftw3 )
# Link project "library" when compile FLattice
# target_link_libraries ( FLattice library_code )
# Add sub-directory
# add_subdirectory ( lib )
I installed Boost by brew install boost and I simply just added the #Boost part to my original CMakeLists.txt file.
What am I doing wrong? Any thoughts?
update Seems like the program is working fine (I thought it wasn't working, but it was). However, I still get the same warning message.
ipo: warning #11012: unable to find #loader_path/libboost_system-mt.dylib
I started to think that this warning is an Intel issue and not a boost issue as in https://software.intel.com/en-us/forums/intel-c-compiler/topic/518493.
Perhaps I simply can't find it, but I want to add some code to a project of mine (libunwind found here http://www.nongnu.org/libunwind/download.html)
This library does not come with a CMakeLists.txt file and when I try to include it cmake complains about this fact. Right now I've simply added the libunwind directory to my external code and added a reference in my main CMakeLists.txt
Any input would be great.
Dealing with libraries there are 2 options for you :
If you've downloaded and was able to build and install it you can try to find it later on inside you CMAKE like this ( in case of Boost ) and link to your target:
find_package( Boost COMPONENTS date_time system serialization thread program_options filesystem unit_test_framework regex chrono REQUIRED )
if( NOT Boost_FOUND )
message( FATAL_ERROR "Cannot find boost!" )
endif( NOT Boost_FOUND )
message(STATUS "boost found")
include_directories( ${Boost_INCLUDE_DIRS} )
link_directories( ${Boost_LIBRARY_DIRS} )
target_link_libraries(YOUR_TARGET_NAME ${Boost_LIBRARIES})
2. You can add external library sources as a stand-alone target and use smth like this for CMake to build it :
set (sources
async_waiter.h
async_waiter_impl.h
async_waiter_impl.cpp
)
add_library( async_waiter ${sources} )
and later on link you target to it with :
target_link_libraries(YOUR_TARGET_NAME async_waiter)
If you want to build it each time along with your project, the easiest way would be to:
Add the source code somewhere into your project tree
Add a custom CMake target which should run before the compilation starts
In that custom target, run whatever is needed to compile the library (in your case it's ./configure -> make -> make install.
However that is rarely needed and most of the times you should just build the library once and link it as any other external library.
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.