I have the following problem. I have a project (classic c++ library) called arsenalgear-cpp
and want to compile it and let it be distributable for other projects.
My CMakeLists.txt
is the following:
# CMake project settings
cmake_minimum_required( VERSION 3.15 )
project( arsenalgear-cpp
VERSION 1.0
DESCRIPTION "Build system for arsenalgear-cpp."
LANGUAGES CXX
)
# Error if building out of a build directory
file( TO_CMAKE_PATH "${PROJECT_BINARY_DIR}/CMakeLists.txt" LOC_PATH )
if( EXISTS "${LOC_PATH}" )
message( FATAL_ERROR "You cannot build in a source directory (or any directory with "
"CMakeLists.txt file). Please make a build subdirectory. Feel free to "
"remove CMakeCache.txt and CMakeFiles." )
endif()
# Other settings for paths
include_directories( . )
# Compile tests
add_subdirectory( test )
# Create static library
file( GLOB source src/*cpp )
add_library( arsenalgear STATIC ${source} )
# Installing headers
if( UNIX )
set( INSTALLATION_DIR_INCLUDE /usr/include )
elseif( APPLE )
set( INSTALLATION_DIR_INCLUDE /usr/local/include )
elseif( WIN32 )
set( WIN_INSTALLATION_DIR_INCLUDE "" CACHE STRING "Installation directory for Windows OSs." )
set( INSTALLATION_DIR_INCLUDE ${WIN_INSTALLATION_DIR_INCLUDE} )
endif()
INSTALL(
FILES include/containers.hpp include/math.hpp include/operators.hpp include/system.hpp include/stream.hpp include/type.hpp include/constants.hpp include/utils.hpp
DESTINATION ${INSTALLATION_DIR_INCLUDE}/arsenalgear
)
# Creating static libraries path
if( UNIX )
set( INSTALLATION_DIR_LIB /usr/lib )
elseif( APPLE )
set( INSTALLATION_DIR_LIB /usr/local/lib )
elseif( WIN32 )
set( WIN_INSTALLATION_DIR_LIB "" CACHE STRING "Installation directory for Windows OSs." )
set( INSTALLATION_DIR_LIB ${WIN_INSTALLATION_DIR_LIB} )
endif()
# Installing cmake package configuration files and libraries
INSTALL(
FILES build/libarsenalgear.a
DESTINATION ${INSTALLATION_DIR_LIB}
)
install(
FILES scripts/cmake/arsenalgearConfig.cmake
DESTINATION ${INSTALLATION_DIR_LIB}/cmake/arsenalgear
)
I will not post the content of the CMakeLists.txt
of the directory test
, since it is trivial.
I will post instead the content of scripts/cmake/arsenalgearConfig.cmake
which is important:
# Headers path
set( arsenalgear_INCLUDE_DIRS ${PREFIX}/include/arsenalgear)
# Libraries path
set( arsenalgear_LIBRARIES ${PREFIX}/lib/libarsenalgear.a)
The library is correctly compiled and if I want to use it with an external project (once I have installed it) I should do:
find_package( arsenalgear )
target_link_libraries( ${TARGET_NAME} PRIVATE arsenalgear )
And that's fine. However I would be able to link the library using the CMake namespace, in this way:
find_package( arsenalgear )
target_link_libraries( ${TARGET_NAME} PRIVATE arsenalgear::arsenalgear )
I searched all around the web and the StackOverflow community for similar questions and found many of them, but none solved my problem.
How should I modify my CMake files in order to satisfy my requirements? Thanks.
PS: if you want to suggest also general improvements to my CMake files you are more than welcome!