0

I am trying to get a basic cmake project to work with 2 subprojects (each a library) where one depends on the other.

As such I have a self-contained project for lib1 and a self-contained project for lib2 (but that depends on lib1). Some lib2 authors could while others will not have access to lib1 source. If no access is desired, it seems that building and installing one after the other is the way to go. Is there a way to build them in one go using cmake? Currently, I am trying using an overall project, libs.

However, as the libraries are related I want to call them in a boost / poco like fashion. That is for lib2 I would like to write:

find_package(libs COMPONENTS lib1 REQUIRED)

target_link_libraries(lib2 PUBLIC libs::lib1)

I have created config files for both lib1 and lib2 and libs. However, I keep getting the same error

CMake Error at libs/lib2/CMakeLists.txt:67 (find_package):
By not providing "Findlibs.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "libs", but
CMake did not find one.

Could not find a package configuration file provided by "libs" with any of
the following names:

libsConfig.cmake
libs-config.cmake

Add the installation prefix of "libs" to CMAKE_PREFIX_PATH or set
"libs_DIR" to a directory containing one of the above files.  If "libs"
provides a separate development package or SDK, be sure it has been
installed.

I don't understand where to put these files. It searches for them under prefix/ etc but at the time of building none of these libraries is yet installed. What is the proper place to put the -config.cmake files? I have put them all in

${CMAKE_BINARY_DIR}/libs

which seems similar to Poco, but the error remains. How can the libraries find each other?

// update:

From the comments I understand that lib1 first needs to be build in order to use find_package. Is there a way (in cmake) to

  1. build lib1, install it, build lib2
  2. or is this better to do using a bash / python / etc script?
Mike
  • 3,775
  • 8
  • 39
  • 79
  • "However, as the libraries are related I want to call them in a boost / poco like fashion." - Using `find_package` is absolutely not implied by "relation" between the libraries. Using `find_package` expects that the package is **already installed**. If you build `lib1` and `lib2` together, then you definitely have no installed `libs` package which contains given libraries. Just use `target_link_libraries(lib2 PUBLIC lib1)`. – Tsyvarev Aug 25 '21 at 16:11
  • If you want just to add "namespace" to the library which you are building, then use ALIAS library: `add_library(lib2 STATIC ...)`, `add_library(libs::lib2 ALIAS lib2)`. – Tsyvarev Aug 25 '21 at 16:13
  • I have used the alias library. I have updated the question to give more background on what I want to achieve. – Mike Aug 26 '21 at 10:05
  • It is up to your whether to manually install `lib1` an then build `lib2` or to create a script which performs given actions. – Tsyvarev Aug 26 '21 at 10:16
  • I was wondering if you can "script" it in cmake or need to use other tools. I was not planning on doing it manually. – Mike Aug 26 '21 at 10:26
  • 1
    In CMake you could run external commands using [execute_process](https://cmake.org/cmake/help/latest/command/execute_process.html). E.g. `execute_process(COMMAND ${CMAKE_COMMAND} /path/to/source/lib1 WORKING_DIRECTORY /path/to/build/dir/lib1)` will configure `lib1` library into given directory. – Tsyvarev Aug 26 '21 at 10:51

0 Answers0