I wanted to use DearImGui therefore I needed to either copy ImGui into the project or use a package manager, so I chose the latter. I'm currently using Conan as a my package manager, the file looks like this:
conanfile.txt
[requires]
boost/1.79.0
imgui/1.88
glad/0.1.36
glfw/3.3.7
[generators]
cmake_find_package
cmake_paths
CMakeDeps
CMakeToolchain
It has all the dependancies as a normal ImGui project would need as well as boost which had been downloaded as a binary previously and works fine, using just a normal header like #include <boost/asio.hpp>. I figure this happens because it was aleady installed.
I have a basic file structure that looks like this, prebuild:
conanfile.txt
CMakeLists.txt
src -
main.cpp
With this main CMakeLists I set up a linker with conan and simple setup instructions like the project name, executable, and the libraries
cmake_minimum_required(VERSION 3.23)
project(RenderCam CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(${CMAKE_CURRENT_SOURCE_DIR}/build/conan_paths.cmake)
add_executable(${PROJECT_NAME} src/main.cpp)
find_package(Boost 1.79.0 REQUIRED COMPONENTS thread system filesystem)
if(TARGET boost::boost)
target_link_libraries(${PROJECT_NAME} boost::boost)
endif()
find_package(glfw3 3.3 REQUIRED)
if(TARGET glfw)
message("Found GLFW")
target_link_libraries(${PROJECT_NAME} glfw)
endif()
find_package(imgui 1.88 REQUIRED)
if(TARGET imgui::imgui)
message("found imgui")
target_link_libraries(${PROJECT_NAME} imgui::imgui)
endif()
After running two commands (conan install .. and cmake .. -DCMAKE_BUILD_TYPE=Release) in the new build directory the CMake build responds with all the found messages for the project as well as the location of the files, which should be expected. Though when add the #include <imgui.h> to the main.cpp it states the file is not found which shouldn't be if I have linked it. On the other hand #include <boost/asio.hpp> has no errors and I can go upon my day with all of the commands. I would just like to include the directories and have tried multiple steps and guides before I took it here. For further reference, this is my conanprofile :
Configuration for profile default:
[settings]
os=Macos
os_build=Macos
arch=x86_64
arch_build=x86_64
compiler=apple-clang
compiler.version=13
compiler.libcxx=libc++
build_type=Release
[options]
[conf]
[build_requires]
[env]
Don't mix up mutually exclusive generators like cmake_find_package vs CMakeDeps, or cmake_paths vs CMakeToolchain.
Here is a basic example of non-intrusive integration of conan:
conanfile.txt
[requires]
boost/1.79.0
imgui/1.88
glfw/3.3.7
[generators]
CMakeToolchain
CMakeDeps
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(RenderCam CXX)
find_package(Boost 1.79.0 REQUIRED thread system filesystem)
find_package(glfw3 3.3 REQUIRED)
find_package(imgui 1.88 REQUIRED)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE Boost::headers Boost::thread Boost::system Boost::filesystem glfw imgui::imgui)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14)
Install dependencies, configure & build:
mkdir build && cd build
conan install .. -s build_type=Release -b missing
cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
cmake --build . --config Release
Related
Context:
I have a cpp program built on MacOS 12.6 with the following CMakeLists.txt file.
cmake_minimum_required(VERSION 3.19.0)
project(cpp-test VERSION 0.1.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
add_executable(cpp-test main.cpp)
add_library(test-helpers main.cpp ${PROJECT_SOURCE_DIR}/helpers.hpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
# this is super important in order for cmake to include the vcpkg search/lib paths!
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
# find library and its headers
find_path(IXWEBSOCKET_INCLUDE_DIR ixwebsocket/IXWebSocket.h)
find_library(IXWEBSOCKET_LIBRARY ixwebsocket)
find_package(OpenSSL REQUIRED)
find_package(CURL REQUIRED)
# include headers
include_directories(${IXWEBSOCKET_INCLUDE_DIR} ${CURL_INCLUDE_DIR})
# Cmake will automatically fail the generation if the lib was not found, i.e is set to NOTFOUND
target_link_libraries(
${PROJECT_NAME} PRIVATE
${IXWEBSOCKET_LIBRARY}
OpenSSL::SSL
OpenSSL::Crypto
${CURL_LIBRARIES}
"-framework Foundation"
"-framework Security"
"-lz"
)
This compiles just fine. However, when I try to pull it into my Ubuntu VM and try to build it /build> cmake .., I get the following errors
CMake Error in CMakeLists.txt:
Found relative path while evaluating include directories of "cpp-test":
"IXWEBSOCKET_INCLUDE_DIR-NOTFOUND"
CMake Error in CMakeLists.txt:
Found relative path while evaluating include directories of
"test-helpers":
"IXWEBSOCKET_INCLUDE_DIR-NOTFOUND"
-- Generating done
What I have tried...
I have installed vcpkg and created my symlink ln -s /path/to/vcpkg /usr/local/bin/vcpkg.
I have installed ixwebsocket via vcpkg install ixwebsocket, but it seems that the CMAKE_TOOLCHAIN_FILE is not being parsed correctly.
I'm a bit lost, any help would be appreciated
This is not a great answer to the issue, but I ended up resolving it by building ixwebsocket via CMake instead.
It seems that vcpkg was not compatible with the linux distro in my VM.
Edit: Solution at bottom
Here is my CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(game)
set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp
)
# Conan Requirements
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
add_executable(game ${SOURCES})
find_package(SDL2 REQUIRED)
find_package(sol2 REQUIRED)
target_compile_options( game PRIVATE
$<$<CONFIG:Debug>:
-fsanitize=address,leak
>
)
target_link_options( game PRIVATE
$<$<CONFIG:Debug>:
-fsanitize=address,leak
>
)
target_compile_features(game
PRIVATE
cxx_std_20
)
target_link_libraries(game PRIVATE
SDL2::SDL2
sol2::sol
)
Here is my conanfile.txt
[requires]
lua/5.4.4
sdl/2.0.20
[generators]
cmake
cmake_find_package
CMakeDeps
CMakeToolchain
I get the following error:
CMake Error at CMakeLists.txt:16 (find_package):
By not providing "Findsol2.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "sol2", but
CMake did not find one.
Could not find a package configuration file provided by "sol2" with any of
the following names:
sol2Config.cmake
sol2-config.cmake
Add the installation prefix of "sol2" to CMAKE_PREFIX_PATH or set
"sol2_DIR" to a directory containing one of the above files. If "sol2"
provides a separate development package or SDK, be sure it has been
installed.
I am building using the following methods:
conan install . --install-folder build --build=missing
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=build/conan_toolchain.cmake -S . -B build -GNinja
cmake --build build
It's odd because I'm only getting the error about sol2 and not SDL
edit: SOLUTION IS:
CMakeLists.txt file
cmake_minimum_required(VERSION 3.20)
project(game)
set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp
)
# Conan Requirements
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
add_executable(game ${SOURCES})
find_package(SDL2 REQUIRED)
find_package(sol2 REQUIRED)
target_compile_options( game PRIVATE
$<$<CONFIG:Debug>:
-fsanitize=address,leak
>
)
target_link_options( game PRIVATE
$<$<CONFIG:Debug>:
-fsanitize=address,leak
>
)
target_compile_features(game
PRIVATE
cxx_std_20
)
target_link_libraries(game PRIVATE
SDL2::SDL2
sol2::sol2
)
conanfile.txt is
[requires]
sol2/3.3.0
sdl/2.0.20
[generators]
cmake
cmake_find_package
CMakeDeps
CMakeToolchain
build script is
#!/usr/bin/sh
conan install . --install-folder build --build=missing
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=build/conan_toolchain.cmake -S . -B build -GNinja
I'm trying to build a c++ library, which will be using itself another library.
I would like to output at the end a single .so file, so it is easily copied and used in any other project.
In this library I am using another library, GLFW.
Now, I can create my library fine, but when I am using it I am getting linking errors, where the GLFW functions are not defined. This makes me think that the GLFW lib is not exported with my library.
I've seen this that seemed to be a solution, but i gave me lot of duplicate symbol errors.
I'm quite a beginner with cmake, so maye there is something obvious I'm not seeing. Here is my CMakeLists.txt :
cmake_minimum_required(VERSION 3.22)
project(MyLib)
set(CMAKE_CXX_STANDARD 23)
# define folders path
get_filename_component(ROOT_DIR ${CMAKE_CURRENT_LIST_FILE} PATH)
set(HEADER "${ROOT_DIR}/include")
set(SRCS_PATHS "${ROOT_DIR}/src")
set(TESTS_SRC "${ROOT_DIR}/tests")
# add dependencies
set(DEP_HEADERS "${ROOT_DIR}/dependencies/GLFW/include")
# set the project sources and headers files
include_directories(${HEADER})
include_directories(${DEP_HEADERS})
set(SRCS [...])
add_library(MyLib SHARED ${SRCS})
# set the project property linker language
set_target_properties(MyLib PROPERTIES LINKER_LANGUAGE CXX)
# target tests
add_executable(window ${TESTS_SRC}/window.cpp)
target_link_libraries(window MyLib)
I've seen I'm not the only one with this issue, but most of the answers I've tried won't work and lead to the same problem.
From what I can deduce from your CMakeLists.txt, you should do something like this (I don't like vendoring, not an expert of this approach, so maybe there is something more elegant):
cmake_minimum_required(VERSION 3.20)
project(MyLib)
# glfw static PIC
set(CMAKE_POSITION_INDEPENDENT_CODE_SAVED ${CMAKE_POSITION_INDEPENDENT_CODE})
set(BUILD_SHARED_LIBS_SAVED ${BUILD_SHARED_LIBS})
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(dependencies/GLFW EXCLUDE_FROM_ALL)
set(CMAKE_POSITION_INDEPENDENT_CODE ${CMAKE_POSITION_INDEPENDENT_CODE_SAVED})
set(BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS_SAVED})
# MyLib
add_library(MyLib SHARED [...])
target_include_directories(MyLib PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
target_link_libraries(MyLib PRIVATE glfw)
target_compile_features(MyLib PUBLIC cxx_std_23)
# Tests
add_executable(window tests/window.cpp)
target_link_libraries(window PRIVATE MyLib)
target_compile_features(MyLib PRIVATE cxx_std_23)
But honestly it's bad to hardcode all these informations in a CMakeLists, you should have a generic CMakeLists and avoid vendoring:
cmake_minimum_required(VERSION 3.20)
project(MyLib)
# MyLib
find_package(glfw3 REQUIRED)
add_library(MyLib [...])
target_include_directories(MyLib PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
target_link_libraries(MyLib PRIVATE glfw)
target_compile_features(MyLib PUBLIC cxx_std_23)
# Tests
add_executable(window tests/window.cpp)
target_link_libraries(window PRIVATE MyLib)
target_compile_features(MyLib PRIVATE cxx_std_23)
And then you would decide at build time how to build each lib:
// build & install glfw once, as static PIC (glfw is not vendored in MyLib source code here)
cd <glfw_source_dir>
cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=<glfw_install_dir>
cmake --build build
cmake --build build --target install
// build MyLib as shared
cd <mylib_source_dir>
cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DCMAKE_PREFIX_PATH=<glfw_install_dir>
cmake --build build
I'm trying to build a C++ project with Conan to download the libraries, but the problem is that I can't build it since Asio throw error on it's own functions.
Here is the result of the build : https://pastebin.com/WQ2bFHvm
I'm going to share you my configuration :
//Conan Profile
[settings]
arch=x86_64
arch_build=x86_64
build_type=Debug
compiler=apple-clang
compiler.libcxx=libc++
compiler.version=12.0
os=Macos
os_build=Macos
[options]
[build_requires]
[env]
conanfile.txt
//conanfile.txt
[requires]
asio/1.20.0
boost/1.77.0
libbacktrace/cci.20210118
sfml/2.5.1#bincrafters/stable
[options]
libstd:compiler.libcxx=libstdc++11
[generators]
cmake
cmake_find_package
CMakeLists.txt :
//CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(R-Type)
# conan
set(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
include(build/conanbuildinfo.cmake)
conan_basic_setup()
# Package
find_package(asio 1.20.0 REQUIRED)
find_package(Boost 1.77.0 REQUIRED)
find_package(SFML 2.5.1 COMPONENTS graphics window system REQUIRED)
# .cmake files
include(./rtype/client/build.cmake)
include(./rtype/server/build.cmake)
include(./ecs-engine/build.cmake)
include(./graphics/build.cmake)
set(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
include(build/conanbuildinfo.cmake)
conan_basic_setup()
include_directories(${CMAKE_INCLUDE_PATH})
find_package(asio 1.19.2 REQUIRED)
find_package(SFML 2.5.1 COMPONENTS window graphics system audio REQUIRED)
set(ECS_ENGINE_DIR ./ecs-engine/)
set(GRAPHICS_DIR ./graphics/)
set(RTYPE_CLIENT_DIR ./rtype/client/)
set(RTYPE_SERVER_DIR ./rtype/server/)
build_engine()
set(ECS_ENGINE_INCLUDES INCLUDE_DIR)
build_graphics()
build_server()
build_client()
Server cmake build :
//build.cmake
set(RTYPE_SERVER_EXECUTABLE rtype_server)
set(RTYPE_SERVER_DIR ./rtype/server/)
macro(build_server)
set(SOURCE_DIR ${RTYPE_SERVER_DIR}/sources)
set(INCLUDE_DIR ${RTYPE_SERVER_DIR}/includes)
include_directories(${CMAKE_INCLUDE_PATH} ${INCLUDE_DIR})
add_executable(${RTYPE_SERVER_EXECUTABLE}
${SOURCE_DIR}/main.cpp
${SOURCE_DIR}/UDP.cpp
)
target_link_libraries(${RTYPE_SERVER_EXECUTABLE} ${CONAN_LIBS})
endmacro()
And finally here are the commands I run to build the project :
rm -rf build
mkdir build && cd build
conan install .. --build=missing
cmake .. -G "Unix Makefiles"
cmake --build .
I've tried to install the libraries locally but it does not work, do you guys have any idea on what I'm missing ?
I'm setting up a c++ project, which uses cmake and has two build type:
debug, having a cmake-build-debug folder
release, having a cmake-build-release folder
I want to add plog through cmake, so I installed it successfully by defining a conanfile.txt:
[requires]
plog/1.1.5
[generators]
cmake
then, I conan install . on my root and I edited my CMakeLists.txt like this:
set(CMAKE_VERBOSE_MAKEFILE ON)
cmake_minimum_required(VERSION 3.17)
project(test)
set(CMAKE_CXX_STANDARD 20)
set(CXX_EXTENSIONS OFF)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(LLVM_ENABLE_WARNINGS ON)
endif()
# ADDED ROW
include(conanbuildinfo.cmake)
add_executable(primo main.cpp io.cpp io.h)
# ADDED ROW
target_link_libraries(plog ${CONAN_LIBS})
the problem is that cmake complaints about the last row:
CMake Error at CMakeLists.txt:15 (target_link_libraries):
Cannot specify link libraries for target "plog" which is not built by this
project.
I'm new to cmake so maybe I've configured it badly. What seems to be the problem?