Question regarding exporting packages in CMake
i have some clarification needed to export some static libs using CMake. The two static libs are Foo and FooInterfaces and the CMakeLists.txt for those packages are shown below Foo Module project(Foo LANGUAGES CXX) add_library( ${PROJECT_NAME} FooManager.cpp FooManagerFactory.cpp ) target_include_directories(${PROJECT_NAME} PUBLIC $ $ ) target_include_directories(${PROJECT_NAME} PRIVATE $ ) target_link_libraries(${PROJECT_NAME} PUBLIC FooInterfaces Util ) install(TARGETS ${PROJECT_NAME} EXPORT ClientTargets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} INCLUDES DESTINATION ${PROJECT_SOURCE_DIR}/include/) FooInterfaces module project(FooInterfaces LANGUAGES CXX) add_library(FooInterfaces INTERFACE) target_include_directories(FooInterfaces INTERFACE $ $ ) target_link_libraries(FooInterfaces INTERFACE Utils ) install(TARGETS ${PROJECT_NAME} EXPORT ClientTargets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} INCLUDES DESTINATION ${PROJECT_SOURCE_DIR}/include/) My top level CMakeLists.txt is as below ... include(CMakePackageConfigHelpers) write_basic_package_version_file( "${PROJECT_BINARY_DIR}/ClientConfigVersion.cmake" VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion) INSTALL(EXPORT ClientTargets NAMESPACE ClientUtils:: FILE ClientUtilsTargets.cmake DESTINATION ${CMAKE_INSTALL_DIR}/cmake/ClientUtils) and SampleClientUtilsTargets.cmake is as below @PACKAGE_INIT@ include(CMakeFindDependencyMacro) include(${CMAKE_CURRENT_LIST_DIR}/ClientUtilsTargets.cmake) the error i get when i build the project is CMake Error in /Foo/CMakeLists.txt: Target "Foo" INTERFACE_INCLUDE_DIRECTORIES property contains path: "%absloute_path%/ClientUtils/Foo/include" which is prefixed in the source directory. CMake Error in FooInterfaces/CMakeLists.txt: Target "FooInterfaces" INTERFACE_INCLUDE_DIRECTORIES property contains path: "%absloute_path%/ClientUtils/FooInterfaces/include" which is prefixed in the source directory. -- Generating done CMake Generate step failed. Build files cannot be regenerated correctly. CMake Configure Error Any insights as to what i might be missing here, i checked my modules and i am using BUILD_INTERFACE to include my include directory, is that not correct. Second since my goal is to export these packages, am i missing any other logic that i need to include as well, i believe once i am able to successfully build, than consumer project can just do find_package(ClientUtils::foo) etc to link to that module. ----UPDATE 1------ i even tried to change the INSTALL_INTERFACE but still seeing the same error, if i understand correctly we can have absolute paths in BUILD_INTERFACE but not in INSTALL_INTERFACE. Would like to confirm that as well whether i am missing any other steps to export modules. Thanks in advance. target_include_directories(${PROJECT_NAME} PUBLIC $ $ ) target_include_directories(${PROJECT_NAME} PRIVATE $ $ ) -----UPDATE 2---------- Actually i am able to build fine now after i remove INCLUDES DESTINATION from the install function so now the install looks like below install(TARGETS ${PROJECT_NAME} EXPORT ClientTargets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) But even after including the dependency in the consumer package top level config file is here project(TestApp2 LANGUAGES CXX) find_package(ClientUtils::Foo) find_package(ClientUtils::FooInterfaces) find_package(ClientUtils::Utls) add_executable(TestApp2 main.cpp ) target_link_libraries(TestApp2 PUBLIC FooInterfaces Foo Utils ) i see the following error FAILED: sample-app/TestApp2 : && ... ld.lld: error: unable to find library -lFooInterfaces clang-15: error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed. CMake Build Error Which makes me think i am missing some steps or doing something wrong inorder to export these modules, any pointers/feedback is greatly appreciated.
i have some clarification needed to export some static libs using CMake. The two static libs are Foo and FooInterfaces and the CMakeLists.txt for those packages are shown below
Foo Module
project(Foo LANGUAGES CXX)
add_library(
${PROJECT_NAME}
FooManager.cpp
FooManagerFactory.cpp
)
target_include_directories(${PROJECT_NAME}
PUBLIC
$
$
)
target_include_directories(${PROJECT_NAME}
PRIVATE
$
)
target_link_libraries(${PROJECT_NAME}
PUBLIC
FooInterfaces
Util
)
install(TARGETS
${PROJECT_NAME}
EXPORT ClientTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${PROJECT_SOURCE_DIR}/include/)
FooInterfaces module
project(FooInterfaces LANGUAGES CXX)
add_library(FooInterfaces INTERFACE)
target_include_directories(FooInterfaces
INTERFACE
$
$
)
target_link_libraries(FooInterfaces INTERFACE Utils )
install(TARGETS
${PROJECT_NAME}
EXPORT ClientTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${PROJECT_SOURCE_DIR}/include/)
My top level CMakeLists.txt is as below
...
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${PROJECT_BINARY_DIR}/ClientConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
INSTALL(EXPORT ClientTargets
NAMESPACE ClientUtils::
FILE ClientUtilsTargets.cmake
DESTINATION ${CMAKE_INSTALL_DIR}/cmake/ClientUtils)
and SampleClientUtilsTargets.cmake is as below
@PACKAGE_INIT@
include(CMakeFindDependencyMacro)
include(${CMAKE_CURRENT_LIST_DIR}/ClientUtilsTargets.cmake)
the error i get when i build the project is
CMake Error in /Foo/CMakeLists.txt:
Target "Foo" INTERFACE_INCLUDE_DIRECTORIES property contains path:
"%absloute_path%/ClientUtils/Foo/include"
which is prefixed in the source directory.
CMake Error in FooInterfaces/CMakeLists.txt:
Target "FooInterfaces" INTERFACE_INCLUDE_DIRECTORIES property
contains path:
"%absloute_path%/ClientUtils/FooInterfaces/include"
which is prefixed in the source directory.
-- Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.
CMake Configure Error
Any insights as to what i might be missing here, i checked my modules and i am using BUILD_INTERFACE to include my include directory, is that not correct. Second since my goal is to export these packages, am i missing any other logic that i need to include as well, i believe once i am able to successfully build, than consumer project can just do find_package(ClientUtils::foo) etc to link to that module.
----UPDATE 1------
i even tried to change the INSTALL_INTERFACE but still seeing the same error, if i understand correctly we can have absolute paths in BUILD_INTERFACE but not in INSTALL_INTERFACE. Would like to confirm that as well whether i am missing any other steps to export modules. Thanks in advance.
target_include_directories(${PROJECT_NAME}
PUBLIC
$
$
)
target_include_directories(${PROJECT_NAME}
PRIVATE
$
$
)
-----UPDATE 2----------
Actually i am able to build fine now after i remove INCLUDES DESTINATION from the install function so now the install looks like below
install(TARGETS
${PROJECT_NAME}
EXPORT ClientTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
But even after including the dependency in the consumer package top level config file is here
project(TestApp2 LANGUAGES CXX)
find_package(ClientUtils::Foo)
find_package(ClientUtils::FooInterfaces)
find_package(ClientUtils::Utls)
add_executable(TestApp2
main.cpp
)
target_link_libraries(TestApp2
PUBLIC
FooInterfaces
Foo
Utils
)
i see the following error
FAILED: sample-app/TestApp2
: &&
...
ld.lld: error: unable to find library -lFooInterfaces
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
CMake Build Error
Which makes me think i am missing some steps or doing something wrong inorder to export these modules, any pointers/feedback is greatly appreciated.