I am new to C++ and want to include the boost library (specifically the filesystem part which needs to be built) in my project. I tried many solutions from other stackoverflow users but they didn't help me at all. I am using CLion with CMake.
The main.cpp is calling the other .cpp files inside the modules/ folder.
The file structure:
ProjectName
>boost
>lots of folders and .hpp files
>cmake-build-debug
>modules
encryption.cpp
encryption.h
output.cpp
output.h
CMakeLists.txt
main.cpp
The boost folder doesn't contain the entirety of boost when you download and extract it. I dragged the boost folder inside of boost_1_72_0 in my project (just so you know that there's no libs folder, etc. inside)
The CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(ProjectName)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc")
set(SOURCE_FILES
main.cpp
modules/encryption.cpp modules/encryption.h modules/output.cpp modules/output.h
)
set(Boost_ARCHITECTURE -x64)
set(BOOST_ROOT boost/)
set(Boost_INCLUDE_DIRS boost/filesystem)
find_package(Boost COMPONENTS system filesystem REQUIRED)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(ProjectName ${SOURCE_FILES})
target_link_libraries(ProjectName ${Boost_LIBRARIES})
output.cpp
// some includes //
#define BOOST_FILESYSTEM_NO_DEPRECATED
#include "../boost/filesystem.hpp"
// some code //
The Error message:
CMake Error at C:/Program Files/JetBrains/CLion 2019.1.4/bin/cmake/win/share/cmake-3.14/Modules/FindBoost.cmake:2147 (message):
Unable to find the requested Boost libraries.
Unable to find the Boost header files. Please set BOOST_ROOT to the root
directory containing Boost or BOOST_INCLUDEDIR to the directory containing
Boost's headers.
Call Stack (most recent call first):
CMakeLists.txt:15 (find_package)
-- Configuring incomplete, errors occurred!
See also "C:/Users/username/Desktop/C++/ProjectName/cmake-build-debug/CMakeFiles/CMakeOutput.log".
mingw32-make.exe: *** [cmake_check_build_system] Error 1
Makefile:235: recipe for target 'cmake_check_build_system' failed
I know that it's basically telling me what I have to do but I don't know what's exactly meant by the "root directory" of boost, by the directory "containing Boost's headers" and how to put everything together.
Many thanks in advance!
I dragged the boost folder inside of boost_1_72_0 in my project
Looks like you just copied boost source into your project dir.
You have to compile boost since you need filesystem. Or you can get boost from:
vcpkg - it's the easiest way for you. I am highly recommended this way.
Sourceforge.
Conan
I don't know what's exactly meant by the "root directory"...
Since you are using boost by calling find_package(Boost) - CMake uses FindBoost module. It will try to find your boost installation inside system PATH variable or in some other "standard" places. Your boost "installation" is not common, so you have to specify where boost is with BOOST_ROOT variable. set(BOOST_ROOT boost/) is incorrect way to do this. You have to specify absolute path like set(BOOST_ROOT "C:/lib/boost/boost17.2")
or relative to current CMakeList.txt - set(BOOST_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/boost}.
With right installed boost all you have to do is:
find_package(Boost REQUIRED [COMPONENTS <libs>...])
target_link_libraries(main PRIVATE ${Boost_LIBRARIES})
target_include_directories(main PRIVATE ${Boost_INCLUDE_DIRS})
Usually, you don't need to set Boost_ARCHITECTURE and Boost_INCLUDE_DIRS CMake does it for you.
When you use find_package with REQUIRED option you don't need to check whether the library found or not since CMake raises an error when a library isn't found.
BOOST_ROOT is a directory when boost installed or unpacked.
BOOST_INCLUDEDIR is a directory with boost headers (usually it's BOOST_ROOT/boost). So try to set the full path to your boost_1_72_0 directory to BOOST_ROOT CMake variable.
Also, I had a problem with COMPONENTS option. Try to remove it if errors remain.
Related
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.
I am trying to make a self contained library whose dependencies are self contained.All installs are installed to <repo_dir>/libs//build.
To find dependencies in cmake build system I use find_package. Because all installs are in a custom location I point I use HINTS to help out like so -
# CMake 3.16.3
# ../lib/Catch2/build is where Catch2Config.cmake is located
find_package(Catch2 REQUIRED HINTS "../lib/Catch2/build" NO_MODULE)
include_directories(tests)
add_executable(tests tests.cpp)
target_link_libraries(tests PRIVATE project_warnings project_options Catch2::Catch2)
I recognize that Catch2 likely doesn't need this as it is header only, however I am trying to learn what cmake is doing.
The CMakeLists.txt above prints out
include could not find load file:
<repo_dir>/lib/Catch2/build/Catch2Targets.cmake │
Call Stack (most recent call first):
tests/CMakeLists.txt:1 (find_package)
It looks like a target file is needed as well. I cannot find one in the Catch2 repo.
I am also trying the same process for folly, however it does not work for me either.
Using the following in module mode for Boost worked. Why does it work?
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${PROJECT_SOURCE_DIR}/lib/boost_1_75_0/")
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "${PROJECT_SOURCE_DIR}/lib/boost_1_75_0/libs")
find_package(Boost 1.60.0 REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(${CMAKE_PROJECT_NAME} ${Boost_LIBRARIES})
Could someone help to uncover my problems and how I should go about using
Header only libraries
Static lib binaries
Self-Compiled Libraries
Whose resources are in unusual places?
I am currently migrating a couple of CodeBlock projects to use CMake and Visual Studio Code. I am facing an issue related to the include directory not being known by the consuming target.
I followed a guide so far as I was not aware of the "modern CMake way" using targets at all until yet. Mainly referring to here: https://rix0r.nl/blog/2015/08/13/cmake-guide/ and the official CMake docs.
There are two libraries (.so). One of them needs to other one to be there (build) when it get's loaded by the application later on.
I am not sure if everything is even intended to work like I assume it to work so I will just continue with the code and the expected result.
Library A (which is needed by Library B) has following CMakeLists
cmake_minimum_required(VERSION 3.0.0)
project(A)
set(SOURCE somesource.c)
include(GNUInstallDirs)
find_package(JNI REQUIRED)
add_library(JNI SHARED IMPORTED)
set_property(TARGET JNI PROPERTY INTERFACE_INCLUDE_DIRECTORIES $ENV{JAVA_HOME}/include $ENV{JAVA_HOME}/include/linux)
set_property(TARGET JNI PROPERTY IMPORTED_LOCATION ${JNI_LIBRARIES})
add_library(${PROJECT_NAME} SHARED ${SOURCE})
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include $ENV{JAVA_HOME}/include $ENV{JAVA_HOME}/include/linux>
$<INSTALL_INTERFACE:include>
)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}Config
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(EXPORT ${PROJECT_NAME}Config DESTINATION ${CMAKE_INSTALL_PREFIX}/cmake)
Notice I only have one include file which is public and located in "include", so its used by the library itself but should also be part of the install interface
used by the "consuming" target.
Library B (which uses Library A and its header)
cmake_minimum_required(VERSION 3.0)
project(B)
set(SOURCE somesource.c)
add_library(${PROJECT_NAME} SHARED ${SOURCE})
find_package(A REQUIRED)
target_include_directories(${PROJECT_NAME}
PRIVATE ${PROJECT_SOURCE_DIR}
)
So in Library B I assume that I do not need to add the include_directories from Library A again. This is why I actually did the target approach so there is no need to keep track of linker/include dependencies. "find_package" should handle these on it's own right? I do not link neither because it is a shared object, not a static library.
Both libraries use the same CMAKE_INSTALL_PREFIX ofc and library A has been installed (make install). I double-checked on this. In case of using a different prefix or not doing the install, CMake already complains about not finding it.
So considering the issue now:
Within a source file from library B, I am using
#include <libcobjava.h>
resulting in an error when building as the compiler complains about "No such file or directory" Am I missing something? The file is definitely located at
CMAKE_INSTALL_PREFIX/include.
Apart from that as I am still new to CMake: Is this even the way you would do things if you own all the source?
Edit: Some more details on the follow-up issue as discussed in comments. Library A is libcobjava.
First I had the target_include_directories part as following (including the path where jni.h is located in $<INSTALL_INTERFACE:)
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include $ENV{JAVA_HOME}/include $ENV{JAVA_HOME}/include/linux>
$<INSTALL_INTERFACE:include $ENV{JAVA_HOME}/include $ENV{JAVA_HOME}/include/linux>
)
results in following error:
[cmake] Configuring done
[cmake] CMake Error in CMakeLists.txt:
[cmake] Target "libcobjava" INTERFACE_INCLUDE_DIRECTORIES property contains path:
[cmake]
[cmake] "/media/rbs42/data/Gebos/RBS42/tools/libcobjava/${_IMPORT_PREFIX}/include"
[cmake]
[cmake] which is prefixed in the source directory.
[cmake]
[cmake]
[cmake] Generating done
[cms-driver] Error during CMake configure: [cmake-server] Failed to compute build system.
This is why I removed $ENV{JAVA_HOME}/include $ENV{JAVA_HOME}/include/linux> from $<INSTALL_INTERFACE again.
The actual error I am getting in library B now is during compiling:
[build] /media/rbs42/data/rbs42/usr/include/libcobjava.h:1:10: fatal error: jni.h: No such file or directory
[build] #include <jni.h>
because library B does not have the include directories from library A that are not included in the $<INSTALL_INTERFACE. This is what one would expect. And thats the reason I added the following code before in library A.
set_property(TARGET JNI PROPERTY INTERFACE_INCLUDE_DIRECTORIES $ENV{JAVA_HOME}/include $ENV{JAVA_HOME}/include/linux)
which I assumed is taking care of propagating the include dependencies from library A to the "consuming" library B, although they are not part of the $<INSTALL_INTERFACE.
I am not a C++ programmer, only have made a course a while ago. Using homebrew I installed libbitcoin and was hoping that I can reference the library like I was able to reference the boost libraries. I also realized that there are no links in /usr/local/bin to the Cellar.
I think I could get it working by using the absolute paths but I am looking for the proper way of handling this constellation that I just mentioned.
Current CMake:
cmake_minimum_required(VERSION 2.8.4)
project(cplusplus)
message(STATUS "start running cmake...")
find_package(boost 1.65.1 COMPONENTS system filesystem REQUIRED)
find_package(libbitcoin 3.3.0 COMPONENTS system filesystem REQUIRED)
message("system: ${CMAKE_SYSTEM_PREFIX_PATH}")
find_library(LIB_BITCOIN libbitcoin)
message("bitcoin: ${LIB_BITCOIN}")
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(cplusplus main.cpp)
if(Boost_FOUND)
target_link_libraries(cplusplus ${Boost_LIBRARIES})
endif()
Currently I get these errors:
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /Users/johndow/Documents/Workspace/bitcoin-code/cplusplus
-- start running cmake...
-- Boost version: 1.65.1
CMake Error at CMakeLists.txt:8 (find_package):
By not providing "Findlibbitcoin.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"libbitcoin", but CMake did not find one.
Could not find a package configuration file provided by "libbitcoin"
(requested version 3.3.0) with any of the following names:
libbitcoinConfig.cmake
libbitcoin-config.cmake
Add the installation prefix of "libbitcoin" to CMAKE_PREFIX_PATH or set
"libbitcoin_DIR" to a directory containing one of the above files. If
"libbitcoin" provides a separate development package or SDK, be sure it has
been installed.
-- Configuring incomplete, errors occurred!
See also "/Users/johndoe/Documents/Workspace/bitcoin-code/cplusplus/cmake-build-debug/CMakeFiles/CMakeOutput.log".
[Finished]
You seem to have double lookup for libbitcoin library in your CMakeLists file. You are first looking for it by:
find_package(libbitcoin ...)
and then by
find_library(LIB_BITCOIN libbitcoin)
Cmake is not happy (as your error message says) with the find_package() clause as libbitcoin does not provide cmake configuration by itself. You have many ways how to fix it, just two of them:
remove find_package() and use only find_library(), I think this is the simpler way and your project should work this way
provide cmake configuration for libbitcoin by yourself. Good introduction how to do this is here (and good to read anyway):
https://cmake.org/Wiki/CMake:How_To_Find_Libraries
As far as I know, currently libbitcoin does not export any <libbitcoin>Config.cmake package.
But it does export a libbitcoin.pc file for generic use with pkg-config.
ie: /usr/local/lib/pkgconfig/libbitcoin.pc
If you get results from invoking pkg-config --cflags libbitcoin then it's there.
And then you can put something like this in your CMakeLists.txt:
#use this if libbitcoin is installed to some custom location
set(ENV{PKG_CONFIG_PATH} "/path/to/libbitcoin/pkgconfig/:$ENV{PKG_CONFIG_PATH}")
#then later..
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIB_BITCOIN REQUIRED libbitcoin)
#then later..
target_link_libraries(${PROJECT_NAME} PRIVATE ${LIB_BITCOIN_LIBRARIES})
target_include_directories(${PROJECT_NAME} PRIVATE ${LIB_BITCOIN_INCLUDE_DIRS})
That should pull in boost, make the libbitcoin includes visible and solve all manner of compiler and linker woes.
(Or if you are feeling mad, you could always make use of this gist).
I am trying to compile mlpack (http://www.mlpack.org/) which requires boost.
First I installed boostpro 1.51 (http://www.boostpro.com/download/) and boost is now installed in C:\Program Files\boost\boost_1_51
Then I compiled and installed armadillo (another dependancy of mlpack) using cmake and mingw32-make. Boost is also a dependency of armadillo. Following some advises (Cmake doesn't find Boost), I added to the CMakeLists file:
SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "C:\\Program Files\\boost\\boost_1_51")
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "C:\\Program Files\\boost\\boost_1_51\\lib")
Things went smoothly.
Now I am trying to run CMake on mlpack but get this error:
CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/FindBoos
t.cmake:1192 (message):
Unable to find the requested Boost libraries.
Boost version: 1.51.0
Boost include path: C:/Program Files/boost/boost_1_51
The following Boost libraries could not be found:
boost_program_options
boost_unit_test_framework
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:192 (find_package)
I tried to add (as for armadillo), but no improvement:
SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "C:\\Program Files\\boost\\boost_1_51")
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "C:\\Program Files\\boost\\boost_1_51\\lib")
Then I tried to add as well:
set(BOOST_ROOT "C:\\Program Files\\boost\\boost_1_51")
set(BOOST_INCLUDEDIR "C:\\Program Files\\boost\\boost_1_51")
set(BOOST_LIBRARYDIR "C:\\Program Files\\boost\\boost_1_51\\lib")
For the BOOST_INCLUDEDIR, I really do not know which folder I should point too. boost_1_51 contains: bin, boost, dist, doc, lib, libs, more
I also tried this (cmake is using the wrong cboost libs), without success.
I am also a little confused as armadillo seemed to have compiled without issues (or I missed something ?)
Many thanks
Note: content of the cmakelists file:
#Unfortunately this configuration variable is necessary and will need to be
#updated as time goes on and new versions are released.
set(Boost_ADDITIONAL_VERSIONS "1.41" "1.41.0" "1.42" "1.42.0" "1.43" "1.43.0" "1.44" "1.44.0" "1.45.0" "1.46.0" "1.46.1" "1.47.0" "1.48.0" "1.49.0" "1.51.0")
find_package(Boost
COMPONENTS
program_options
unit_test_framework
REQUIRED
)
include_directories(${Boost_INCLUDE_DIRS})
I added 1.51.0 myself.
from what I understood from another post (CMake not finding Boost) this might be related to not finding libraries with correct names. In my lib folder I have (dll and lib files):
boost_program_options-vc80-mt-1_51.dll
boost_program_options-vc80-mt-gd-1_51.dll
boost_program_options-vc100-mt-1_51.dll
boost_program_options-vc100-mt-gd-1_51.dll
somehow I am supposed to play with commands like
set(Boost_USE_MULTITHREADED ON)
so that it will look for the lib with the expected name ????