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!
Related
I'm trying to link GLEW to my CMake project with little success.
Apparently, it can't find GLEW_LIBRARIES.
I'm using CLion 2019.3.4 and MinGW.
So far I've tried these things:
Defining GLEW_LIBRARIES
Defining GLEW_STATIC_LIBRARIES and GLEW_SHARED_LIBRARIES, as the FindGLEW.cmake file documents that (Line 41 to 45).
Doing both of the above.
Doing 1 and 2 except instead of LIBRARIES it is LIBRARY.
I don't know what else to do.
Heres the CMake lists for reference:
cmake_minimum_required(VERSION 3.15)
project(myProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
set(CMAKE_MODULE_PATH "${CMAKE_HOME_DIRECTORY}/cmake_modules/")
# This part of the code is actually in a separate file,
# called LibrarySetup.cmake
#
# include(LibrarySetup.cmake)
if(WIN32)
set(LIB_PREFIX "")
set(LIB_SUFFIX ".dll")
elseif(UNIX)
set(LIB_PREFIX "lib")
set(LIB_SUFFIX ".lib")
endif()
set(GLFW_INCLUDE_DIR "include/glfw/include/")
set(GLFW_LIBRARY "include/glfw/lib/${LIB_PREFIX}glfw3${LIB_SUFFIX}")
set(GLEW_INCLUDE_DIR "include/glew/include/")
set(GLEW_SHARED_LIBRARIES "include/glew/lib/Release/Win32/glew32.lib")
set(GLEW_STATIC_LIBRARIES "include/glew/lib/Release/Win32/glew32s.lib")
set(GLEW_VERBOSE)
# And here the external file ends.
find_package(GLFW REQUIRED)
find_package(GLEW REQUIRED)
include_directories(${GLFW_INCLUDE_DIR} ${GLEW_INCLUDE_DIRS})
add_subdirectory(include/glfw)
add_executable(myProject main.cpp)
target_link_libraries(myProject ${GLFW_LIBRARY} ${GLEW_LIBRARIES})
And the errors:
~\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\193.6494.38\bin\cmake\win\bin\cmake.exe -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" "~\Documents\CLion Projects\myProject"
CMake Error at ~/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/193.6494.38/bin/cmake/win/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find GLEW (missing: GLEW_LIBRARIES)
Call Stack (most recent call first):
~/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/193.6494.38/bin/cmake/win/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
~/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/193.6494.38/bin/cmake/win/share/cmake-3.15/Modules/FindGLEW.cmake:207 (find_package_handle_standard_args)
CMakeLists.txt:12 (find_package)
-- Configuring incomplete, errors occurred!
See also "~/Documents/CLion Projects/myProject/cmake-build-debug/CMakeFiles/CMakeOutput.log".
[Failed to reload]
The version of CMake your script requires ships with FindGLEW which should do the work of finding the library for you (i.e. set up a Glew target, define include and library paths, etc). You can see the documentation for this module by running:
cmake --help-module findglew
Providing include paths and library definitions of GLEW to your executable should be as simple as:
find_package(GLEW REQUIRED)
add_executable(myProject main.cpp)
target_link_libraries(myProject GLEW::GLEW)
This will provide include and lib paths via the transitive dependency of the GLEW::GLEW target. You should not need to set the paths manually as your example does. The find module will search in the default system locations for the library. If it can't find it you can provide it with a hint via setting the GLEW_ROOT variable to point to your local install location.
set(GLEW_ROOT <my location of GLEW>)
How did you install GLEW? Can you provide an indication of where it is installed on your system and that might make it easier to see why the find module failed?
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
I successfully build my project on locale machine, but when i upload my build in Travis i see this:
CMake Error at /usr/share/cmake-3.2/Modules/FindBoost.cmake:1182
(message):
Unable to find the requested Boost libraries.
Boost version: 1.46.1
Boost include path: /usr/include
Could not find the following static Boost libraries:
boost_log
Some (but not all) of the required Boost libraries were found. You
may
need to install these additional Boost libraries. Alternatively,
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:18 (find_package)
-- Configuring incomplete, errors occurred!
I many time search this error in google, but i not found answer. My CMakeList file this:
cmake_minimum_required(VERSION 3.2)
project(Recast-server)
set(CMAKE_CXX_STANDARD 14)
file(GLOB_RECURSE SOURCE_FILES
"src/*.h"
"src/*.c"
"src/*.cpp")
include_directories(src/headers/)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
add_definitions(-DBOOST_LOG_DYN_LINK)
find_package(Boost 1.46 COMPONENTS system
thread
log
log_setup
program_options
filesystem
REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
add_executable(Recast-server ${SOURCE_FILES})
target_link_libraries(Recast-server ${Boost_LIBRARIES})
target_link_libraries(Recast-server ${CMAKE_THREAD_LIBS_INIT})
You can ask(or try commit) also in this PullRequest: https://github.com/bender-wardrobe/Recast/pull/15
Big Thanks for your answer <3
If I remember correctly Boost.Log was only added in later versions of Boost.
For earlier versions of Boost you can use the standalone Boost.Log from http://boost-log.sf.net
edit
You set the version 1.46 in your CMakeLists.txt. Maybe try a later version. Don't know about Travis.
I am new to C/C++, Cmake, and Boost; I've read every post in SO of other people having my same problem and I couldn't figure it out.
EDIT: as explained in the comment, I already read another similar post, and I'm already doing what is proposed in the accepted solution (i.e. using COMPONENTS libraryName and set(Boost_USE_STATIC_LIBS OFF). Actually, putting Boost_USE_STATIC_LIBS to OFF removed the "lib" prefix from the libraries name in the argn parameter. And I seem to need that prefix..
I built Boost for Android with bjam and, hoping that I have done everything correctly, I used this command:
./b2 install include=/home/myUser/android-ndk-r12b/sources/cxx-stl/gnu-libstdc++/4.9/include include=/home/myUser/android-ndk-r12b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include include=/home/myUser/android-ndk-r12b/platforms/android-19/arch-arm/usr/include toolset=gcc-arm target-os=android --prefix=/home/myUser/boost_build --with-system --with-random --with-date_time -sNO_BZIP2=1 link=static runtime-link=shared threading=multi
And I end up with this folder structure:
/home/myUser/boost_build/
- include/
-boost/
- # a lot of folders and .hpp files
- lib/
- libboost_date_time.a
- libboost_random.a
- libboost_system.a
Then I launch Cmake in order to compile my project with this command:
cmake -DBOOST_ROOT=/home/myUser/boost_build -DBOOST_INCLUDEDIR=/home/myUser/boost_build/include -DBOOST_LIBRARYDIR=/home/myUser/boost_build/lib -DBOOST_VER:STRING=1.61.0 ./
And this is the revelant part of CMakeList.txt:
option(BUILD_SHARED_LIBS "Build the shared library" OFF)
#option(Boost_USE_STATIC_LIBS "Use Boost static version" ON)
set(Boost_USE_STATIC_LIBS ON)
#tried both of the versions above, I don't even know the difference
set(BOOST_VER "1.61.0")
set(Boost_VERSION 106100)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(Boost_FIND_QUIETLY 0 )
set(Boost_DEBUG 1)
find_package(Boost ${BOOST_VER} REQUIRED COMPONENTS system date_time random)
Finally, I end up with this error:
-- [ /usr/share/cmake-3.5/Modules/FindBoost.cmake:1558 ] Boost_FOUND = 1
# e.d.: Notice that Boost_FOUND = 1. Reading through the FindBoost code, I understand that this means that he found the include directory with the header files.
CMake Error at /usr/share/cmake-3.5/Modules/FindBoost.cmake:1719 (message):
# e.d.: don't mind the line numbers in the FindBoost.cmake files... I added a lot of messages around, to debug
Unable to find the requested Boost libraries.
Boost version: 1.61.0
Boost include path: /home/myUser/boost_build/include
Could not find the following static Boost libraries:
boost_system
boost_date_time
boost_random
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:48 (find_package)
If I try to print the find_library parameters, in FindBoost.cmake:
message(STATUS "looking for library: var: ${var} , dollarvar: ${${var}}, argn: ${ARGN}")
find_library(${var} ${ARGN})
message(STATUS "dollarvar: ${${var}}" )
I get:
-- looking for library: var: Boost_SYSTEM_LIBRARY_RELEASE , dollarvar: , argn: NAMES;libboost_system-ghs-mt-1_61;libboost_system-ghs-mt;libboost_system-mt-1_61;libboost_system-mt;libboost_system;HINTS;/home/myUser/boost_build/lib;/home/myUser/boost_build/lib;/home/myUser/boost_build/stage/lib;/home/myUser/boost_build/include/lib;/home/myUser/boost_build/include/../lib;/home/myUser/boost_build/include/../lib/;/home/myUser/boost_build/include/stage/lib;PATHS;C:/boost/lib;C:/boost;/sw/local/lib;NAMES_PER_DIR;DOC;Boost system library (release)
-- dollarvar: Boost_SYSTEM_LIBRARY_RELEASE-NOTFOUND
EDIT2: I tried to copypaste my boost_build directory and my project directory from Linux to Windows, where I installed CMake too, and with my great annoyance... cmake under windows worked. Unfortunately cmake under windows generates some Visual Studio Project files, instead of a makefile. Now I'm wondering why cmake version 3.5.1 under Ubuntu 16.04 doesn't work, and cmake version 3.6.2 under Windows 7 does.
I am kind of desperate:
For my studies I need to work with Eigen and CMake. I'm able to use Eigen if I copy the whole library in the directories where my compiler looks by default but as soon as I try to find it via
find_package(Eigen3 REQUIRED)
I get the following error:
CMake Error at /usr/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
Could NOT find Eigen3 (missing: EIGEN3_INCLUDE_DIR EIGEN3_VERSION_OK)
(Required is at least version "2.91.0")
Call Stack (most recent call first):
/usr/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:388 (_FPHSA_FAILURE_MESSAGE)
FindEigen3.cmake:76 (find_package_handle_standard_args)
CMakeLists.txt:8 (find_package)
-- Configuring incomplete, errors occurred!
Now I searched for solutions but all I those I tried (also those available on stackoverflow:
Find package Eigen3 for CMake
or
CMake Can't find Eigen3 )
did not work.
My Eigen Version (according to the Macros in Core/util/Macros.h) is 3.2.5.
I keep the Eigen directory in /usr/local/include, I use the FindEigen3.cmake which comes with the Eigen library and my CMakeLists.txt looks as follows:
cmake_minimum_required(VERSION 2.8)
project(Test)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR})
message("Found Eigen3 in: ${EIGEN3_INCLUDE_DIR}")
add_executable(main test.cpp)
Has anyone an idea what's going wrong?
Kind regards,
Julien
Turning my comment into an answer
The find package scripts - like FindEigen3.cmake - normally use the find_path() command to detect the package's include directory (see it's documentation for the full details).
FindEigen3.cmake uses the following code snippet:
find_path(EIGEN3_INCLUDE_DIR NAMES signature_of_eigen3_matrix_library
PATHS
${CMAKE_INSTALL_PREFIX}/include
${KDE4_INCLUDE_DIR}
PATH_SUFFIXES eigen3 eigen
)
So it looks in CMAKE_INSTALL_PREFIX which on Unix/Linux hosts is /usr/local by default.
The following has worked for me:
Go to the Eigen source directory and run the CMake and installation steps
> mkdir build
> cd build
> cmake ..
> make install
Then copy - as you have done - FindEigen3.cmake to your projects source directory.
Now your code does find Eigen (just changed to list(APPEND ...))
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
find_package(Eigen3 REQUIRED)
References
Eigen & CMake
CMake: Of what use is find_package() if you need to specify CMAKE_MODULE_PATH anyway?
Cmake doesn't find Boost
Add the path of FindEigen3.cmake before find_package(Eigen3 REQUIRED), like this:
LIST(APPEND CMAKE_MODULE_PATH "/usr/share/cmake-2.8/Modules/")
find_package(Eigen3)