Root
CMakeLists.txt
Valyrie/
CMakeLists.txt
Submodules/
CMakeLists.txt
SubmodulePackage/
CMakeLists.txt
I have a directory structure listed like above. However even if I add_subdirectory("submodules") so the submodule packages get added first cmake won't find the packages with find_package inside the Valkyrie cmakelists.txt. Is there a way to do that? I'd do relative path, but some submodules don't seem to get brought in correctly by just relative pathing their header files for some reason (header-only library).
Root Level Cmake
cmake_minimum_required (VERSION 3.8)
project (Valkyrie)
# Include sub-projects.
add_subdirectory("submodules")
add_subdirectory("Valkyrie")
Submodules CMake
cmake_minimum_required (VERSION 3.8)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory("json")
add_subdirectory("spdlog")
add_subdirectory("glfw")
add_subdirectory("entt")
add_subdirectory("glm")
Valkyrie
cmake_minimum_required (VERSION 3.8)
find_package(Vulkan REQUIRED)
find_package(spdlog REQUIRED) #Error here
file(GLOB_RECURSE sources CONFIGURE_DEPENDS "*.cpp")
file(GLOB_RECURSE headers CONFIGURE_DEPENDS "*.h")
add_library(Valkyrie ${headers} ${sources})
target_compile_features(Valkyrie PUBLIC cxx_std_20)
target_include_directories(Valkyrie
PUBLIC "../submodules/glfw/include"
PUBLIC "../submodules/glm"
PUBLIC "../submodules/entt/single_include"
PUBLIC "../submodules/spdlog/include"
PUBLIC ${Vulkan_INCLUDE_DIRS}
)
target_link_libraries(Valkyrie
"${CMAKE_BINARY_DIR}/submodules/glfw/src/glfw3.lib"
${Vulkan_LIBRARIES}
)
Related
I was checking the quickstarter to use googletest with cmake. I was wondering how I should modify the CMakeLists.txt they provide (given below) in order to use googlemock.
cmake_minimum_required(VERSION 3.14)
project(my_project)
# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()
add_executable(
hello_test
hello_test.cc
)
target_link_libraries(
hello_test
GTest::gtest_main
)
include(GoogleTest)
gtest_discover_tests(hello_test)
for the moment, #include <gtest/gtest.h> works but #include <gmock/gmock.h> doesn't
GMock should be available via GTest::gmock cmake target. At least this is the target name used in the cmake configuration files of a GTest build installed via cmake.
target_link_libraries(
hello_test
GTest::gmock
)
when building a project stucture like following:
LinearAlgebra
|---HEADER
|-----|---Linear.h
|---SRC
|-----|---Linear.cpp
|---TEST
|-----|---hello_test.cc
here is my CMakeList.txt:
cmake_minimum_required(VERSION 3.10)
project(LinearAlgebra )
set(CMAKE_CXX_STANDARD 11)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
add_library(LA_HEADER INTERFACE header/Linear.h)
add_library(LA_SRC INTERFACE src/Linear.cpp)
target_include_directories(LA_HEADER INTERFACE "${PROJECT_SOURCE_DIR}/header" "${PROJECT_SOURCE_DIR}/src")
enable_testing()
add_executable(
hello_test
test/hello_test.cc
)
target_link_libraries(
hello_test
gtest_main
LA_HEADER
LA_SRC
)
include(GoogleTest)
gtest_discover_tests(hello_test)
it works fine with cmake -S . -B build, but when it comes to cmake --build build following error occured:
Undefined symbols for architecture x86_64:
"Linear::Linear()", referenced from:
HelloTest_BasicAssertions_Test::TestBody() in hello_test.cc.o
it seems something wrong with my cmake file that didn't tell Linear.h where Linear.cpp located. So how to fix it ?? thanks for your help...
seems i misunderstand add_library, change three lines and it works out:
cmake_minimum_required(VERSION 3.10)
project(LinearAlgebra )
include_directories(header) # include_directories instead of add_library for .h files
add_library(LA_LIB src/Linear.cpp) # use add_library for .cpp files only!!!
set(CMAKE_CXX_STANDARD 11)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()
add_executable(
hello_test
test/hello_test.cc
)
target_link_libraries(
hello_test
gtest_main
LA_LIB # link LA_LIB only..
)
include(GoogleTest)
gtest_discover_tests(hello_test)
leave it here for anyone have the same requirements.
I have tried following this solution this individual has from this question. I am trying to do this with boost with the following cmake file in a separate directory from my Source:
#---------------------------------------------------------------------------
# Get and build boost
SET_PROPERTY(DIRECTORY PROPERTY "EP_BASE" ${ep_base})
SET(boost_GIT_TAG "origin/master")
set( Boost_Bootstrap_Command )
if( UNIX )
set( Boost_Bootstrap_Command ./bootstrap.sh )
set( Boost_b2_Command ./b2 )
else()
if( WIN32 )
set( Boost_Bootstrap_Command bootstrap.bat )
set( Boost_b2_Command b2.exe )
endif()
endif()
ExternalProject_Add(Boost_external_Download
GIT_REPOSITORY "https://github.com/boostorg/boost.git"
GIT_TAG ${boost_GIT_TAG}
BUILD_IN_SOURCE 1
UPDATE_COMMAND ""
PATCH_COMMAND ""
CONFIGURE_COMMAND ${Boost_Bootstrap_Command}
BUILD_COMMAND ${Boost_b2_Command} install
--without-python
--without-mpi
--disable-icu
--prefix=${CMAKE_BINARY_DIR}/Boost
--threading=single,multi
--link=shared
--variant=release
-j8
INSTALL_COMMAND ""
INSTALL_DIR ""
)
if( NOT WIN32 )
set(Boost_LIBRARY_DIR ${CMAKE_BINARY_DIR}/Boost/lib/ )
set(Boost_INCLUDE_DIR ${CMAKE_BINARY_DIR}/Boost/include/boost/ )
else()
set(Boost_LIBRARY_DIR ${CMAKE_BINARY_DIR}/Boost/lib/ )
set(Boost_INCLUDE_DIR ${CMAKE_BINARY_DIR}/Boost/include/boost/ )
endif()
ExternalProject_Get_Property(Boost_external_Download BINARY_DIR)
SET(Boost_DIR ${BINARY_DIR} CACHE PATH "")
add_library(Boost_external SHARED IMPORTED)
The only thing that is different between one of the solutions and mine is that I don't have the set_target_properties right after I add the library. Since boost has a lot of libraries in there and I am not exactly sure what needs to be added.
In my Source Folder, my CMakeLists.txt does the following:
include_directories(${Boost_INCLUDE_DIR})
add_executable(Main main.cpp)
target_link_libraries(Main Boost_external)
add_dependencies(Main Boost_external_Download)
However, in main.cpp when I use #include <boost/process.hpp> just to see if it links, I get the error that #include <boost/process.hpp> no such file or directory. When I try to make the project. I am not really sure where I am going wrong with this so any helpful advice would be appreciated.
Edit: structure
Root Folder
|
- CmakeLists.txt
- Externals Directory
|
| - Externals.cmake
| - boostExternal.cmake
| - other external cmake files
- Source Directory
|
| - CmakeLists.txt
| - main.cpp
The Root Cmake calls the Externals.cmake file. Which then includes all the other Cmake files within that directory. Once those are finished, the root Cmake file add the directory Source, and that Cmake file has the call to include_directories(${Boost_INCLUDE_DIR}).
Is there a way in cmake to find the source directory of the CMakeLists.txt file in which a target was defined?
Somethign like:
if (TARGET Foo)
message("Library Foo was alread built in ${LOCATION_OF_FOOS_CMAKE}")
else()
add_library(Foo ...)
endif()
Edit:
Unfortunately, my cmake scripts have to work on a default ubuntu 14.04 installation. So I'm limited to cmake 2.8
You can use target property SOURCE_DIR (added with CMake 3.7):
get_target_property(FOO_SOURCE_DIR Foo SOURCE_DIR)
For older versions of CMake you can overwrite e.g. add_library() and define your own SOURCE_DIR target property:
function(add_library _target)
_add_library(${_target} ${ARGN})
set_target_properties(${_target} PROPERTIES SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
endfunction(add_library)
My project directory contains a CMakeLists.txt file and src and include directories at its root. src also contains its own CMakeLists.txt, which is linked by the one at the root. Is there a way I can specify to CMake to set a default global build directory so that the syntax in src/CMakeLists.txt is close to the following?
include_directories(include)
add_executable(first main.cpp foo.cpp)
add_executable(second bar.cpp)
I would like this directory tree to be built:
CMakeLists.txt
src/
CMakeLists.txt
main.cpp
foo.cpp
bar.cpp
include/
...
bin/ (or build/)
first
second
You could set CMAKE_RUNTIME_OUTPUT_DIRECTORY:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
However, this won't create subdirectories inside bin/ for each different target.
If you want that, you could create a helper function to wrap add_subdirectories:
function(my_add_executable TargetName)
set(Files ${ARGV})
list(REMOVE_AT Files 0)
add_executable(${TargetName} ${Files})
set_target_properties(${TargetName} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY
"${CMAKE_SOURCE_DIR}/bin/${TargetName}")
endfunction()
then simply change your calls to:
my_add_executable(first main.cpp foo.cpp)
my_add_executable(second bar.cpp)
For further details, run
cmake --help-variable "CMAKE_RUNTIME_OUTPUT_DIRECTORY"
cmake --help-property "RUNTIME_OUTPUT_DIRECTORY"
cmake --help-property "RUNTIME_OUTPUT_DIRECTORY_<CONFIG>"