How to create cmake project with components - cmake

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)

Related

CMake/CPack is inserting dynamic links instead of regular files for external shared libraries

I need to use an specific external shared library on a project. It links fine, and locally it runs like a charm.
The problem is when I need to make a distribution package. When I run cpack the output file has the symbolic links to these external shared libraries. I need it to include the external libraries as well (not just the symbolic link). How can I do it?
Here is my CMakeLists.txt
project(test VERSION 1.0.0 LANGUAGES CXX C)
set(CMAKE_CXX_STANDARD 11)
set(Protobuf_LIBRARIES $ENV{CONDA_PREFIX}/lib/libprotobuf.so)
add_executable(test main.cpp)
target_link_libraries(test
${Protobuf_LIBRARIES}
)
include(GNUInstallDirs)
install(TARGETS test
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(FILES ${Protobuf_LIBRARIES}
DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT RuntimeLibraries
)
set(CPACK_PACKAGE_NAME "Test")
include(CPack)
=== EDIT ===
Following #alex-reinking suggestion, and for the sake of completition on this question, here is the CMakeLists.txt working. It still has a problem with dependencies pointing to local dynamic libraries, so I needed to use an external app to solve that on OSX ( https://github.com/auriamg/macdylibbundler/ ). Be aware that, on this example, I'm including a Linux library.
project(test VERSION 1.0.0 LANGUAGES CXX C)
set(CMAKE_CXX_STANDARD 11)
set(Protobuf_LIBRARIES $ENV{CONDA_PREFIX}/lib/libprotobuf.so)
add_executable(test main.cpp)
target_link_libraries(test
${Protobuf_LIBRARIES}
)
include(GNUInstallDirs)
install(TARGETS test
RUNTIME_DEPENDENCY_SET A-dependency
)
install(RUNTIME_DEPENDENCY_SET A-dependency PRE_EXCLUDE_REGEXES )
install(FILES ${MYLIB_LOCATION} DESTINATION ${CMAKE_INSTALL_LIBDIR})
set(CPACK_PACKAGE_NAME "Test")
include(CPack)```

Problem using FetchContent_Declare together with shared library

I'm having problem using FetchContent_Declare with a shared library. I'm trying to apply a modular design (instead of creating one mega-repository with 20 modules I'm allocating a dedicated repository to each module). I'm trying to use FetchContent_Declare in order to link dependant modules together (I'm not a fan of git-submodules since those require that user manually initialises them). The problem I'm having is that the DLL generated by the project fetched via the mentioned function isn't copied to the binary output of the parent project. Here is a dependency graph to make it more clear.
Repo A:
- bin
- bin-etc
- lib
- include
- project_a
.. header files ..
- src
- CMakeLists.txt : 1
.. source files ..
- CMakeLists.txt : 2
Repo B: Includes A
- bin
- bin-etc
- lib
- output
.. cmake files are built from here ..
- _deps
- project_a-src
- bin
.. here the DLL file is being generated ..
- include
- project_a
.. header files ..
- src
- CMakeLists.txt : 3
.. source files ..
- CMakeLists.txt : 4
The DLL file is being generated in ./output/_deps/project_a-src/bin instead of ./bin.
Here are my CMakeLists.txt files:
# CMakeLists.txt : 1
cmake_minimum_required(VERSION 3.16)
set(PROJECTNAME project_a)
set(PROJECTDIR "${CMAKE_CURRENT_SOURCE_DIR}")
file(GLOB_RECURSE inc "${CMAKE_CURRENT_SOURCE_DIR}/../include/*.hpp")
file(GLOB_RECURSE src "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/../include" FILES ${inc})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${src})
if (MSVC)
add_compile_options(/W3) # warning level 3
add_compile_options(/MP) # Multi-processor compilation
endif()
add_library(
${PROJECTNAME}
SHARED
${inc}
${src}
)
target_include_directories(${PROJECTNAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../include/")
# CMakeLists.txt : 2 and 4
cmake_minimum_required(VERSION 3.16)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_SYSTEM_VERSION 10.0.19041.0)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin-etc")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
project(project_a LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED)
add_subdirectory(src)
# CMakeLists.txt : 3
cmake_minimum_required(VERSION 3.16)
set(PROJECTNAME project_b)
set(PROJECTDIR "${CMAKE_CURRENT_SOURCE_DIR}")
file(GLOB_RECURSE inc "${CMAKE_CURRENT_SOURCE_DIR}/../include/*.hpp")
file(GLOB_RECURSE inc_src "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
file(GLOB_RECURSE src "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/../include" FILES ${inc})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${inc_src})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${src})
if (MSVC)
add_compile_options(/W3) # warning level 3
add_compile_options(/MP) # Multi-processor compilation
endif()
add_library(
${PROJECTNAME}
SHARED
${inc}
${inc_src}
${src}
)
target_include_directories(${PROJECTNAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../include/")
include(FetchContent)
FetchContent_Declare(project_a
GIT_REPOSITORY <REPO LINK>
GIT_TAG master)
FetchContent_MakeAvailable(project_a)
target_link_libraries(${PROJECTNAME} project_a)
What would be the solution to this problem? Should I use another approach for the design like this? Is adding a simple copy command to cmake the right solution?
Variables like CMAKE_RUNTIME_OUTPUT_DIRECTORY affects on the output directory for all further created libraries ... unless the variable is changed.
The project_a in CMakeLists.txt(2) does
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
unconditionally, so it changes the variable even if it is built as a subproject (with FetchContent).
Generally, any project in its root CMakeLists.txt could check, whether it is actually top-level project. And perform some settings only in case it is:
cmake_minimum_required(VERSION 3.21)
project(project_a LANGUAGES CXX)
if (PROJECT_IS_TOP_LEVEL)
# Do global settings only if we are top-level project.
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_SYSTEM_VERSION 10.0.19041.0)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin-etc")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
endif()
# Other settings are treated as project-specific,
# so could be done in "subproject mode" too.
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED)
add_subdirectory(src)
Note, that variable PROJECT_IS_TOP_LEVEL appears only in CMake 3.21. For detect, whether the project is top-level in older CMake versions, consult that question and its answers: How to detect if current scope has a parent in CMake?.

Produce .lib of header-only library that depends on external resources

EDIT: I've read up and understood the initial issue was caused by scanning-header-only not having cpp files and thus a lib file not being generated. Edited the question to reflect that extra understanding:
My current project folder structure and relevant CMakeLists content:
leveling
├── CMakeLists.txt: add_subdirectory(deps)
└── deps
├── CMakeLists.txt: add_subdirectory(scanning-header-only)
└── scanning
├── CMakeLists.txt: add_subdirectory(deps)
│ add_library(scanning-header-only file.h)
│ target_include_directories(scanning-header-only PUBLIC ${CMAKE_CURRENT_LIST_DIR}/deps/tinyxml2)
│ target_link_libraries(scanning-header-only PUBLIC tinyxml2)
└── deps
├── CMakeLists.txt: add_subdirectory(tinyxml2)
└── tinyxml2
But a scanning-header-only library file is not being generated, and thus the root project can't target_link_libraries(leveling scanning-header-only) and has had to target_include_directories(leveling ${CMAKE_CURRENT_LIST_DIR}/deps/scanning-header-only/deps/tinyxml2)
Is it possible to target_link_library a header-only library that depends on external resources?
I see that a header-only library without external resource dependency could be add_library(.. INTERFACE), but I'm failing to do so with the dependency on tinyxml2
A dirty workaround is adding and empty cpp file to scanning-header-only so a lib file is generated, but is there a correct way to do this?
Here is minimal example v1: https://www.dropbox.com/s/r1lbajz3xoat1bg/leveling-header-only-test%20v1.zip?dl=0
leveling CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
set(LEVELING_NAME leveling)
project(${LEVELING_NAME})
#
# To put tinyxml.dll next to the executable, to workaround having to make tinyxml2.dll reachable in PATH
#
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
math(EXPR platform_bits "${CMAKE_SIZEOF_VOID_P} * 8")
set(platform_dir bin/${CMAKE_SYSTEM_NAME}-${platform_bits})
foreach(config DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
foreach(var
CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${config}
CMAKE_LIBRARY_OUTPUT_DIRECTORY_${config}
CMAKE_RUNTIME_OUTPUT_DIRECTORY_${config}
)
set(${var} "${CMAKE_BINARY_DIR}/${platform_dir}/${config}")
string(TOLOWER "${${var}}" ${var})
endforeach()
endforeach()
#
# ----------------------------------------------------------------------
#
add_subdirectory(deps)
add_executable(${LEVELING_NAME} main.cpp)
target_include_directories(${LEVELING_NAME} PUBLIC
${CMAKE_CURRENT_LIST_DIR}/deps/scanning
)
target_link_libraries(${LEVELING_NAME}
xml-reading
)
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${LEVELING_NAME}) # Set Startup Project in VS. Implemented in CMake v3.6
set_target_properties(${LEVELING_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") # Set Working Directory of project in VS. Implemented in CMake v3.8
scanning CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
set(XML_NAME xml-reading)
project(${XML_NAME})
#
# To put tinyxml.dll next to the executable, to workaround having to make tinyxml2.dll reachable in PATH
#
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
math(EXPR platform_bits "${CMAKE_SIZEOF_VOID_P} * 8")
set(platform_dir bin/${CMAKE_SYSTEM_NAME}-${platform_bits})
foreach(config DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
foreach(var
CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${config}
CMAKE_LIBRARY_OUTPUT_DIRECTORY_${config}
CMAKE_RUNTIME_OUTPUT_DIRECTORY_${config}
)
set(${var} "${CMAKE_BINARY_DIR}/${platform_dir}/${config}")
string(TOLOWER "${${var}}" ${var})
endforeach()
endforeach()
#
# ----------------------------------------------------------------------
#
add_subdirectory(deps)
add_library(${XML_NAME} INTERFACE CamerasXML.h)
target_include_directories(${XML_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}/deps/tinyxml2
)
target_link_libraries(${XML_NAME}
INTERFACE tinyxml2
)
which yields
CMake Error at deps/scanning/CMakeLists.txt:33 (add_library):
add_library INTERFACE library requires no source arguments.
A .lib is when you create a STATIC (.lib) or SHARED (.lib and .dll) library on Windows. What you want is an INTERFACE library and it generates no files. http://mariobadr.com/creating-a-header-only-library-with-cmake.html has an example. Then you can use the following commands listed here, https://cmake.org/cmake/help/latest/command/add_library.html#interface-libraries, to populate the interface. Notice that it uses INTERFACE not PUBLIC.
target_link_libraries(INTERFACE),
target_link_options(INTERFACE),
target_include_directories(INTERFACE),
target_compile_options(INTERFACE),
target_compile_definitions(INTERFACE), and
target_sources(INTERFACE),
I've never actually used this but I assume it works as documented.
A simple add_library(${XML_NAME} INTERFACE) (not specifying any source files), while having target_include_directories(${XML_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR}/deps/tinyxml2) and target_link_libraries(${XML_NAME} INTERFACE tinyxml2) will do the trick.
The tinyxml2 includes are made available to the parent project, and the tinyxml2 library is linked in the parent project.

target_link_libraries erroneously pulls in MODULE library at link time

I have two C++ projects using CMake 3.12.2.
The first one is MODULE library (a dynamically loaded plugin). It installs a DLL, a header file and the configuration file for CMake.
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(MyPlugin LANGUAGES CXX)
set(CMAKE_DEBUG_POSTFIX "d")
# MODULE libraries are dynamically loaded at runtime and never linked against
add_library(MyPlugin MODULE
include/a.h
src/a.cpp
src/b.h
src/b.cpp
)
target_include_directories(MyPlugin
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/MyPlugin>
PRIVATE
src
)
install(TARGETS MyPlugin EXPORT MyPluginConfig
# MODULE libraries are installed as LIBRARY
LIBRARY DESTINATION plugins COMPONENT Runtime
RUNTIME DESTINATION bin COMPONENT Runtime
PUBLIC_HEADER DESTINATION include/MyPlugin COMPONENT Development
)
install(FILES $<TARGET_PDB_FILE:MyPlugin> DESTINATION plugins OPTIONAL COMPONENT Runtime)
install(
DIRECTORY include/
DESTINATION include/MyPlugin
FILES_MATCHING PATTERN "*.h"
)
install(EXPORT MyPluginConfig
NAMESPACE MyPlugin::
DESTINATION lib/cmake/MyPlugin
)
The second one is a simple executable which pulls in the header file of the plugin via target_link_libraries (the modern CMake way).
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(MyExe LANGUAGES CXX)
set(CMAKE_DEBUG_POSTFIX "d")
find_package(MyPlugin REQUIRED)
add_executable(MyExe src/main.cpp)
target_link_libraries(MyExe MyPlugin::MyPlugin)
Using the vs2015 generated solution the link fails because the plugin DLL is fed during the link of the executable...
Has anyone a solution for this or should I file a bug?
Regards.
A solution to this issue is to split the library in two: an interface library which only provides the headers and a module library which does not export any header.
The module library CMakeLists.txt becomes:
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(MyPlugin LANGUAGES CXX)
set(CMAKE_DEBUG_POSTFIX "d")
# Move public headers to a dedicated INTERFACE library
add_library(MyPluginInterface INTERFACE)
add_custom_target(Includes SOURCES include/a.h)
target_include_directories(MyPluginInterfacecmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(MyPlugin LANGUAGES CXX)
set(CMAKE_DEBUG_POSTFIX "d")
# Move public headers to a dedicated INTERFACE library
add_library(MyPluginInterface INTERFACE)
add_custom_target(Includes SOURCES include/a.h)
target_include_directories(MyPluginInterface
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/MyPlugin>
)
install(TARGETS MyPluginInterface EXPORT MyPluginConfig
PUBLIC_HEADER DESTINATION include/MyPlugin COMPONENT Development
)
# MODULE libraries are dynamically loaded at runtime and never linked against
add_library(MyPlugin MODULE
src/a.cpp
src/b.h
src/b.cpp
)
target_link_libraries(MyPlugin MyPluginInterface)
install(TARGETS MyPlugin
# MODULE libraries are installed as LIBRARY
LIBRARY DESTINATION plugins COMPONENT Runtime
RUNTIME DESTINATION bin COMPONENT Runtime
)
install(FILES $<TARGET_PDB_FILE:MyPlugin> DESTINATION plugins OPTIONAL COMPONENT Runtime)
install(
DIRECTORY include/
DESTINATION include/MyPlugin
FILES_MATCHING PATTERN "*.h"
)
install(EXPORT MyPluginConfig
NAMESPACE MyPlugin::
DESTINATION lib/cmake/MyPlugin
)
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/MyPlugin>
)
install(TARGETS MyPluginInterface EXPORT MyPluginConfig
PUBLIC_HEADER DESTINATION include/MyPlugin COMPONENT Development
)
# MODULE libraries are dynamically loaded at runtime and never linked against
add_library(MyPlugin MODULE
src/a.cpp
src/b.h
src/b.cpp
)
target_link_libraries(MyPlugin MyPluginInterface)
install(TARGETS MyPlugin
# MODULE libraries are installed as LIBRARY
LIBRARY DESTINATION plugins COMPONENT Runtime
RUNTIME DESTINATION bin COMPONENT Runtime
)
install(FILES $<TARGET_PDB_FILE:MyPlugin> DESTINATION plugins OPTIONAL COMPONENT Runtime)
install(
DIRECTORY include/
DESTINATION include/MyPlugin
FILES_MATCHING PATTERN "*.h"
)
install(EXPORT MyPluginConfig
NAMESPACE MyPlugin::
DESTINATION lib/cmake/MyPlugin
)
The executable CMakeLists.txt becomes:
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(MyExe LANGUAGES CXX)
set(CMAKE_DEBUG_POSTFIX "d")
find_package(MyPlugin REQUIRED)
add_executable(MyExe src/main.cpp)
target_link_libraries(MyExe MyPlugin::MyPluginInterface)

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