How to link an interface with static lib with CMake - dll

What I want is a main library, based on an interface (in the future more than one). That interface must be used by other libs, like lib1 and lib2.
The idea is to create lib1 and lib2 as static, and then link both to the mainLib, which must be a shared library.
For this example, let's imagine I'm only using lib1, because the process would be the same for lib2.
My problem is that I don't really know how to link the libraries correctly. First I need to link the interface library with lib1, so it becomes
a new static library and this one has to be linked with the mainlib shared library.
I'm trying to make a project with the following structure:
mainLib
|-------> CMakeLists.txt
|-------> include
| |----> mainlib.h
|-------> src
| |----> mainlib.cpp
|
|-------> interface
| |----> CMakeLists.txt
| |----> include
| |----> interface.hpp
| |----> src
| |----> interface.cpp
| |----> lib1
| |----> CMakeLists.txt
| |----> include
| |----> lib1.hpp
| |----> src
| |----> lib1.cpp
| |----> lib2
| |----> CMakeLists.txt
| |----> include
| |----> lib2.hpp
| |----> src
| |----> lib2.cpp
Here's a version of the code I'm using in my cmakelists:
mainLib
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/interface ${CMAKE_CURRENT_BINARY_DIR}/interface)
set(EXPORTABLE_HEADERS include/mainlib.hpp)
set(SOURCES src/mainlib.cpp)
set(HEADERS ${EXPORTABLE_HEADERS})
add_library(${PROJECT_NAME} SHARED ${SOURCES} ${HEADERS} $<TARGET_OBJECTS:lib1>)
target_include_directories(${PROJECT_NAME}
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
INTERFACE $<INSTALL_INTERFACE:include>)
set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER "${EXPORTABLE_HEADERS}")
install(TARGETS ${PROJECT_NAME} EXPORT ${PRODUCT_TARGET}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
PUBLIC_HEADER DESTINATION include/${PROJECT_NAME})
target_link_libraries(${PROJECT_NAME} lib1)
install(DIRECTORY interface/include/
DESTINATION "include/${PROJECT_NAME}/interface")
install(DIRECTORY interface/lib1/include/
DESTINATION "include/${PROJECT_NAME}/interface/lib1")
interface
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/lib1 ${CMAKE_CURRENT_BINARY_DIR}/lib1)
add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(${PROJECT_NAME} INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
install(TARGETS ${PROJECT_NAME} EXPORT ${PRODUCT_TARGET})
lib1
set(EXPORTABLE_HEADERS include/lib1.hpp)
set(SOURCES src/lib1.cpp)
set(HEADERS ${EXPORTABLE_HEADERS})
add_library(${PROJECT_NAME} OBJECT ${SOURCES} ${HEADERS})
target_include_directories(${PROJECT_NAME}
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE $<TARGET_PROPERTY:interface,INTERFACE_INCLUDE_DIRECTORIES>)
install(TARGETS ${PROJECT_NAME} EXPORT ${PRODUCT_TARGET})
target_link_libraries(${PROJECT_NAME} INTERFACE interface)
So, I need to install all the headers, at the ${PRODUCT_TARGET} defined. But I need to do it following the same estructure of the project.
Right now, all the hpp are installed in the same folder (${PRODUCT_TARGET}).
The interface is not linked with my lib1 because it needs a target 'interface' that is not in the export set, and I don't know how to solve it because
I'm not able to set the install properties in the interface due to lot's of errors.
Once lib1 includes the interface. I want to link it with the mainlib. I'm just doing a target_link_libraries with it, but I'm not sure this is the correct way of
linking shared with static libraries.
EDIT:
I can generate de shared library with this code, but when I try using it, and I include the lib.hpp, it can't find the headers of the interface.
I don't really know where the problem is...
All advices are appreciated.

Related

How to create cmake project with components

Im trying to create project in Cmake which contains components.
So another project can use this project using:
find_package(ltk COMPONENTS Core Networking REQUIRED)
target_link_libraries(test ltk::Core ltk::Networking)
My library project (called ltk)
Here is a project tree:
ltk/
|--- CMakeLists.txt
|--- ltkConfig.cmake.in
|--- ltkConfigVersion.cmake.in
|
+--- Core/
| |--- core.cc
| |--- core.h
| |--- CMakeLists.txt
|
+--- Networking/
|--- networking.cc
|--- networking.h
|--- CMakeLists.txt
Full project is here: https://gitlab.com/T0maas/cmake-testing
The file ltk/CMakeLists.txt has this content:
set(project ltk)
set(LTK_VERSION 0.0.1)
cmake_minimum_required(VERSION 3.21)
project(${project})
add_subdirectory(Core)
add_subdirectory(Networking)
foreach(p LIB BIN INCLUDE CMAKE)
set(var INSTALL_${p}_DIR)
if(NOT IS_ABSOLUTE "${${var}}")
set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
endif()
endforeach()
export(TARGETS Core Networking FILE "${PROJECT_BINARY_DIR}/ltkTargets.cmake")
export(PACKAGE ltk)
file(RELATIVE_PATH REL_INCLUDE_DIR "${INSTALL_CMAKE_DIR}" "${INSTALL_INCLUDE_DIR}")
set(CONF_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}" "${PROJECT_BINARY_DIR}")
configure_file(ltkConfig.cmake.in "${PROJECT_BINARY_DIR}/ltkConfig.cmake" #ONLY)
set(CONF_INCLUDE_DIRS "\${LTK_CMAKE_DIR}/${REL_INCLUDE_DIR}")
configure_file(ltkConfig.cmake.in "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/ltkConfig.cmake" #ONLY)
configure_file(ltkConfigVersion.cmake.in "${PROJECT_BINARY_DIR}/ltkConfigVersion.cmake" #ONLY)
install(FILES
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/ltkConfig.cmake"
"${PROJECT_BINARY_DIR}/ltkConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/cmake/${project}" )
install(EXPORT ltkTargets DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/cmake/${project}" )
And the Core/CMakeLists.txt contains this:
set (component Core)
add_library(${component} SHARED core.cc )
add_library(${project}::${component} ALIAS ${component})
set_target_properties(${component} PROPERTIES PUBLIC_HEADER "core.h")
install(TARGETS ${component}
EXPORT ltkTargets
COMPONENT ${component}
LIBRARY DESTINATION lib/ltk
ARCHIVE DESTINATION lib/ltk
RUNTIME DESTINATION bin
PUBLIC_HEADER DESTINATION include/ltk/
)
The Networking/CMakeLists.txt is similar to Core/CMakeLists.txt
File ltkConfig.cmake.in contains this:
get_filename_component(LTK_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(LTK_INCLUDE_DIRS "#CONF_INCLUDE_DIRS#")
if(NOT TARGET Core AND NOT LTK_BINARY_DIR)
include("${LTK_CMAKE_DIR}/ltkTargets.cmake")
endif()
if(NOT TARGET Networking AND NOT LTK_BINARY_DIR)
include("${LTK_CMAKE_DIR}/ltkTargets.cmake")
endif()
set(LTK_LIBRARIES Core Networking)
And the ltkConfigVersion.cmake.in:
set(PACKAGE_VERSION "#LTK_VERSION#")
# Check whether the requested PACKAGE_FIND_VERSION is compatible
if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
set(PACKAGE_VERSION_COMPATIBLE TRUE)
if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}")
set(PACKAGE_VERSION_EXACT TRUE)
endif()
endif()
After install this project I see some files in /usr/lib/cmake/ltk:
ltkTargets-noconfig.cmake
ltkTargets.cmake
ltkConfigVersion.cmake
ltkConfig.cmake
Testing
But now when I try to configure some testing project, which has following CMakeLists.txt:
cmake_minimum_required(VERSION 3.21)
project(test)
find_package(ltk COMPONENTS Core REQUIRED)
add_executable(main main.cc)
target_link_libraries(main ltk::Core)
It shows following errors:
-- Configuring done
CMake Error at CMakeLists.txt:4 (add_executable):
Target "main" links to target "ltk::Core" but the target was not found.
Perhaps a find_package() call is missing for an IMPORTED target, or an
ALIAS target is missing?
-- Generating done
I've read following links, but this doesn't helped me:
https://gitlab.kitware.com/cmake/community/-/wikis/doc/tutorials/How-to-create-a-ProjectConfig.cmake-file
How to configure project with COMPONENTS in cmake
Edit
Now I added NAMESPACE ltk:: to install(EXPORT) in ltk/CMakeLists.txt:
install(EXPORT ltkTargets DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/cmake/${project}" NAMESPACE ltk::)
But the testing project which contains #include <core.h> complains:
fatal error: core.h: No such file or directory
Path to /usr/include/ltk has not been exported.
Edit 2
Fixed include problems with:
Added to {Core, Networking}/CMakeLists.txt
target_include_directories(${component} PUBLIC $<INSTALL_INTERFACE:include/ltk>)
Now I can use in my testing project this:
target_include_directories(main PUBLIC ltk)

How to export CMake targets with dependant include directories?

I am trying to export CMake targets such that they also include the public include directories of dependant targets.
Let there be three separate but depending CMake projects:
foobar_result defines a data type and methods for this datatype in result.hpp
foobar_interface defines a header-only abstract class in foobar_interface.hpp which uses the datatype defined in foobar_result (#include <result.hpp>)
foobar_implA is an implementation (foobar_implA.cpp, foobar_implA.hpp) of the abstract class in foobar_interface (#include <foobar_interface.hpp>), and hence indirectly depends on the datatype definition (it #include <result.hpp> via #include <foobar_interface.hpp>)
What I want to achieve is that when foobar_implA is find_package(foobar_interface), it should automatically populate the include directories and libraries with the dependencies of foobar_interface so that foobar_implA does not have to discover foobar_result's include directories manually.
So far, I tried to specify the three CMake projects as follows.
1. foobar_result
foobar_result defines a standard library with headers:
cmake_minimum_required(VERSION 3.5)
project(foobar_result)
include(GNUInstallDirs)
add_library(result SHARED src/result.cpp)
target_include_directories(result PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>
)
install(EXPORT ${PROJECT_NAME}
DESTINATION share/${PROJECT_NAME}/cmake
FILE ${PROJECT_NAME}Config.cmake
)
install(TARGETS result EXPORT ${PROJECT_NAME})
install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING PATTERN "*.hpp"
)
The exported foobar_resultConfig.cmake:
add_library(result SHARED IMPORTED)
set_target_properties(result PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
)
and foobar_resultConfig-noconfig.cmake:
set_target_properties(result PROPERTIES
IMPORTED_LOCATION_NOCONFIG "${_IMPORT_PREFIX}/lib/libresult.so"
IMPORTED_SONAME_NOCONFIG "libresult.so"
)
correctly define the includes and libraries.
2. foobar_interface
The interface uses the result library:
cmake_minimum_required(VERSION 3.5)
project(foobar_interface)
include(GNUInstallDirs)
find_package(foobar_result REQUIRED)
add_library(foobar_interface INTERFACE)
target_link_libraries(foobar_interface INTERFACE result)
target_include_directories(${PROJECT_NAME} INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>
)
install(EXPORT ${PROJECT_NAME}
DESTINATION share/${PROJECT_NAME}/cmake
FILE ${PROJECT_NAME}Config.cmake
)
install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING PATTERN "*.hpp"
)
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME})
Now, although the interface foobar_interface links the result library, the exported configuration:
add_library(foobar_interface INTERFACE IMPORTED)
set_target_properties(foobar_interface PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
INTERFACE_LINK_LIBRARIES "result"
)
only depends on the result library, but not its include directories. foobar_interface only exports its own INTERFACE_INCLUDE_DIRECTORIES, but not those of its dependency foobar_result.
3. foobar_implA
The implementation of the interface depends only on foobar_implA:
cmake_minimum_required(VERSION 3.5)
project(foobar_implA)
include(GNUInstallDirs)
find_package(foobar_interface REQUIRED)
add_library(foobar_implA SHARED src/foobar_implA.cpp)
target_link_libraries(foobar_implA PUBLIC foobar_interface)
target_include_directories(foobar_implA PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>
)
install(EXPORT ${PROJECT_NAME}
DESTINATION share/${PROJECT_NAME}/cmake
FILE ${PROJECT_NAME}Config.cmake
)
install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING PATTERN "*.hpp"
)
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME})
fails with:
In file included from [...]/src/foobar_implA/include/foobar_implA.hpp:3,
from [...]/src/foobar_implA/src/foobar_implA.cpp:1:
[...]/install/foobar_interface/include/foobar_interface.hpp:3:10: fatal error: result.hpp: No such file or directory
3 | #include <result.hpp>
| ^~~~~~~~~~~~
because find_package(foobar_interface) will not populate the include directories with the one defined in foobar_result.
I can only work around this issue by manually including foobar_result again:
find_package(foobar_result REQUIRED)
target_link_libraries(foobar_implA PUBLIC result)
but this is obviously not what I want, as this would not scale well when the dependencies in foobar_interface increase.
I am using colcon to separate the build and install spaces for all three projects. This way, all projects have to properly export and import their include and link directories. Installing all projects in the same prefix would also circumvent this problem, as foobar_interface include directories would also contain headers from foobar_result.

How to structure a cmake project with different 3rd-party libraries

I'm new with cmake and try to create a small project which needs some 3rd-party libs. I would like to have the libs as git repos to always be up-to-date. Some libs are just .cpp and .hpp files (glad, imgui) and others are cmake projects (glfw, glm).
The idea is to have a 3rd-party project with all the libs as a kind of subprojects and a sandbox project which uses the libs and includes etc.
And I would like to use modern cmake code which is not installing something outside the framework structure.
Folder structure:
Framework
|--3rd_party
| |--glad
| | |--include
| | |--src
| |--glfw-master
| | |--...
| | |--CMakeLists.txt
| |--glm-master
| | |--..
| | |--CMakeLists.txt
| |--imgui-master
| | |--*.cpp
| | |--*.hpp
| | |--examples
| | | |--*.cpp
| | | |--*.hpp
| |--CMakeLists.txt
|--sandbox
| |--main.cpp
| |--CMakeLists.txt
|--CMakeLists.txt
So I created this folder structure and also some CMakeLists:
CMakeLists.txt (Framework)
cmake_minimum_required(VERSION 3.10)
project(Framework)
add_subdirectory("3rd_party")
add_subdirectory("sandbox")
CMakeLists.txt (3rd_party)
#GLFW
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(glfw-master)
# GLM
set(GLM_TEST_ENABLE OFF CACHE BOOL "" FORCE)
add_subdirectory(glm-master)
# Glad
add_library(
Glad STATIC
"glad/src/glad.c"
)
target_include_directories(Glad PUBLIC "glad/include")
# ImGui
add_compile_definitions(IMGUI_IMPL_OPENGL_LOADER_GLAD=1)
set(IMGUI_SOURCES
"imgui-master/imgui.cpp"
"imgui-master/imgui_demo.cpp"
"imgui-master/imgui_draw.cpp"
"imgui-master/imgui_widgets.cpp"
"imgui-master/examples/imgui_impl_glfw.cpp"
"imgui-master/examples/imgui_impl_opengl3.cpp"
)
set(IMGUI_HEADERS
"imgui-master/imconfig.h"
"imgui-master/imgui.h"
"imgui-master/imgui_internal.h"
"imgui-master/imstb_rectpack.h"
"imgui-master/imstb_textedit.h"
"imgui-master/imstb_truetype.h"
"imgui-master/examples/imgui_impl_glfw.h"
"imgui-master/examples/imgui_impl_opengl3.h"
)
add_library(
ImGui STATIC
${IMGUI_SOURCES}
${IMGUI_HEADERS}
)
target_include_directories(ImGui PUBLIC "imgui-master" "glfw-master/include" "glad/include")
CMakeLists.txt (sandbox)
project(Sandbox)
find_package(OpenGL REQUIRED)
add_executable(sandbox main.cpp)
# OpenGL
target_include_directories(Sandbox PUBLIC ${OPENGL_INCLUDE_DIR})
target_include_directories(Sandbox PUBLIC external)
# Glfw
target_include_directories(Sandbox PUBLIC "../3rd_party/glfw-master/include")
# Link libs
target_link_libraries(Sandbox PUBLIC
${OPENGL_LIBRARIES}
"../3rd_party/glfw-master/src/Debug/glfw3"
Glad
ImGui
glm_static
)
The code works but not as I expect. At first I know it's a little bit ugly maybe there is a better way to handle the path for includes and sources but the bigger problem is project structure.
For example, when I build it for ms vs studio I have three solutions
./framework.sln
./sandbox/sandbox.sln
./3rd_party/glfw-master/glfw.sln
And glad, glm and imgui are part of sandbox.sln
What I would like to have is a solution with two sub-solutions sandbox and 3rd_party which also has sub-solutions or projects for all the libs.
So is it possible at all and if yes how can I create such a structure with cmake?
After further investigation and a lot of try and error with CMake, I guess I started with wrong expectations and some missunderstandings.
It is not possible to get a useful solution/project structure for Visual Studio from CMake files because all CMake-projects will result in a VS-solution and all add_library or add_executable will be a VS-project. If I want to create a CMake-project for my framework and add GLFW as third party, which also creates a CMake-project, it will end with two different VS-solutions and bang the structure is gone. I think that is why Microsoft introduced a special open context for CMake files in Visual Studio.
If a project is opened with this context, the "solution" viewer shows the folder structure of the project. And that is more or less what I expected in the beginning.
There is also an option to switch the view to see all CMake targets (exec, libs, ...).
I also "upgraded" my CMakeLists. I use different ways to add the third party libs:
add_subdirectory for libs with CMakeLists like GLFW
add_library and INTERFACE for header-only libs like glm
add_library STATIC for static libs like ImGui (in my case)
The includes for Sandbox are provided by the public interface of the libs and the build/dependency order is given by order in target_link_libraries.
I hope that is was helpul and will spare you a lot of time ;)
Cheers!
boss0r
CMakeLists.txt (Framework)
cmake_minimum_required(VERSION 3.10)
project(OpenGL_Framework
VERSION 0.0.1
LANGUAGES CXX C # C for GLFW
)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_subdirectory(ThirdParty)
add_subdirectory(Sandbox)
CMakeLists.txt (3rd_party)
# GLFW
set(GLFW_LIB_NAME "GLFW")
set(GLFW_INC_PATH ${GLFW_LIB_NAME}/include)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
#set(GLFW_VULKAN_STATIC OFF CACHE BOOL "" FORCE) # OFF by default
#set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE) # OFF by default
set(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory(${GLFW_LIB_NAME})
# spdlog
set(SPDLOG_LIB_NAME "spdlog")
set(SPDLOG_MASTER_PROJECT OFF CACHE BOOL "" FORCE)
add_subdirectory(${SPDLOG_LIB_NAME})
# OR
#set(SPDLOG_LIB_NAME "spdlog")
#set(SPDLOG_SRC_PATH ${SPDLOG_LIB_NAME}/src)
#set(SPDLOG_INC_PATH ${SPDLOG_LIB_NAME}/include)
#add_library(${SPDLOG_LIB_NAME}
# STATIC
# ${SPDLOG_SRC_PATH}/spdlog.cpp
#)
#target_include_directories(${SPDLOG_LIB_NAME}
# PUBLIC
# ${SPDLOG_INC_PATH})
# glm
set(GLM_LIB_NAME "glm")
set(GLM_INC_PATH ${GLM_LIB_NAME}/glm)
add_library(${GLM_LIB_NAME} INTERFACE)
target_include_directories(${GLM_LIB_NAME}
INTERFACE
${GLM_INC_PATH}
)
# OR
#set(GLM_LIB_NAME "glm")
#set(GLM_INC_PATH ${GLM_LIB_NAME}/glm)
#set(GLM_TEST_ENABLE OFF CACHE BOOL "" FORCE)
#add_subdirectory(${GLM_LIB_NAME})
#target_include_directories(${GLM_LIB_NAME}
# PUBLIC
# $(GLM_INC_PATH)
#)
# Glad
set(GLAD_LIB_NAME "Glad")
set(GLAD_SRC_PATH "${GLAD_LIB_NAME}/src")
set(GLAD_INC_PATH "${GLAD_LIB_NAME}/include")
add_library( ${GLAD_LIB_NAME}
STATIC
"${GLAD_SRC_PATH}/glad.c"
)
target_include_directories(${GLAD_LIB_NAME}
PUBLIC
"${GLAD_INC_PATH}"
)
# ImGui
set(IMGUI_LIB_NAME "ImGui")
set(IMGUI_SOURCES
"${IMGUI_LIB_NAME}/imgui.cpp"
"${IMGUI_LIB_NAME}/imgui_demo.cpp"
"${IMGUI_LIB_NAME}/imgui_draw.cpp"
"${IMGUI_LIB_NAME}/imgui_widgets.cpp"
"${IMGUI_LIB_NAME}/examples/imgui_impl_glfw.cpp"
"${IMGUI_LIB_NAME}/examples/imgui_impl_opengl3.cpp"
)
set(IMGUI_HEADERS
"${IMGUI_LIB_NAME}/imconfig.h"
"${IMGUI_LIB_NAME}/imgui.h"
"${IMGUI_LIB_NAME}/imgui_internal.h"
"${IMGUI_LIB_NAME}/imstb_rectpack.h"
"${IMGUI_LIB_NAME}/imstb_textedit.h"
"${IMGUI_LIB_NAME}/imstb_truetype.h"
"${IMGUI_LIB_NAME}/examples/imgui_impl_glfw.h"
"${IMGUI_LIB_NAME}/examples/imgui_impl_opengl3.h"
)
set(IMGUI_INC_PATH "${IMGUI_LIB_NAME}/")
add_library(${IMGUI_LIB_NAME}
STATIC
${IMGUI_SOURCES}
${IMGUI_HEADERS}
)
target_compile_definitions(${IMGUI_LIB_NAME}
PRIVATE
IMGUI_IMPL_OPENGL_LOADER_GLAD=1
)
target_include_directories(${IMGUI_LIB_NAME}
PUBLIC
"${IMGUI_INC_PATH}"
"${GLFW_INC_PATH}"
"${GLAD_INC_PATH}"
)
CMakeLists.txt (sandbox)
project(Sandbox)
find_package(OpenGL REQUIRED)
add_executable(${PROJECT_NAME} Sandbox.cpp)
target_include_directories(${PROJECT_NAME}
PUBLIC
external
${OPENGL_INCLUDE_DIR}
)
target_link_libraries(${PROJECT_NAME}
PUBLIC
${OPENGL_gl_LIBRARY}
glfw
Glad
ImGui
glm
#glm_static # if build with add_subdirectory
spdlog::spdlog
)

Cannot find good include directory and lib in my project after find_package

I'm building my own library using this CMakeLists.txt :
PROJECT (AstroLib)
cmake_minimum_required(VERSION 3.0)
set(AALib_VERSION_MAJOR 1 CACHE STRING "major version" FORCE)
set(AALib_VERSION_MINOR 95 CACHE STRING "minor version" FORCE)
set(AALib_VERSION ${AALib_VERSION_MAJOR}.${AALib_VERSION_MINOR} CACHE STRING "version" FORCE)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON)
include(GenerateExportHeader)
file(GLOB AALib_SRCS "AALIb/source/*.cpp")
file(GLOB AALib_Headers "AALIb/include/*.h" "AALIb/include/*.hpp")
message ("AALib_VERSION ${AALib_VERSION}")
message ("CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}")
message ("CMAKE_CURRENT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}")
add_library (AALib STATIC ${AALib_SRCS})
target_include_directories(AALib PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/AALib/include>
$<INSTALL_INTERFACE:include>
PRIVATE AALib/source)
generate_export_header( AALib )
set_property(TARGET AALib PROPERTY VERSION ${AALib_VERSION})
install(TARGETS AALib EXPORT AALibConfig
LIBRARY DESTINATION "lib/${CMAKE_BUILD_TYPE}"
ARCHIVE DESTINATION "lib/${CMAKE_BUILD_TYPE}"
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/AALib_export.h" DESTINATION include COMPONENT Devel)
install(FILES ${AALib_Headers} DESTINATION include)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/AALib/AALibConfigVersion.cmake"
VERSION ${AALib_VERSION}
COMPATIBILITY AnyNewerVersion
)
export(TARGETS AALib
FILE "${CMAKE_CURRENT_BINARY_DIR}/AALib/AALibConfig.cmake"
NAMESPACE Upstream::
)
install(EXPORT AALibConfig DESTINATION AALib/cmake)
set(FOO "rtatzera")
#configure_file(AALibTargets.cmake "${CMAKE_CURRENT_BINARY_DIR}/AALib/AALibConfig.cmake" COPYONLY)
set(ConfigPackageLocation lib/cmake/AALib)
message ("ConfigPackageLocation ${ConfigPackageLocation}")
install(EXPORT AALibConfig
FILE
AALibConfig.cmake
NAMESPACE
Upstream::
DESTINATION
${ConfigPackageLocation}
)
I run cmake-gui with this cmakelists.txt and I can build and install my library on my computer.
Now I want to use this lib in another project using this cmakelists.txt :
cmake_minimum_required(VERSION 3.0)
PROJECT (DataMeteo)
find_package(OpenCV REQUIRED)
find_package(AALib REQUIRED)
file(GLOB DataMeteo_SRCS
"*.h"
"*.cpp")
ADD_EXECUTABLE (DataMeteo ${DataMeteo_SRCS})
message ( "AALib = ${AAlib}")
if (OpenCV_FOUND AND AALib_FOUND)
target_link_libraries( DataMeteo PUBLIC ${AALIb_} ${OpenCV_LIBS})
else (OpenCV_FOUND AND AALib_FOUND)
message("OPENC not found or AALib NOT FOUND ")
endif (OpenCV_FOUND AND AALib_FOUND)
In VS project I can find all opencv libs and include but I cannot find include and lib of my own Library. There is no error when I run cmake-gui.
Where is error(s) in my code (cmakelists.txt)?
Configuration Windows 10 64 bits VS2017 CMake 3.11.0

Overlapping dependencies between libraries in CMake

Let's say there's the following directory structure:
projects
|
+--lib1
| |
| +-CMakeFiles.txt
|
+--lib2
| |
| +-CMakeFiles.txt
|
+--test
|
+-CMakeFiles.txt
lib1/CMakeFiles.txt:
cmake_minimum_required(VERSION 2.0)
add_library(lib1 STATIC lib1.cpp)
lib2/CMakeFiles.txt:
cmake_minimum_required(VERSION 2.0)
add_subdirectory(../lib1 ${CMAKE_CURRENT_BINARY_DIR}/lib1)
add_library(lib2 STATIC lib2.cpp)
target_link_libraries(lib2 lib1)
test/CMakeFiles.txt:
cmake_minimum_required(VERSION 2.0)
project(test)
add_subdirectory(../lib1 ${CMAKE_CURRENT_BINARY_DIR}/lib1)
add_subdirectory(../lib2 ${CMAKE_CURRENT_BINARY_DIR}/lib2)
add_executable(test main.cpp)
target_link_libraries(test lib1 lib2)
I.e. lib2 depends on lib1 and test depends on both of them. (I know that technically static libraries don't "link", but that's just an example.)
The problem is that with the current setup, lib1 compiles twice - the first time it is within the "test" build directory, and a second time it is within "test/build_directory/lib2/build_directory". I'd like to avoid that.
I want to be able to add a dependency on lib1, lib2 or both of them (using add_subdirectory) to any project that's located elsewhere. So moving CMakeFiles isn't an option. I also want to avoid compiling any library several times.
How can I do that?
Platform: CMake v. 2.8.4 and Windows XP SP3
A top-level CMakeLists.txt file isn't an option, because I want to keep a clean top-level directory and be able to include libraries in other projects that can be located elsewhere. Because it is Windows, I can't "install package system-wide" - I don't want to lose ability to switch compiler on the fly. Utility libraries built with different compilers will use different C runtime libraries/ABI, and as a result will be incompatible.
One other solution is to add a guard at the top of the subdirectory-CMakeLists.txt:
if(TARGET targetname)
return()
endif(TARGET targetname)
Which will cause cmake to do nothing the second time the subdirectory is added (if targetname is being defined in that file, of course).
This will lead to the lib beeing build in an sort-of-arbitrary place (depending on which module added it first) in the build/ tree, but it will be built only once and linked everywhere.
In your example, you would add
if(TARGET lib1)
return()
endif(TARGET lib1)
at the top of lib1/CMakeFiles.txt
With CMake, library dependencies are transitive, so you shouldn't call add_subdirectory twice in test/CMakeFiles.txt (nor do you need to list lib1 as a dependency of test since it is already a dependency of lib2's).
So you could modify test's CMakeFiles.txt to:
cmake_minimum_required(VERSION 2.8.7) # Prefer the most current version possible
project(test)
add_subdirectory(../lib2 ${CMAKE_CURRENT_BINARY_DIR}/lib2)
add_executable(test main.cpp)
target_link_libraries(test lib2)
Also, you should probably remove the cmake_minimum_required calls from your non-project CMakeFiles.txt files (the lib ones). For further info, run:
cmake --help-policy CMP0000
This setup will still cause all libs to be recompiled if you add a similar test2 subdirectory and project which depends on lib1 and lib2. If you really don't want to have a top-level CMakeFiles.txt in projects/, then you're stuck with what you're doing, or you could use either the export or install command.
export would create a file which could be included by other projects and which imports the targets into the project which calls include.
install could install the libraries to another common subdirectory of projects/. Depending on your source directory structure, this could have the benefit of only making the intended library API headers available to dependent projects.
However, both of these options require the dependent library projects to be rebuilt (and installed) if they are modified, whereas your current setup includes all the dependent targets in your project, so any change to a source file in a dependent library will cause your test target to go out of date.
For further details about export and install, run:
cmake --help-command export
cmake --help-command install
Perhaps add a top-level CMakeLists.txt in your projects dir. Something like:
project( YourProjects )
add_subdirectory( lib1 )
add_subdirectory( lib2 )
add_subdirectory( test )
This should be sufficient and will give you a solution-file or makefile in your top-level build-dir. You should then remove the add_subdirectory( ../lib1 ... from your lib1 and lib2 projects, but instead simply link to them. CMake will know how to find lib1 and lib2 when compiling test.
I.e. in lib2:
project( lib2)
add_library(lib2 STATIC lib2.cpp)
target_link_libraries(lib2 lib1)
and in test:
project( test )
add_executable(test main.cpp)
target_link_libraries(test lib1 lib2)
Added bonus: you will get makefiles/solutionfiles for building lib2 (with dependent lib1) in the lib2 directory...