First I downloaded the GLFW 32 bit binaries for Windows from their website. Below are the contents of this download:
I then copied the "include" and "lib-vc2019" files into a folder called "Dependencies" under my Clion project folder "OpenGL":
Following the instructions from "With CMake and installed GLFW binaries" from https://www.glfw.org/docs/3.3/build_guide.html#build_link_cmake_package
In my CMakeLists.txt file I have the following:
cmake_minimum_required(VERSION 3.19)
project(OpenGL)
set(CMAKE_CXX_STANDARD 20)
add_executable(OpenGL Main.cpp)
include_directories(Dependencies)
find_package(glfw3 3.3 REQUIRED)
target_link_libraries(OpenGL glfw)
When I try to build, I get the following errors:
CMake Error at CMakeLists.txt:10 (find_package):
By not providing "Findglfw3.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "glfw3", but
CMake did not find one.
Could not find a package configuration file provided by "glfw3" (requested
version 3.3) with any of the following names:
glfw3Config.cmake
glfw3-config.cmake
Add the installation prefix of "glfw3" to CMAKE_PREFIX_PATH or set
"glfw3_DIR" to a directory containing one of the above files. If "glfw3"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
See also "C:/Users/moehe/Desktop/CS/CPP/OpenGL/cmake-build-debug/CMakeFiles/CMakeOutput.log".
mingw32-make.exe: *** [Makefile:194: cmake_check_build_system] Error 1
Have spent a lot of time on this and very confused. If someone could provide a step by step guidance to make this work, would greatly appreciate it.
You misunderstood the "installed" part: those generate a glfw3Config.cmake file that tells CMake where the library and headers live. find_package will find and load that file.
Replace the last two lines of your CMake file with the following. This sets up a CMake target with the predefined library and header files:
add_library(glfw STATIC IMPORTED)
set_target_properties(glfw PROPERTIES
IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/Dependencies/lib-vc2019/glfw3.lib"
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(OpenGL glfw)
See the phenomenal It's time to do CMake right for a good introduction to modern CMake.
Related
I want to compile SealPIR library using emscripten to generate a wasm file.
When using this command:
emcmake cmake .
I get this error:
CMake Error at CMakeLists.txt:19 (find_package):
By not providing "FindSEAL.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "SEAL", but
CMake did not find one.
Could not find a package configuration file provided by "SEAL" (requested
version 3.2.0) with any of the following names:
SEALConfig.cmake
seal-config.cmake
Add the installation prefix of "SEAL" to CMAKE_PREFIX_PATH or set
"SEAL_DIR" to a directory containing one of the above files. If "SEAL"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
See also "/home/Zied/webassembly/SealPIR/CMakeFiles/CMakeOutput.log".
emcmake: error: 'cmake . -DCMAKE_TOOLCHAIN_FILE=/home/Zied/webassembly/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_CROSSCOMPILING_EMULATOR="/home/Zied/webassembly/emsdk/node/14.15.5_64bit/bin/node"' failed (1)
SEAL is correctly installed. when i run the same command without emcmake it works just fine.
This is my CMakeList
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(SealPIR VERSION 2.1 LANGUAGES CXX)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
add_executable(main
main.cpp
)
add_library(sealpir STATIC
pir.cpp
pir_client.cpp
pir_server.cpp
)
find_package(SEAL 3.2.0 EXACT REQUIRED)
target_link_libraries(main sealpir SEAL::seal)
When using a toolchain file for cross compiling, CMake will by default disable system libraries. It won't search into any directory to avoid finding files that is not compatible with the target system.
You think you didn't used a toolchain file? Think again! emcmake hides that from you. Look carefully at the error output.
Here you compiled the SEAL library, but you installed it in the default path, which is /usr/local.
We can tell CMake to explicitly search there, but I wouldn't recommend, but you can try if it works:
emcmake cmake . -CMAKE_PREFIX_PATH=/usr/local
The proper solution would be to create a directory with all the emscripten libraries in it:
# In the SEAL build directory
emcmake cmake .. -DCMAKE_INSTALL_PREFIX=/home/anblic/webassembly/install
Then after installing the libraries in that directory, you can set the prefix path in the same directory as the install path:
# Assuming you're in a build/ subdirectory
emcmake cmake .. -DCMAKE_PREFIX_PATH=/home/anblic/webassembly/install
I'm trying to use Caffe in my C++ project which I compile with CMakeLists.txt, but it doesn't want to work. My only line in the code is
#include <caffe/caffe.hpp>
I compiled Caffe myself, it is installed in the directory "/home/tamas/caffe". My CMakeLists.txt looks like this so far:
cmake_minimum_required (VERSION 3.5)
include(FindPkgConfig)
project (main)
set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD_REQUIRED TRUE)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -pthread")
set (source_dir "${PROJECT_SOURCE_DIR}/src/")
set (OpenCV_DIR "/home/tamas/opencv/include/opencv2")
set (Caffe_DIR "/home/tamas/caffe")
file (GLOB source_files "${source_dir}/ssd_video.cpp")
find_package(OpenCV 4.4.0 REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
find_package(Caffe REQUIRED)
include_directories(${Caffe_INCLUDE_DIRS})
add_executable (main ${source_files})
target_link_libraries(main ${OpenCV_LIBS})
target_link_libraries(main ${Caffe_LIBRARIES})
The error is the following:
CMake Error at CMakeLists.txt:24 (find_package):
By not providing "FindCaffe.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Caffe", but
CMake did not find one.
Could not find a package configuration file provided by "Caffe" with any of
the following names:
CaffeConfig.cmake
caffe-config.cmake
Add the installation prefix of "Caffe" to CMAKE_PREFIX_PATH or set
"Caffe_DIR" to a directory containing one of the above files. If "Caffe"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
The problem is that I have searched and I don't have a FindCaffe.cmake file on my computer. I found an example for CaffeConfig.cmake, but I tried it and it doesn't work either.
Is there a way I can link Caffe with my C++ project? Thanks!
To fix this issue you may do the following:
Download this FindCAFFE.cmake file
Create cmake dir in your repo root directory and put the downloaded file there.
Modify your CMake file:
add set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
change set (Caffe_DIR "/home/tamas/caffe") to set (CAFFE_ROOT_DIR "/home/tamas/caffe")
change find_package(Caffe REQUIRED) to find_package(CAFFE REQUIRED)
use CAFFE_INCLUDE_DIRS and CAFFE_LIBRARIES for include directories and link libraries respectively
Clean up your build dir and run cmake command again
<library>_DIR should not be set manually in CMake code usually. There are better alternatives that should be used as setting these variable won't necessarily do what you want. It won't change where find_package finds its libraries.
The CaffeConfig.cmake file is generated when building Caffe. You should never download another one, these files are compatible only with a specific build configuration.
The Caffe library supports to be used with CMake, so FindCaffe.cmake is unnecessary.
For find_package to work, either set the <package>_ROOT variable (require CMake 3.12 minimum) or you must append the install path in CMAKE_PREFIX_PATH. Here's a CMake example that uses the prefix path:
# If you only built the library
list (APPEND CMAKE_PREFIX_PATH "/home/tamas/caffe/build-dir")
# If you installed the library there
list (APPEND CMAKE_PREFIX_PATH "/home/tamas/caffe/")
find_package(Caffe REQUIRED)
Note that the Caffe_LIBRARIES and Caffe_INCLUDE_DIRS won't be set. This is old CMake style and the Caffe library uses the new style. This is what you should do:
target_link_libraries(main PUBLIC caffe caffeproto)
This line add both include directory and adds linking to the libraries too.
I'm trying to add GLFW to my C++ project in CLion. I've downloaded the pre-built Windows binaries, and I'm attempting to add them to my CMake file, but I'm getting errors. Here's my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.12.3)
project(Streamlined)
set(CMAKE_CXX_STANDARD 17)
list(APPEND CMAKE_MODULE_PATH "$(CMAKE_CURRENT_LIST_DIR}/cmake")
add_executable(Streamlined src/main.cpp)
find_package(glfw3 REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS})
target_link_libraries(${CMAKE_PROJECT_NAME} ${GLFW_LIBRARY})
And my cmake/Findglfw3.cmake file:
set(FIND_GLFW_PATHS ${CMAKE_SOURCE_DIR}/../Library/glfw-3.2.1.bin.WIN64/lib)
find_path(GLFW_INCLUDE_DIR glfw3.h
PATH_SUFFIXES include
PATHS ${FIND_GLFW_PATHS})
find_library(GLFW_LIBRARY NAMES libglfw3
PATH_SUFFIXES lib
PATHS ${FIND_GLFW_PATHS})
However, I'm getting the following error:
CMake Error at CMakeLists.txt:10 (find_package):
By not providing "Findglfw3.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "glfw3", but
CMake did not find one.
Could not find a package configuration file provided by "glfw3" with any of
the following names:
glfw3Config.cmake
glfw3-config.cmake
Add the installation prefix of "glfw3" to CMAKE_PREFIX_PATH or set
"glfw3_DIR" to a directory containing one of the above files. If "glfw3"
provides a separate development package or SDK, be sure it has been
installed.
I created the Findglfw3.cmake file, and appended it to CMAKE_MODULE_PATH, so I don't understand why I'm getting this error. I attempted this solution as well, but I'm getting a "PkgConfig not found" error.
I've been trying for a few days to correctly setup OpenCV with CLion with little success, so asking on SO.
Here's what my CMakeLists looks like:
cmake_minimum_required(VERSION 3.12)
project(ocv_test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(OpenCV REQUIRED)
set(SOURCE_FILES main.cpp)
add_executable(ocv_test ${SOURCE_FILES})
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(ocv_test ${OpenCV_LIBS})
Here's the error I get:
"C:\Program Files\JetBrains\CLion 2018.2.2\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\Owner\CLionProjects\ocv-test
Could not find OpenCV_CORE_INCLUDE_DIR
Could not find OpenCV_HIGHGUI_INCLUDE_DIR
Include dir: OFF
CMake Error at C:/Program Files/JetBrains/CLion 2018.2.2/bin/cmake/win/share/cmake-3.12/Modules/FindOpenCV.cmake:220 (MESSAGE):
OpenCV required but some headers or libs not found. Please specify it's
location with OpenCV_ROOT_DIR env. variable.
Call Stack (most recent call first):
CMakeLists.txt:6 (find_package)
-- Configuring incomplete, errors occurred!
See also "C:/Users/Owner/CLionProjects/ocv-test/cmake-build-debug/CMakeFiles/CMakeOutput.log".
I primarily followed these steps from another SO answer, but here are the steps:
Installed MinGW-64
Architecture: x86_64, Threads: posix, Exception: sjlj
Installed CMake 3.12.2 x64 msi
In System variables, set/create the following:
_CMAKE_HOME (C:\Program Files (x86)\CMake)
_MINGW_HOME (C:\mingw\mingw64)
Then, add the following to Path variable:
%_CMAKE_HOME%\bin
%_MINGW_HOME%\bin
Download OpenCV 3.4.3, and Extract to:
C:\opencv\opencv-3.4.3
Using CMake, Configure w/ MinGW Makefiles and specifying Native compilers:
C: C:/mingw/mingw64/bin/x86_64-w64-mingw32-gcc.exe
C++: C:/mingw/mingw64/bin/x86_64-w64-mingw32-g++.exe
Then, Generate (without Tests, Docs, Python, WITH_IPP, WITH_MSMF) to:
C:_dev_sw\opencv\opencv-3.4.3\build_mingw
Run mingw32-make, then mingw32-make install in C:_dev_sw\opencv\opencv-3.4.3\build_mingw
In System variables, set/create the following:
_OPENCV_HOME (C:\opencv\opencv-3.4.3\build_mingw\install\x64\mingw)
Then, add the following to Path variable:
%_OPENCV_HOME%\bin
Add FindOpenCV.cmake to:
C:\Program Files\JetBrains\CLion 2018.2.2\bin\cmake\win\share\cmake-3.12\Modules
Create new C++ executable project in CLion (ocv-test)
Update MakeLists.txt file (see above)
Reload MakeLists.txt and get errors shown above
I tried to update the CMakeLists as below, but still same errors:
cmake_minimum_required(VERSION 3.12)
project(ocv_test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Where to find CMake modules and OpenCV
set(OpenCV_DIR "C:\\opencv\\opencv-3.4.3\\build_mingw\\install")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(ocv_test main.cpp)
# add libs you need
set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs)
# linking
target_link_libraries(ocv_test ${OpenCV_LIBS})
Unlike this SO answer, I do not see OpenCV_DIR name in my CMake build. Also, I tried updating _OPENCV_HOME to OpenCV_ROOT_DIR (as error says), but that didn't work either.
Does anything seem off?
===
Edit 1:
FindOpenCV was the issue (so skip step 11). Setting the OPENCV_DIR var in CMakeLists fixed the errors, and built successfully (Thanks Tsyvarev!).
I'm not sure if setting OPENCV_DIR in CMakeLists will be an issue if the project is ran on another PC and/or OS, so I added OPENCV_DIR entry (pointing to /install directory) into CMake GUI, Repeated steps 6-8, created new but similar CLion project, and got the following error:
CMake Error at CMakeLists.txt:10 (find_package):
By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "OpenCV", but
CMake did not find one.
Could not find a package configuration file provided by "OpenCV" with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If "OpenCV"
provides a separate development package or SDK, be sure it has been
installed.
Again, this is fixed if I set the OPENCV_DIR variable. But how can it be avoided since it's already configured in GUI?
I'm trying to set up a really basic project with Cmake and OpenCV 3.0.
My folder structure looks like this:
OpenCVTest
|
|--- build
|--- data
|--- include
|--- src
|--- CMakeLists.txt
The CMakeLists.txt file has the following content:
cmake_minimum_required(VERSION 3.4)
project(OpenCVTest)
find_package(OpenCV REQUIRED)
include_directories(
${CMAKE_SOURCE_DIR}/include
${OpenCV_INCLUDE_DIRS})
set(SOURCES
src/main.cpp
src/MyData.cpp
include/MyData.h)
add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
I extracted OpenCV, set the environment variable OPENCV_DIR = <PATH_TO_OPEN_CV>\opencv\build\x86\vc12, and added %OPENCV_DIR%\bin to the PATH variable in Windows, as suggested by the OpenCV + CMake tutorial.
When running CMake, the following error message occurs:
CMake Error at CMakeLists.txt:4 (find_package):
By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has asked
CMake to find a package configuration file provided by "OpenCV", but CMake did
not find one.
Could not find a package configuration file provided by "OpenCV" with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If "OpenCV"
provides a separate development package or SDK, be sure it has been
installed
I therefore changed the value of the environment variable OpenCV_DIR = <PATH_TO_OPEN_CV>\opencv\build, where the OpenCVConfig.cmake file is located.
After doing so (and restarting to Windows to update the environment variables!!!), CMake is able to configure and generate the project successfully.
However, CMake uses static library linking...
OpenCV STATIC: ON
Found OpenCV 3.0.0 in <PATH_TO_CMAKE>/opencv/build/x86/vc12/staticlib
... and you have to switch the Runtime Library in Visual Studio (C/C++ --> Code Generation --> Runtime Library) to /MTd.
Adding...
set(BUILD_SHARED_LIBS ON)
before
find_package(OpenCV REQUIRED)
will force CMake to use dynamic linking, however, then the PATH variable does not contain the correct path to the OpenCV DLL's, and <PATH_TO_OPEN_CV>\opencv\build\x86\vc12\bin needs to be added (for 32bit and VS2013).
All ob the above solutions seem quite inelegant and not really portable to me. I also tried several FindOpenCV.cmake files, however, none of them were able to correctly find the OpenCV directory.
My question is: Can anybody provide a solution for setting up a very basic CMake + OpenCV 3.0 example project, that does not have the above described shortcomings?