CMake finding incorrect version of OpenCV - macos

I have multiple versions of OpenCV installed, and CMake is finding the wrong one. I have installed opencv and opencv3 using brew, and they exist in the following paths:
/usr/local/opt/opencv3
/usr/local/opt/opencv
My CMakeLists.txt looks like the following:
cmake_minimum_required(VERSION 2.8)
set(ENV{OpenCV_DIR} "/usr/local/opt/opencv3")
project(TestProject)
#OpenCV
find_package( OpenCV 3 REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
MESSAGE ( STATUS "Found OpenCV: ${OpenCV_VERSION}" )
MESSAGE ( STATUS "OpenCV_INCLUDE_DIRS= ${OpenCV_INCLUDE_DIRS}" )
MESSAGE ( STATUS "OpenCV_DIR= $ENV{OpenCV_DIR}" )
I am using OpenCV_DIR to point to the location of the OpenCV that I would like to use (in this example it's set immediately before just to be 100% certain that this variable points to the right place).
My output is this:
-- Found OpenCV: 3.0.0
-- OpenCV_INCLUDE_DIRS= /usr/local/include/opencv
-- OpenCV_DIR= /usr/local/opt/opencv3
So, it's finding the right version of OpenCV (3.0.0), but the include path is set to some other opencv, in this case /usr/local/include/opencv points to /usr/local/opt/opencv, which is the 2.4.8 version.
As a result, none of my programs find the right files to include! Does anyone know how to tell CMake which version to look for, if the OpenCV_DIR environment variable does not seem to work?

You can explicitly assign the path in find_package. For example,
find_package( OpenCV 3 REQUIRED PATHS "/usr/local/opt/opencv3" )

As indicated OP is using cMake V2.8. Therefore the following applies to include your desired directory.
#OpenCV
find_package( OpenCV 3 REQUIRED )
include_directories([BEFORE] "/usr/local/opt/opencv3") # your cv2 desired folder
# stated here.
The [BEFORE] argument informs cMake about this particular folder to use first rather than the other CV folder.
For cMake higher than v2.8 you should not use the include_directories but rather the following:
target_include_directories(test PRIVATE ${YOUR_DIRECTORY})

Related

Cmake finds Eigen library but does not mark with found

I found a strange behavior with cmake. On my computer I have two versions of Eigen:
installed one via ubuntu repo (version 3.2.2)
downloaded one (version 3.3.3).
I have added the location of the 2. Eigen library in $PATH.
In the CMakeFiles.txt I write
find_package(Eigen3 3.3.3 REQUIRED)
if (NOT Eigen3_FOUND)
MESSAGE( STATUS "Eigen not found.")
endif(NOT Eigen3_FOUND)
MESSAGE( STATUS "EIGEN_DIR: " ${Eigen3_INCLUDE_DIR})
but it outputs the following:
-- Found Eigen3: /home/armena/armena/eigen3 (Required is at least version "3.3.3")
-- Eigen not found.
-- EIGEN_DIR:
From what I understand it finds the library but it is not able to return its location. Any idea how to fix this? Thanks
The problem is that Eigen's package config file does not follow CMake's recommended naming guidelines; all of its variables are prefixed with EIGEN3_, not with Eigen3_. If you change your CMakeList like this, it should work:
if (NOT EIGEN3_FOUND)
message( STATUS "Eigen not found.")
endif()
message( STATUS "EIGEN_DIR: " ${EIGEN3_INCLUDE_DIR})

ROS Kinetic Opencv3 Cmake error

I try to use opencv3 with ROS kinetic binary packages but when I use catkin_make I have an error that I can't fix !
Result of rospack find opencv3 : /opt/ros/kinetic/share/opencv3
Error: CMake Warning at /opt/ros/kinetic/share/catkin/cmake/catkinConfig.cmake:76 (find_package):
Could not find a package configuration file provided by "opencv3" with any
of the following names:
opencv3Config.cmake
opencv3-config.cmake
Add the installation prefix of "opencv3" to CMAKE_PREFIX_PATH or set
"opencv3_DIR" to a directory containing one of the above files. If
"opencv3" provides a separate development package or SDK, be sure it has
been installed.
Thanks for your help
If you want to use OpenCV 3 in ROS Kinetic, you only need to do the following in your CMakeLists.txt: find_package(OpenCV REQUIRED), because OpenCV 3 is the default in ROS Kinetic. Pay attention to the capitalization, it must be OpenCV (i.e. find_package(opencv) is not going to work!)
References: Section 1.2 and 2 at http://wiki.ros.org/opencv3
I used this post for opencv2 :
[ROS hydro opencv2 linking error during 'catkin_make'
I put in Cmake:
find_package(OpenCV)
include_directories(${OpenCV_INCLUDE_DIRS})

How to use CMake to include a library, then include its headers using angled brackets in source file?

I used homebrew to install opencv and openni and nite.
and I've managed to make a cmake file for opencv, and somehow it already had a defined path for its directory OpenCV_LIBS, not sure how or where I find out about that.
cmake_minimum_required(VERSION 2.8)
project( main )
find_package( OpenCV REQUIRED )
add_executable( main main.cpp )
target_link_libraries( main ${OpenCV_LIBS} )
After installing toktakke's build openni and nite.
I gave this a shot, and extended to the following, and again I'm not sure where I'd find these aliases to their directories.
cmake_minimum_required(VERSION 2.8)
project( main )
find_package( OpenCV REQUIRED )
include_directories(${OPENNI_INCLUDE})
Include_directories(${NITE_INCLUDE})
add_executable( main main.cpp)
target_link_libraries( main ${OpenCV_LIBS} ${OPENNI_LIB} ${NITE_LIB} )
but it wasn't integrated well
in .bash_profile I have the following:
export OPENNI_INCLUDE="/usr/local/Cellar/openni/1.5.7.10/include/ni"
export OPENNI_LIB="/usr/local/Cellar/openni/1.5.7.10/lib"
export OPENNI_DIR="/usr/local/Cellar/openni/1.5.7.10"
export NITE_INCLUDE="/usr/local/Cellar/nite/1.5.2.21/include/nite"
export NITE_LIB="/usr/local/Cellar/nite/1.5.2.21/lib"
export NITE_DIR="/usr/local/Cellar/nite/1.5.2.21"
now I get the following error:
fatal error: 'XnOpenNI.h' file not found
#include <XnOpenNI.h>
You should add the
include_directories($ENV{OPENNI_INCLUDE})
after your project definition. This will add the path to the OPENNI include files as an -I option to the compiler. More about the 'include_directories' command is here.
Regarding the origin of OpenCV_LIBS:
When the 'find_package( OpenCV REQUIRED )' is used, cmake will automatically call the FindOpenCV.cmake (found in the path specified by CMAKE_MODULE_PATH variable). The FindOpenCV.cmake is responsible for actually locate the OpenCV libraries and include files and define both the OpenCV_LIBS and bunch of other variables.
This makes it "magically" available for you. More info about find_package magic could be found in the CMake docs

CMake: wrong zlib found - how to build zlib from src (with main project CMakeLists.txt) and link it?

// brief version
How can I make CMake to use my supplied zlib (which it also has to build from source) instead of the one found by the finder without breaking the finder for other libs (OpenGL)?
ZLib needs to be used by the main project and also libPNG which comes as source as well.
Primary target platform is Windows.
// longer version:
In my project I need to link against libpng, zlib and OpenGL. With libpng being dependent on zlib. But zlib is also required by the main project.
I need to supply sourcecode for all libs except OpenGL, and build those libraries along with
the main project to assert linking the correct version and simplify building on Windows.
I found ways to do all this with custom libraries where no built-in finder exists, but I can't override the finder properly for just zlib. If I change the search path for libs, then OpenGL is not found.
However I can't get cmake to use my supplied zlib instead of a rouge zlib.DLL that the package finder finds somewhere in my system. (The one from tortoise git)
I tried to set ZLIB_LIBRARY to a specific filepath, but that only works on MinGW, and I also think this is not the way to do it.
(And also I had to explicitly link to png16_static instead of just libpng, for an inexplicable reason.)
Any help on this is much appreciated. Maybe I'm taking this on the wrong way?
Target&Development Platform:
Windows7
Visual Studio 2010
and MinGW (both need to work)
My (simplified example) CMakeLists.txt:
cmake_minimum_required (VERSION 2.6)
project (MyProject)
find_package(OpenGL)
add_executable(MyProject main.cpp)
include_directories(${INCLUDE_DIRECTORIES} "${PROJECT_BINARY_DIR}")
include_directories(${INCLUDE_DIRECTORIES} "external_libs/lpng162")
include_directories(${INCLUDE_DIRECTORIES} "external_libs/zlib-1.2.8")
include_directories(${INCLUDE_DIRECTORIES} "${PROJECT_BINARY_DIR}/external_libs/zlib-1.2.8")
add_subdirectory("external_libs/zlib-1.2.8")
link_directories(${LINK_DIRECTORIES} "${PROJECT_BINARY_DIR}/external_libs/zlib-1.2.8")
# libpng will not build correctly if this not set
set (ZLIB_ROOT "${PROJECT_SOURCE_DIR}/external_libs/zlib-1.2.8")
# manually set this to prevent cmake from finding the tortiose-git zlib.dll first
# DOES NOT WORK CORRECTLY, only with mingw32
set (ZLIB_LIBRARY "${PROJECT_BINARY_DIR}/external_libs/zlib-1.2.8/libzlib.dll")
add_subdirectory("external_libs/lpng162")
TARGET_LINK_LIBRARIES(MyProject png16_static zlib ${OPENGL_LIBRARY})
Project (simplified example) structure:
./main.cpp
./CMakeLists.txt
./external_libs/zlib-1.2.8/ <- contains respective source
./external_libs/lpng162/ <- contains respective source
Third-party libraries most likely call FindZLIB.cmake to determine the location of CMake. You already had the right idea by setting the ZLIB_LIBRARY manually, but were not quite getting it right:
add_subdirectory(<path_to_zlib_src_dir>)
set(ZLIB_INCLUDE_DIR "<path_to_zlib_src_dir>" "${CMAKE_BINARY_DIR}/<path_to_zlib_build_dir>")
set(ZLIB_LIBRARY zlib)
add_subdirectory(<path_to_lpng_src_dir>)
The include directory needs to contain both src and build path as zconf.h is build by CMake
The library name is only the CMake-target name, not the complete path to the resulting file.
On Windows dlls are not automatically copied by CMake. You might want to add some additional code to make sure that the zlib and lpng dlls end up in the right place.
You can call find_package(zlib) yourself to make sure it behaves as expected
In the rare case that a third-party lib does not use the find script, you will have to dig into that project's CMakeLists to find out what is going on

Cmake doesn't find Boost

I'm trying to configure a project using CMake, but it fails to find Boost libraries even though they are in the specified folder. I have specified Boost_INCLUDE_DIR, Boost_LIBRARYDIR and BOOST_ROOT , but I still get an error saying that CMake is not able to find Boost. What could be the reason of such error?
Are you sure you are doing it the correct way? The idea is that CMake sets BOOST_INCLUDE_DIR, BOOST_LIBRARYDIR and BOOST_ROOT automatically. Do something like this in CMakeLists.txt:
FIND_PACKAGE(Boost)
IF (Boost_FOUND)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
ADD_DEFINITIONS( "-DHAS_BOOST" )
ENDIF()
If boost is not installed in a default location and can, thus, not be found by CMake, you can tell CMake where to look for boost like this:
SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "C:/win32libs/boost")
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "C:/win32libs/boost/lib")
Of course, those two lines have to be before the FIND_PACKAGE(Boost) in CMakeLists.txt.
There is more help available by reading the FindBoost.cmake file itself. It is located in your 'Modules' directory.
A good start is to set(Boost_DEBUG 1) - this will spit out a good deal of information about where boost is looking, what it's looking for, and may help explain why it can't find it.
It can also help you to figure out if it is picking up on your BOOST_ROOT properly.
FindBoost.cmake also sometimes has problems if the exact version of boost is not listed in the Available Versions variables. You can find more about this by reading FindBoost.cmake.
Lastly, FindBoost.cmake has had some bugs in the past. One thing you might try is to take a newer version of FindBoost.cmake out of the latest version of CMake, and stick it into your project folder alongside CMakeLists.txt - then even if you have an old version of boost, it will use the new version of FindBoost.cmake that is in your project's folder.
Good luck.
For me this error was simply because boost wasn't installed so on ubuntu:
sudo apt install build-essential libboost-system-dev libboost-thread-dev libboost-program-options-dev libboost-test-dev
I struggled with this problem for a while myself. It turned out that cmake was looking for Boost library files using Boost's naming convention, in which the library name is a function of the compiler version used to build it. Our Boost libraries were built using GCC 4.9.1, and that compiler version was in fact present on our system; however, GCC 4.4.7 also happened to be installed. As it happens, cmake's FindBoost.cmake script was auto-detecting the GCC 4.4.7 installation instead of the GCC 4.9.1 one, and thus was looking for Boost library files with "gcc44" in the file names, rather than "gcc49".
The simple fix was to force cmake to assume that GCC 4.9 was present, by setting Boost_COMPILER to "-gcc49" in CMakeLists.txt. With this change, FindBoost.cmake looked for, and found, my Boost library files.
You can also specify the version of Boost that you would like CMake to use by passing -DBOOST_INCLUDEDIR or -DBOOST_ROOT pointing to the location of correct version boost headers
Example:
cmake -DBOOST_ROOT=/opt/latestboost
This will also be useful when multiple boost versions are on the same system.
I also had a similar problem and discovered that the BOOST_INCLUDE_DIR, BOOST_LIBRARYDIR and BOOST_ROOT env variables must hold absolute paths.
HTH!
In my case Boost was not installed. I used below command on Mac and then cmake find_package(Boost) works like a charm
brew install Boost
Please note upper case 'B' in Boost!
If you are building your own boost do not forget to use the --layout=versioned otherwise the search for a particular version of library will fail
For cmake version 3.1.0-rc2 to pick up boost 1.57 specify -D_boost_TEST_VERSIONS=1.57
cmake version 3.1.0-rc2 defaults to boost<=1.56.0 as is seen using -DBoost_DEBUG=ON
cmake -D_boost_TEST_VERSIONS=1.57 -DBoost_DEBUG=ON -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
One more bit of advice for anyone trying to build CGAL in particular, with statically linked Boost. It is not enough to define Boost_USE_STATIC_LIBS; it gets overridden by the time Boost_DEBUG outputs its value. The thing to do here is to check the "Advanced" checkbox and to enable CGAL_Boost_USE_STATIC_LIBS.
I had the same problem while trying to run make for a project after installing Boost version 1.66.0 on Ubuntu Trusty64. The error message was similar to (not exactly like) this one:
CMake Error at
/usr/local/Cellar/cmake/3.3.2/share/cmake/Modules/FindBoost.cmake:1245 (message):
Unable to find the requested Boost libraries.
Boost version: 0.0.0
Boost include path: /usr/include
Detected version of Boost is too old. Requested version was 1.36 (or newer).
Call Stack (most recent call first):
CMakeLists.txt:10 (FIND_PACKAGE)
Boost was definitely installed, but CMake couldn't detect it. After spending plenty of time tinkering with paths and environmental variables, I eventually ended up checking cmake itself for options and found the following:
--check-system-vars = Find problems with variable usage in system files
So I ran the following in the directory at issue:
sudo cmake --check-system-vars
which returned:
Also check system files when warning about unused and uninitialized variables.
-- Boost version: 1.66.0
-- Found the following Boost libraries:
-- system
-- filesystem
-- thread
-- date_time
-- chrono
-- regex
-- serialization
-- program_options
-- Found Git: /usr/bin/git
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/myproject
and resolved the issue.
See FindBoost.cmake first. The variables you set are the correct ones but they should be all uppercase.
Make sure the library architecture matches with CMake configuration.
cmake -A x64 ..
I suggest creating a minimal executable which only includes a Boost library to see if it compiles.
#include <iostream>
#include <boost/date_time.hpp>
int main() {
using namespace std;
using namespace boost::gregorian;
date today = day_clock::local_day();
cout << today << endl;
}
find_package(Boost REQUIRED COMPONENTS
date_time
)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIRS})
add_executable(test_boost "test_boost.cpp")
target_link_libraries(test_boost Boost::date_time)
Start debugging by checking Boost_FOUND first.
message(STATUS "Boost_FOUND: ${Boost_FOUND}")
The version should be found even if no libraries are found. (Boost_VERSION)
If Boost_LIBRARY_DIRS becomes non-empty, it should compile.
I had the same problem, and none of the above solutions worked. Actually, the file include/boost/version.hpp could not be read (by the cmake script launched by jenkins).
I had to manually change the permission of the (boost) library (even though jenkins belongs to the group, but that is another problem linked to jenkins that I could not figure out):
chmod o+wx ${BOOST_ROOT} -R # allow reading/execution on the whole library
#chmod g+wx ${BOOST_ROOT} -R # this did not suffice, strangely, but it is another story I guess
This can also happen if CMAKE_FIND_ROOT_PATH is set as different from BOOST_ROOT.
I faced the same issue that in spite of setting BOOST_ROOT, I was getting the error.
But for cross compiling for ARM I was using Toolchain-android.cmake in which I had (for some reason):
set(BOOST_ROOT "/home/.../boost")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --sysroot=${SYSROOT}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --sysroot=${SYSROOT} -I${SYSROOT}/include/libcxx")
set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS}")
set(CMAKE_FIND_ROOT_PATH "${SYSROOT}")
CMAKE_FIND_ROOT_PATH seems to be overriding BOOST_ROOT which was causing the issue.
For those using python3.7 docker image, this solved:
apt install libboost-system-dev libboost-thread-dev
Maybe
brew install boost
will help you.

Resources