I want to use vcpkg in a CMake project in Windows, because I need boost and xerces that are both handled by this package manager.
I've the following CMakeLists.txt:
cmake_minimum_required (VERSION 3.12.0)
project (myproj)
set (CMAKE_PREFIX_PATH ${XERCES_ROOT})
set (Boost_USE_STATIC_LIBS ON)
set (Boost_USE_MULTITHREADED ON)
unset (Boost_INCLUDE_DIR CACHE)
unset (Boost_LIBRARY_DIRS CACHE)
# set (CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/modules)
find_package (Boost COMPONENTS filesystem regex REQUIRED)
find_package (XercesC CONFIG REQUIRED)
set (CMAKE_INCLUDE_CURRENT_DIR ON)
message (STATUS "binary dir is ${CMAKE_BINARY_DIR}")
include_directories (${CMAKE_BINARY_DIR}/${PROJECT_NAME}/)
include_directories (${CMAKE_CURRENT_SOURCE_DIR}/..)
include_directories (${Boost_INCLUDE_DIRS})
include_directories (${XercesC_INCLUDE_DIRS})
set (PROJECT_SRC
code.cpp
)
add_library (${PROJECT_NAME} SHARED ${PROJECT_SRC})
add_dependencies (${PROJECT_NAME} UPDATE_RESOURCES)
target_link_libraries (${PROJECT_NAME} ${Boost_LIBRARIES} XercesC::XercesC)
Boost and xerces-c are installed with vcpkg. Since I'm using Visual Studio Code I'm setting vcpkg variables in settings.json:
"cmake.configureSettings": {
"CMAKE_TOOLCHAIN_FILE" : "some/path/vcpkg/scripts/buildsystems/vcpkg.cmake",
"VCPKG_TARGET_TRIPLET": "x64-windows"
}
When I run che CMake I obtain following errors:
[cmake] CMake Error at C:/Program Files/CMake/share/cmake-3.14/Modules/FindBoost.cmake:2132 (message):
[cmake] Unable to find the requested Boost libraries.
[cmake]
[cmake] Unable to find the Boost header files. Please set BOOST_ROOT to the root
[cmake] directory containing Boost or BOOST_INCLUDEDIR to the directory containing
[cmake] Boost's headers.
[cmake] Call Stack (most recent call first):
[cmake] D:/projects/vcpkg/scripts/buildsystems/vcpkg.cmake:233 (_find_package)
[cmake] src/myroject/CMakeLists.txt:24 (find_package)
[cmake]
[cmake]
[cmake] CMake Error at D:/Projects/vcpkg/installed/x64-windows/share/xercesc/vcpkg-cmake-wrapper.cmake:1 (_find_package):
[cmake] Could not find a package configuration file provided by "XercesC" with any
[cmake] of the following names:
[cmake]
[cmake] XercesCConfig.cmake
[cmake] xercesc-config.cmake
[cmake]
[cmake] Add the installation prefix of "XercesC" to CMAKE_PREFIX_PATH or set
[cmake] "XercesC_DIR" to a directory containing one of the above files. If
[cmake] "XercesC" provides a separate development package or SDK, be sure it has
[cmake] been installed.
[cmake] Call Stack (most recent call first):
[cmake] D:/Projects/vcpkg/scripts/buildsystems/vcpkg.cmake:189 (include)
[cmake] src/ZLA/CMakeLists.txt:25 (find_package)
[cmake]
[cmake]
[cmake] Configuring incomplete, errors occurred!
[cmake] See also "D:/Projects/zla/build/vscode/CMakeFiles/CMakeOutput.log".
[cms-driver] Error during CMake configure: [cmake-server] Configuration failed.
At the moment I've installed xerces with vcpkg commands, while boost is currently not installed, but I was expecting that during the execution of the cmake command, vcpkg will download and build needed build packages.
I've tried also the command line:
cmake -DCMAKE_TOOLCHAIN_FILE=D:/Projects/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows ../
but the result is the same.
What I'm doing wrong? How can I use vcpkg successfully?
In theory it's as simple as (assuming vcpkg as installed in C:/vcpkg as it is for github actions);
Install your "foo" package with vcpkg install foo
Make sure your CMakeLists.txt finds and uses the package with;
find_package(FOO)
# Use these instead of the package doesn't have proper cmake
package support.
# find_path(FOO_INCLUDE_DIRS foo.h)
# find_library(FOO_LIBRARYS foo)
include_directories(${FOO_INCLUDE_DIRS})
target_link_libraries(myprogram ${FOO_LIBRARIES})
Run cmake with
-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake
But... this didn't work for me until I added --triplet x64-windows to the vcpkg install command.
The DCMAKE_TOOLCHAIN_FILE sets the various CMAKE_(SYSTEM_)?(PREFIX|LIBRARY|INCLUDE|FRAMEWORK)_PATH variables to enable the find_*() cmake functions to work, but note that these paths include the VCPKG_TARGET_TRIPLET. In my case the package install with vcpkg install <foo> defaulted to x86-windows but then invoking cmake with -DCMAKE_TOOLCHAIN_FILE=C:/.... defaulted to x64-windows so it couldn't find the the package.
Currently vcpkg defaults to the older x86 target, but modern Visual Studio (as used by githup actions) defaults to x64. The fix was to install the package with vcpkg -triplet x64-windows install <foo>. It took me way too long going down too many red-herring rabbit holes to discover this.
You need to install the packages beforehand (using vcpkg install ).
(Then you could specify the toolchain as a CMake option:
-DCMAKE_TOOLCHAIN_FILE=C:\path\to\vcpkg\scripts\buildsystems\vcpkg.cmake
but this won't work if you already specify a toolchain, such as when cross-compiling.)
"include" it, instead, to avoid this problem:
Add this line to the project CMakeLists.txt before find_package():
include(/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake)
boost is currently not installed, but I was expecting that during the execution of the cmake command, vcpkg will download and build needed build packages.
This is not the case as far as I know. You need to install the packages you want with vcpkg beforehand for the triplet you plan to use (i.e. x64-windows). You will then need to ensure that the correct triplet is being used when you run CMake (check the VCPKG_TARGET_TRIPLET variable in your CMakeCache.txt). If it's incorrect, you can change it and re-configure using CMake.
Additionally, based on the error output you're getting, it doesn't seem that xerces has been installed properly either using vcpkg. You can check what is installed with vcpkg by running:
vcpkg list --triplet x64-windows
I had the same issue and just solved it either with:
set(CURL_DIR "C:/Libs/vcpkg/installed/x64-windows/share/curl")
or
list(APPEND CMAKE_PREFIX_PATH "C:/Libs/vcpkg/packages/curl_x64-windows/share/curl/")
I installed vcpkg under C:/Libs
Related
I want to include Python.h and numpy/arrayobject.h in my C++ script. But it cannot open these source files.
CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(myProj VERSION 0.1.0 DESCRIPTION "myProj")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(PythonLibs REQUIRED) <--- work
find_package(Python3 3.6 COMPONENTS Interpreter NumPy REQUIRED) <--- not work
include_directories(${PYTHON_INCLUDE_DIRS})
add_executable(myProj main.cc)
target_link_libraries(myProj ${PYTHON_LIBRARIES} Python3:NumPy)
Error
[cmake] CMake Error at CMakeLists.txt:9 (find_package):
[cmake] By not providing "FindPython3.cmake" in CMAKE_MODULE_PATH this project has
[cmake] asked CMake to find a package configuration file provided by "Python3", but
[cmake] CMake did not find one.
[cmake]
[cmake] Could not find a package configuration file provided by "Python3"
[cmake] (requested version 3.6) with any of the following names:
[cmake]
[cmake] Python3Config.cmake
[cmake] python3-config.cmake
[cmake]
[cmake] Add the installation prefix of "Python3" to CMAKE_PREFIX_PATH or set
[cmake] "Python3_DIR" to a directory containing one of the above files. If
[cmake] "Python3" provides a separate development package or SDK, be sure it has
[cmake] been installed.
Path of NumPy
(venv) cyan#linux01:~/TEMP$ python -c "import numpy; print(numpy.get_include())"
/home/cyan/venv/lib/python3.6/site-packages/numpy/core/include
Question
How can I revise my CMakeLists.txt so to include the header numpy/arrayobject.h in C++?
I'm not seeing a lot of documentation for FindPython3 or FindPython for CMake 3.0.0 so I'm not sure if that module included before CMake 3.12.0. Version 3.12 is the first occurence I could find in the cmake documentation.
The most likely thing is that you just need to change to using
cmake_minimum_required(VERSION 3.12.0)
Updating your CMake version is probably the simplest solution but again you might have good reason to limit the version and just telling you to update isn't much of a solution.
Another hackier solution would be to use one of the paths from Python_INCLUDE_DIRS or Python_LIBRARIES do some relative pathing from there. Something like
set(numpy_INCLUDE_DIR "${Python_INCLUDE_DIR}/../site-packages/numpy
/core/include")
include_directories(${Python_INCLUDE_DIRS} ${numpy_INCLUDE_DIR})
This however assumes that your python directories are setup as if they came from a python distribution and would in turn assume that everyone that uses this also has a distribution with the same file structure.
I am trying to use OpenCV in VS Code.
Here's what I've done:
Installed OpenCV for windows.
Added "C:\opencv\build\x64\vc15\bin","C:\opencv\build\x64\vc15\lib" PATH environment variable.
Here's my CMakeLists.txt file.
cmake_minimum_required(VERSION 3.0.0)
project(opencvtest VERSION 0.1.0)
include(CTest)
enable_testing()
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(opencvtest main.cpp)
target_link_libraries( opencvtest ${OpenCV_LIBS} )
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
But the file throws the following error:
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=C:\TDM-GCC-64\bin\x86_64-w64-mingw32-gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:\TDM-GCC-64\bin\x86_64-w64-mingw32-g++.exe -Hc:/Users/Administrator/Desktop/open -Bc:/Users/Administrator/Desktop/open/build -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- OpenCV ARCH: x64
[cmake] -- OpenCV RUNTIME: mingw
[cmake] -- OpenCV STATIC: OFF
[cmake] CMake Warning at C:/opencv/build/OpenCVConfig.cmake:190 (message):
[cmake] Found OpenCV Windows Pack but it has no binaries compatible with your
[cmake] configuration.
[cmake]
[cmake] You should manually point CMake variable OpenCV_DIR to your build of OpenCV
[cmake] library.
[cmake] Call Stack (most recent call first):
[cmake] CMakeLists.txt:7 (find_package)
[cmake]
[cmake]
[cmake] CMake Error at CMakeLists.txt:7 (find_package):
[cmake] Found package configuration file:
[cmake]
[cmake] C:/opencv/build/OpenCVConfig.cmake
[cmake]
[cmake] but it set OpenCV_FOUND to FALSE so package "OpenCV" is considered to be
[cmake] NOT FOUND.
[cmake]
[cmake]
[cmake] -- Configuring incomplete, errors occurred!
I am trying to run a C++ file in VS Code that includes <opencv2/opencv.hpp>.
As the error suggests, CMake found your OpenCV installation, but it is not compatible. What is it not compatible with? Your compiler. The OpenCV installation is built with MSVC 15 (it also includes a 14 build). You have asked CMake to use MinGW as your compiler. Libraries need to have been built with the same (well, with some leeway) compiler for everything to work.
You have two options:
Build OpenCV yourself with MinGW, or try a third-party MinGW binary distribution of OpenCV. Web searches for "opencv mingw" turn up some
possibilities.
Use the MSVC compiler for your project. Microsoft offers some free versions of its tools. Just be sure to install the optional older toolsets so that you have access to the VC 15 tools to match OpenCV.
So My Workspace Screenshot After Trying a while i cant Get Cmake To find the required packages even after i did everything as shown in vcpkg
cmake_minimum_required(VERSION 3.0.0)
project(TEst VERSION 0.1.0)
include(CTest)
enable_testing()
set(CMAKE_TOOLCHAIN_FILE "N:/Vc-PKG/vcpkg/scripts/buildsystems/vcpkg.cmake")
find_package(glfw3 CONFIG REQUIRED)
target_link_libraries(TEst PRIVATE glfw)
add_executable(TEst main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
and my Vcpkg install directory is N:\Vc-PKG\vcpkg
i also added these lines in vscode
"C_Cpp.default.includePath": [
"N:/Vc-PKG/vcpkg/installed/x64-windows/include",
"N:/Vc-PKG/vcpkg/installed/x86-windows/include",
],
"c-cpp-flylint.cppcheck.includePaths": [
"N:\\Vc-PKG\\vcpkg\\installed\\x64-windows\\include",
"N:\\Vc-PKG\\vcpkg\\installed\\x86-windows\\include",
],
"cmake.generator": "MinGW Makefiles",
The output i get For the following is
[main] Configuring folder: Junks
[proc] Executing command: N:\MSYS64\mingw64\bin\cmake.EXE --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=N:\MSYS64\mingw64\bin\x86_64-w64-mingw32-gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=N:\MSYS64\mingw64\bin\x86_64-w64-mingw32-g++.exe "-Hl:/Programming Projects/Vs Code/Junks" "-Bl:/Programming Projects/Vs Code/Junks/build" -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] CMake Error at CMakeLists.txt:7 (find_package):
[cmake] Could not find a package configuration file provided by "glfw3" with any of
[cmake] the following names:
[cmake]
[cmake] glfw3Config.cmake
[cmake] glfw3-config.cmake
[cmake]
[cmake] Add the installation prefix of "glfw3" to CMAKE_PREFIX_PATH or set
[cmake] "glfw3_DIR" to a directory containing one of the above files. If "glfw3"
[cmake] provides a separate development package or SDK, be sure it has been
[cmake] installed.
[cmake]
[cmake] -- Configuring incomplete, errors occurred!
[cmake] See also "L:/Programming Projects/Vs Code/Junks/build/CMakeFiles/CMakeOutput.log".
[cmake]
Please any help would be much apricated, sry if i made a mistake this is the 1st time i am posting a question.
You can't set CMAKE_TOOLCHAIN_FILE after the call to project(). That's the command responsible for loading the toolchain file in the first place. Move it before the call to project() or better yet: set it at the command line or in a preset.
Also, unless you're actually using CMake 3.0.0, you shouldn't set it as a minimum. CMake is not forwards compatible, so without actually testing it on the minimum version, you have no way of knowing whether it will work that far back.
Background
I am relatively new to C++ and CMake and especially new to CGAL. As of now, I am simply trying to get familiar with CGAL so that I can remake an algorithm of mine that was originally built in MATLAB, but due to the inadequacy of their computational geometry libraries, I am moving to C++ and CGAL.
System and Versions Used
OS Kernel: Ubuntu 20.04
CGAL 5.2.1
CMake 3.16.3
Problem
I am currently trying to build the "Minimal Example Using Qt5", found here.
At first, I was getting an error saying, CGAL/Qt/Basic_viewer_qt.h: No such file or directory. This seemed to be a simple fix since the Qt folder was missing from the /usr/include/CGAL directory, along with others. Here, CGAL was installed with apt-get. To fix this I simply copied the full include directory from CGAL's github.
Now, I still have a problem remaining from CMake, where CGAL_FOUND returns FALSE and when I attempt to build the executable, I get no results or error messages (i.e., no main executable in cgalTest/bin).
Further Details
Project's Folder Structure
cgalTest
-bin
-build
--<CMake stuff>
-CMakeLists.txt
-doc
-include
-lib
-spike
-src
--main.cpp
-test
CMakeLists.txt
cmake_minimum_required(VERSION 3.16.3)
project(cgalTest)
### Link with Libraries ###
# CGAL
find_package(CGAL REQUIRED COMPONENTS Qt5 Core)
if(CGAL_FOUND AND CGAL_Qt5_FOUND)
#required to use basic_viewer
add_definitions(-DCGAL_USE_BASIC_VIEWER -DQT_NO_KEYWORDS)
#create the executable of the application
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp)
#link it with the required CGAL libraries
target_link_libraries(${PROJECT_NAME} CGAL::CGAL CGAL::CGAL_Qt5 CGAL::CGAL_Core)
else()
if(NOT CGAL_FOUND)
message("ERROR: this program requires CGAL and will not be compiled.")
endif()
if(NOT CGAL_Qt5_FOUND)
message("ERROR: this program requires CGAL_Qt5 and will not be compiled.")
endif()
endif()
main.cpp
#include <iostream>
#include <string>
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/draw_surface_mesh.h>
#include <fstream>
typedef CGAL::Simple_cartesian<double> Kernel;
typedef Kernel::Point_3 Point;
typedef CGAL::Surface_mesh<Point> Mesh;
using namespace std;
// Main Code
int main()
{
Mesh sm1;
std::ifstream in1((argc>1)?argv[1]:"data/elephant.off");
in1 >> sm1;
CGAL::draw(sm1);
return EXIT_SUCCESS;
}
How I am Building the Project
I simply run the following:
cmake --build /home/<user>/Projects/cgalTest/build
CMake Configuration Output
[main] Configuring folder: cgalTest
[proc] Executing command: /usr/bin/cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -H/home/<user>/Projects/cgalTest -B/home/<user>/Projects/cgalTest/build -G "Unix Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- Using header-only CGAL
[cmake] -- Targetting Unix Makefiles
[cmake] -- Using /usr/bin/c++ compiler.
[cmake] -- Boost include dirs: /home/linuxbrew/.linuxbrew/include
[cmake] -- Boost libraries:
[cmake] -- Using gcc version 4 or later. Adding -frounding-math
[cmake] ERROR: this program requires CGAL_Qt5 and will not be compiled.
[cmake] CMake Warning at /home/linuxbrew/.linuxbrew/lib/cmake/CGAL/CGAL_enable_end_of_configuration_hook.cmake:99 (message):
[cmake] =======================================================================
[cmake]
[cmake] CGAL performance notice:
[cmake]
[cmake] The variable CMAKE_BUILD_TYPE is set to "Debug". For performance reasons,
[cmake] you should set CMAKE_BUILD_TYPE to "Release".
[cmake]
[cmake] Set CGAL_DO_NOT_WARN_ABOUT_CMAKE_BUILD_TYPE to TRUE if you want to disable
[cmake] this warning.
[cmake]
[cmake] =======================================================================
[cmake] Call Stack (most recent call first):
[cmake] CMakeLists.txt:9999 (CGAL_run_at_the_end_of_configuration)
[cmake]
[cmake]
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: /home/<user>/Projects/cgalTest/build
Final Thoughts
As I have said, I am still pretty new to this and may need some hand holding during some of this. Thanks again, your help will be appreciated.
Also, do not hesitate to critique anything else you see: folder structure, coding practice, etc
So basically, when I installed qt5 I believe I used
sudo apt-get libcal-qt5-default
rather than
sudo apt-get libcal-qt5-dev.
This is of course based on my memory of what I think I did during installation, but simply running the installation for the "dev" fixed it (i.e., making CGAL_QT_FOUND to TRUE and then letting me compile).
Along with this, in main.cpp I was missing the argument definitions for argc and argv.
I cannot get boost::asio to work with cmake in my c++ program. I have actually tried and googled for many hours, but I cannot get it to work!
I want to include boost::asio in my c++ Project under Ubuntu 18.04 with a cmake file.
So I installed the newest CMake (cmake version 3.19.4), and I downloaded boost version 1.74 and executed
./bootstrap.sh --prefix=/usr/
sudo ./b2 install
The install directory is /home/boost/boost_1_74_0. My CMake file looks like this:
cmake_minimum_required (VERSION 3.1.0)
# Project name
project (machine_tryout VERSION 1.0)
# Boost (header only)
#set(Boost_DEBUG 1)
set(BOOST_ROOT "$ENV{HOME}/boost/boost_1_74_0")
set(Boost_INCLUDE_DIR "$ENV{HOME}/boost/boost_1_74_0")
set(Boost_LIBRARY_DIR "$ENV{HOME}/boost/boost_1_74_0/libs")
find_package(Boost REQUIRED Components asio)
# Set Executable
add_executable(${PROJECT_NAME} source/tryout.cpp)
But everything I get is the following:
vm-umic#vm:~/Projects/tryout/build$ cmake ..
CMake Warning at /snap/cmake/775/share/cmake-3.19/Modules/FindBoost.cmake:2034 (message):
No header defined for asio; skipping header check (note: header-only
libraries have no designated component)
Call Stack (most recent call first):
CMakeLists.txt:27 (find_package)
CMake Error at /snap/cmake/775/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:218 (message):
Could NOT find Boost (missing: asio) (found version "1.74.0")
Call Stack (most recent call first):
/snap/cmake/775/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:582 (_FPHSA_FAILURE_MESSAGE)
/snap/cmake/775/share/cmake-3.19/Modules/FindBoost.cmake:2193 (find_package_handle_standard_args)
CMakeLists.txt:27 (find_package)
-- Configuring incomplete, errors occurred!
See also "/home/vm-umic/Projects/tryout/build/CMakeFiles/CMakeOutput.log".
What in the world am I doing wrong? Isnt CMake telling me that it found Boost 1.74? CMake does NOT throw any errors if I try find_package(Boost REQUIRED), but then linking does also not work. I explicitly tell CMake where to find the libraries, so why can't CMake finde Boost?
Try this.
cmake_minimum_required (VERSION 3.1.0)
# Project name
project (machine_tryout VERSION 1.0)
# Boost (header only)
#set(Boost_DEBUG 1)
set(BOOST_ROOT "$ENV{HOME}/boost/boost_1_74_0")
set(Boost_INCLUDE_DIR "$ENV{HOME}/boost/boost_1_74_0")
set(Boost_LIBRARY_DIR "$ENV{HOME}/boost/boost_1_74_0/libs")
find_package(Boost REQUIRED Components system)
# Set Executable
add_executable(${PROJECT_NAME} source/tryout.cpp)
target_link_libraries(${PROJECT_NAME}
${Boost_LIBRARIES})
I just had a similar problem, and found that I had missed a small but important step in the Boost installation: adding the installation directory (i.e. the PREFIX used in b2 install --prefix=PREFIX) to the PATH environment variable.
For me, this solved it – hopefully will for you too!