CLion and CMake Doesn't Complain but Failed to build OpenCV - windows

Summary
In Windows, CLion doesn't complain anything after cmake and before makefile.
All code seems to link correctly without error. I am able to see the reference, documents, linter and jump into cv::Mat or opencv.hpp header file with ctrl RMB.
CMake seems to correctly generate make files without error.
But the compile error occurs: undefined reference to OpenCV methods.
my setup
CMakeLists.txt:
cmake_minimum_required(VERSION 3.9)
project(test)
# Set compile version
set(CMAKE_CXX_STANDARD 17)
add_executable(test ./test.cpp )
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " libraries: ${OpenCV_LIBRARIES}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
target_link_libraries(test ${OpenCV_LIBRARIES})
The output seems correct
"D:\Program Files\JetBrains\CLion 2021.3.2\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" D:\repo\CS5330\test
-- OpenCV library status:
-- version: 4.5.5
-- libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_world
-- libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_world
-- include path: /path/to/scoop/apps/opencv/current/include
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/test/cmake-build-debug
[Finished]
And this generates CMakeCache.txt, which includes,
//The directory containing a CMake configuration file for OpenCV.
OpenCV_DIR:PATH= /path/to/scoop/apps/opencv/current/x64/vc15/lib
//Details about finding OpenCV
FIND_PACKAGE_MESSAGE_DETAILS_OpenCV:INTERNAL=[/path/to/scoop/apps/opencv/current][v4.5.5()]
As you see, system-wide environment variable OpenCV_DIR has been already set and read correctly by CLion.
Here is a simple test code, but failed to run
#include <opencv2/opencv.hpp>
int main() {
cv::Mat img = cv::imread("./test.jpg", -1);
cv::imshow("Mon image", img);
cv::waitKey(0);
return 0;
}
I also installed msys2 from winget, and from msys2 installed clang, make, MinGW-w64 GDB,cmake. Following this tutorial.
And tested through that toolchain instead of CLion bundled toolchain, it returns the same result.
OpenCV binary is from scoop.
For some reason, it has the same problem as one from homebrew. In opencv.hpp, includes headers are incorrect due to file hierarchy that opencv.hpp is inside opencv2 folder.
I changed all #include "opencv2/header.hpp" to #include "header.hpp", but it doesn't help, and vice versa.
Can't figure out the reason for hours.. Any help will be appreciated.

Related

Error in include omp.h library with MacOS and CMake [duplicate]

I'm attempting to add OpenMP to a project that is building with CMake. I'm having no problem building it on Linux with the standard CMake/OpenMP addition:
find_package(OpenMP)
if (OPENMP_FOUND)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}
${OpenMP_EXE_LINKER_FLAGS}")
endif()
Unfortunately this doesn't seem to work on macOS targets. When cmake is called, the following error is given:
-- Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS)
-- Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS)
-- Could NOT find OpenMP (missing: OpenMP_C_FOUND OpenMP_CXX_FOUND)
I'm using macOS High Sierra (10.13.3) along with CMake 3.11. I've installed OpenMP 5.01 via brew, 'brew install libomp'. I've found some previous posts commenting on issues regarding these three but they all seem to deal with a previous way of installing OpenMP on macOS, "brew install clang-omp".
I'm thinking this might have something to do with CMake not support this OpenMP install as I'm able to use OpenMP no problem with standard makefiles. Any information provided would be much appreciated.
I've been able to answer my own question (apologies for not figuring this out beforehand, hopefully this can help others with the same issue).
It seems that a patch has been submitted to CMake to allow it to properly create buildsystems with the new OpenMP install: https://gitlab.kitware.com/cmake/cmake/merge_requests/1812
For others seeing this in the future, update to CMake 3.12 if it has been released at the time of reading.
I had the same problem and it took one complete day to find the solution. I am using Mac-Sierra 10.13.4. I want to use Opencv3 (I think the same problem also appears for opencv2) and openMP. I was actually using Clion as the IDE (CLion uses cmake to configure the project unlike other IDE), so I have to write the CMakeLists.txt file.
There was a conflict of using gcc as the compiler for openCV and openMP. If you use gcc as the compiler then it gives error for opencv as :
imwrite() on OS X error: Undefined symbols
You need to specifically use llvm compiler on OS X to resolve this issue. Following, I am giving the correct code for using OpenCV and OpenMP on Mac-Sierra:
cmake_minimum_required(VERSION 3.10)
project(MyOpenCVTest)
set(CMAKE_CXX_STANDARD 11)
add_executable(MyOpenCVTest main.cpp)
# set("OpenCV_DIR" "/modules/opencv/3.4.1/share/OpenCV/")
set(CMAKE_PREFIX_PATH "/usr/local/Cellar/opencv#3/")
set(OpenCV_INCLUDE_DIRS "/usr/local/Cellar/opencv#3/include/")
set(OpenCV_LIBS "/usr/local/Cellar/opencv#3/lib/")
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
if(UNIX)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=gnu++0x")
endif()
set(CMAKE_C_COMPILER "/usr/local/Cellar/llvm/6.0.0/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/local/Cellar/llvm/6.0.0/bin/clang++")
set(OPENMP_LIBRARIES "/usr/local/Cellar/llvm/6.0.0/lib")
set(OPENMP_INCLUDES "/usr/local/Cellar/llvm/6.0.0/include")
OPTION (USE_OpenMP "Use OpenMP to enamble <omp.h>" ON)
# Find OpenMP
if(APPLE AND USE_OpenMP)
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
set(OpenMP_C "${CMAKE_C_COMPILER}")
set(OpenMP_C_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
set(OpenMP_C_LIB_NAMES "libomp" "libgomp" "libiomp5")
set(OpenMP_libomp_LIBRARY ${OpenMP_C_LIB_NAMES})
set(OpenMP_libgomp_LIBRARY ${OpenMP_C_LIB_NAMES})
set(OpenMP_libiomp5_LIBRARY ${OpenMP_C_LIB_NAMES})
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(OpenMP_CXX "${CMAKE_CXX_COMPILER}")
set(OpenMP_CXX_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
set(OpenMP_CXX_LIB_NAMES "libomp" "libgomp" "libiomp5")
set(OpenMP_libomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
set(OpenMP_libgomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
set(OpenMP_libiomp5_LIBRARY ${OpenMP_CXX_LIB_NAMES})
endif()
endif()
if(USE_OpenMP)
find_package(OpenMP REQUIRED)
endif(USE_OpenMP)
if (OPENMP_FOUND)
include_directories("${OPENMP_INCLUDES}")
link_directories("${OPENMP_LIBRARIES}")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
# set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif(OPENMP_FOUND)
include_directories( ${OpenCV_INCLUDE_DIRS} )
target_link_libraries( MyOpenCVTest ${OpenCV_LIBS})
TARGET_LINK_LIBRARIES(MyOpenCVTest opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs)
You might want to set e.g. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread") such that the linker automatically detects the appropriate pthread library
Do
brew reinstall llvm
to install llvm compiler.
Please note that, you can't use gcc compiler on Mac-Sierra for your project which needs openMP and also openCV. You need to use llvm compiler.
Verify the correct location of llvm and openCV installation directory.

Build GL application with CMake, GLEW_DIR and GLFW_DIR not set

I'm trying to build a demo depending on GLEW and GLFW on macOS. I have installed GLEW and GLFW with brew and they both locate under /usr/local/Cellar. Then I use CMake to locate dependencies and my codes are as follows:
# for GLEW
set(GLEW_DIR /usr/local/Cellar/glew/2.1.0/lib/cmake/glew)
find_package(GLEW REQUIRED PATHS ${GLEW_DIR} NO_DEFAULT_PATH)
message(STATUS ${GLEW_INCLUDE_DIR})
# for GLFW
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
message(STATUS ${GLFW_INCLUDE_DIR})
But as a result the first message always prints /usr/local/include and the second prints nothing. What's the problem with my CMake code? Thanks.

FindGLEW.cmake No Rule To Make Target Error [SOLVED]

I am trying to build a very simple 'hello triangle' OpenGL application using CMake. I want to use find_package(GLEW) and the resulting imported target to link with Glew. When I run the make command, I am getting a 'No rule to make target' error.
According to the CMake documentation
https://cmake.org/cmake/help/latest/module/FindGLEW.html
https://cmake.org/cmake/help/latest/command/find_package.html
I can use find_package(GLEW) to return a target GLEW::GLEW which I will be able to use to link the Glew library to my code. When I run cmake .. everything builds and it reports that the glew-config.cmake file has been found.
-- Found GLEW: /usr/local/lib/cmake/glew/glew-config.cmake
I then run make and receive this error
$ make
Scanning dependencies of target test
[ 50%] Building CXX object CMakeFiles/test.dir/main.cpp.o
make[2]: *** No rule to make target `/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/OpenGL.framework', needed by `test'. Stop.'
I am running Mojave 10.14 and used homebrew to install cmake and glew which are both up to date. The path returned in the error above does not exist and I don't know where it is coming from.
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.15)
project(test)
find_package(GLEW REQUIRED)
add_executable(test main.cpp)
target_link_libraries(test GLEW::GLEW)
Here is my source code:
#include <GL/glew.h>
#include <iostream>
int main(int argc, char **argv){
std::cout << "Glew Test" << std::endl;
}
As you can see it is the bare minimum code needed to simply link Glew.
The problem seems to be similar to this question:
CMake FIND_PACKAGE succeeds but returns wrong path
However, in that case there was a solution using a command line flag specific to Boost. The only reference I have found regarding FindGLEW and problems with macOS is here:
https://gitlab.kitware.com/cmake/cmake/issues/19542
But that seems to have been resolved.
The problem I am having seems to be this path that FindGLEW.cmake is returning:
/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/OpenGL.framework
I have looked in the actual FindGLEW.cmake file to see if I could find the source of this path but I'm not an expert with CMake so got lost pretty quickly. How could I find the origin of this path? Are there any CMake flags or variables I can set to work around this similar to the Boost question above?
I have successfully linked with Glew using this CMakeLists.txt:
cmake_minimum_required(VERSION 3.15)
project(test)
find_library(GLEW_DYLIB GLEW "/usr/local/Cellar/glew/2.1.0/lib")
if(NOT GLEW_DYLIB)
message(FATAL_ERROR "Glew not found")
endif()
add_library(Glew_target SHARED IMPORTED)
set_property(TARGET Glew_target PROPERTY IMPORTED_LOCATION "/usr/local/Cellar/glew/2.1.0/lib/libGLEW.dylib")
set_property(TARGET Glew_target PROPERTY IMPORTED_IMPLIB ${GLEW_DYLIB})
include_directories(
"/usr/local/Cellar/glew/2.1.0/include/"
)
add_executable(test main.cpp)
target_link_libraries(test Glew_target)
However, I would prefer to keep things as simple as possible with find_package() as that is definitely the cleaner method.
EDIT: SOLVED - see Tsyvarev's comment below clarifying the nature of the glew-config file that CMake was finding above. I removed this file and rebuilt the project with no problems.

Point FindBoost CMAKE to boost_python Windows 10, VS 2017

My high level goal is to install the BGSlibrary which requires boost for python on Windows 10 using Visual Studio 2017. I compiled opencv and boost (1.64.0) from source using cmake 3.9.0. During cmake for BGSLIBRARY I get
$ cmake -DBGS_PYTHON_SUPPORT=ON -DBOOST_ROOT="C:/Program Files/boost_1_64_0/" ..
-- BGSLIBRARY WITH PYTHON SUPPORT: ON
-- OpenCV library status:
-- version: 3.3.0
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.9/ Modules/FindBoost.cmake:1877 (message):
Unable to find the requested Boost libraries.
Boost version: 1.64.0
Boost include path: C:/Program Files/boost_1_64_0
Could not find the following Boost libraries:
boost_python
No Boost libraries were found. You may need to set BOOST_LIBRARYDIR to the
directory containing Boost libraries or BOOST_ROOT to the location of
Boost.
Call Stack (most recent call first):
CMakeLists.txt:75 (find_package)
I've seen lots of questions on the cmake findboost module. Okay lets start from here.
I downloaded and extract boost 1.64.0 and placed it here
I ran bootstrap.bat and .b2 to generate the build boost
C:\Program Files\boost_1_64_0>b2 toolset=msvc-14.1 --with-python --user-config=user-config.jam
with a user config
import toolset : using ;
using msvc : 14.1 ;
using python
: 2.7 # Version
: C:\\Python27\\python.exe # Interpreter
: C:\\Python27\\include # inc dir
: C:\\Python27\\libs # link libs
: # conditions
;
I can see python source here
and can confirm that from within the Visual Studio 2017 command prompt I build boost with python support and it finds all targets successfully.
I can see a bunch of "python" .lib files here. Reading other questions suggests that is where it goes.
But I can't seem to get cmake to see it.
I've tried changing the name libboost_python to boost_python. I've tried pointing in cmake -DBOOST_ROOT, or -DBOOST_LIBRARYDIR (or non-debg, -BOOST_LIBRARYDIR). I've tried adding to the lib dir to PATH. But nothing seems to work. Is this a cmake problem, a incomplete boost installation or a problem with BGSLibrary?
EDIT
To answer #utopia, the CMakeList section in question reads
if(BGS_PYTHON_SUPPORT)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost REQUIRED COMPONENTS python)
find_package(PythonLibs REQUIRED)
message(STATUS "Boost library status:")
message(STATUS " version: ${Boost_VERSION}")
message(STATUS " libraries: ${Boost_LIBRARIES}")
message(STATUS " include path: ${Boost_INCLUDE_DIRS}")
message(STATUS "Python library status:")
message(STATUS " version: ${PYTHON_VERSION}")
message(STATUS " libraries: ${PYTHON_LIBRARIES}")
message(STATUS " include path: ${PYTHON_INCLUDE_DIRS}")
endif()
Does this mean that the .lib should be literally named python.lib? With no other characters or perhaps boost_python.lib. Is it that specific?
Building Boost.Python
#utopia led me to the right solution. The .lib needs to be literally named boost_python.lib, not appended with the visual studio compiler version, boost version etc. I was able to successfully build after that, no cmake flags needed.

Boost include headers not found using cmake

This question is very similar to Why is this boost header file not included, however the hints there don't (seem to) solve my problem.
I got a CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(jetorigin)
SET(Boost_ADDITIONAL_VERSIONS "1.43" "1.43.0" "1.44" "1.44.0" "1.45" "1.45.0")
SET(BOOST_ROOT "$ENV{HOME}/usr")
MESSAGE(STATUS "** Search Boost root: ${BOOST_ROOT}")
FIND_PACKAGE(Boost 1.43 COMPONENTS filesystem regex REQUIRED)
MESSAGE(STATUS "** Boost Include: ${Boost_INCLUDE_DIR}")
MESSAGE(STATUS "** Boost Libraries: ${Boost_LIBRARY_DIRS}")
MESSAGE(STATUS "** Boost Libraries: ${Boost_LIBRARIES}")
INCLUDE_DIRECTORIES(${BOOST_INCLUDE_DIR})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
ADD_SUBDIRECTORY(src)
And include some boost headers in my code like this:
#include <boost/regex.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
Now the output from cmake looks ok
-- ** Search Boost root: /home/oli/usr
-- Boost version: 1.43.0
-- Found the following Boost libraries:
-- filesystem
-- regex
-- ** Boost Include: /home/oli/usr/include
-- ** Boost Libraries: /home/oli/usr/lib
-- ** Boost Libraries: /home/oli/usr/lib/libboost_filesystem.so;/home/oli/usr/lib/libboost_regex.so
But I get this error:
error: boost/regex.hpp: No such file or directory
(and similar for the other includes). Full output from make VERBOSE=1 can be found here http://pastebin.ca/2039425. It looks as though there is no -I flag added even though Boost_INCLUDE_DIR seems to be set correctly.
I'm using CMake 2.8.1 by the way.
I would very much appreciate any hints on what is going wrong here..
EDIT:
I've found the problem. Seems I pulled some ancient version of my standard CMakeLists.txt from the depths of my archives. It needs to be:
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
(note the non-capitalized "Boost")...
Maybe older versions of CMake have been more tolerant in this respect or by bad luck I simply chose to start with a version of my CMakeLists.txt with an unfixed bug..
Since you did not specify a mode for find_package, it attempts to first use MODULE mode, and then CONFIG mode. As per the docs for the FindBoost module, the cache variable storing the directory containing Boost headers is named Boost_INCLUDE_DIR and not BOOST_INCLUDE_DIR. As per the CMake language docs' section on variables, CMake variable names are case-sensitive. I don't know why you had another CMakeLists.txt file with BOOST_INCLUDE_DIR working.

Resources