Carlo Wood
Carlo Wood

Reputation: 6791

How to create a cmake libary project with config file?

I am completely stuck :/. I created a simple test "library" with two files fruits.cpp and fruits.h.

Then I created this CMakeLists.txt (following this tutorial)

cmake_minimum_required(VERSION 3.16)

project(fruits_Lib)

add_library(fruits_Lib STATIC)

target_sources(fruits_Lib
  PRIVATE
    "fruits.cpp"
)

set(include_dest "include/fruits-1.0")
set(main_lib_dest "lib/fruits-1.0")
set(lib_dest "${main_lib_dest}/${CMAKE_BUILD_TYPE}")

target_include_directories(fruits_Lib
  PUBLIC
    "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
    "$<INSTALL_INTERFACE:${include_dest}>"
)

add_library(fruits::fruits ALIAS fruits_Lib)

install(TARGETS fruits_Lib EXPORT fruits DESTINATION "${main_lib_dest}")
install(FILES "include/fruits/fruits.h" DESTINATION "${include_dest}")
install(EXPORT fruits DESTINATION "${lib_dest}")

This works in that it compiles and installs etc, and I can even use it as a add_subdirectory in a parent project.

In fact, the files that this installs are:

lib/fruits-1.0/libfruits_Lib.a
lib/fruits-1.0/Debug/fruits-debug.cmake
lib/fruits-1.0/Debug/fruits.cmake
lib/fruits-1.0/libfruits_Libd.a
lib/fruits-1.0/Release/fruits-release.cmake
lib/fruits-1.0/Release/fruits.cmake
include/fruits-1.0/fruits.h

However, when I try to do the following in the parent project:

find_package(fruits CONFIG REQUIRED)

I get the error:

CMake Error at CMakeLists.txt:21 (find_package):
  Could not find a package configuration file provided by "fruits" with any
  of the following names:

    fruitsConfig.cmake
    fruits-config.cmake

Those files are definitely not created or installed by the above CMakeLists.txt :/.

What am I doing wrong? How can I create a static library that provides such config file so that I can use it (after installation) with a find_package(fruits CONFIG) ?

Upvotes: 0

Views: 184

Answers (1)

Shlomi Fish
Shlomi Fish

Reputation: 4500

I think find_package() is for locating and using external and already installed components/libraries so you'll have to install the library first.

Upvotes: 1

Related Questions