0

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!

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Gianluca Bianco
  • 656
  • 2
  • 11
  • 2
    "Namespaced" name `arsenalgear::arsenalgear` is actually name of the IMPORTED target. See e.g. [that answer](https://stackoverflow.com/a/10550334/3440745) about creating such target. Actually, what you are doing with `arsenalgearConfig.cmake` script is named as **package exporting**, and CMake provides much better(and simpler) ways for doing that. See e.g. "Adding Export Configuration" step in their [tutorial](https://cmake.org/cmake/help/latest/guide/tutorial/Adding%20Export%20Configuration.html). – Tsyvarev Dec 01 '22 at 11:33
  • @Tsyvarev thanks for the resources. However the first link doesn't answer to my question. I want to be able to link an external target with the library I created, using the namespace. If I do this with the previously mentione CMakeLists.txt file I get errors. – Gianluca Bianco Dec 01 '22 at 14:51
  • 1
    Add to your `arsenalgearConfig.cmake` file lines `add_library(arsenalgear::arsenalgear STATIC IMPORTED)` and `set_target_properties(arsenalgear::arsenalgear PROPERTIES IMPORTED_LOCATION ${PREFIX}/lib/libarsenalgear.a)`, so the target `arsenalgear::arsenalgear` becomes usable for link with. – Tsyvarev Dec 01 '22 at 14:54
  • Thanks! If you want to make a complete answer I can select it as the right one @Tsyvarev – Gianluca Bianco Dec 01 '22 at 14:57

0 Answers0