2

I have been using CMake's fetch content functionality quite a bit recently to have self-containing build scripts. So far it works quite nicely, consider the following simplified CMakeLists.txt file:

cmake_minimum_required(VERSION 3.16.0)
project(hdf5test LANGUAGES CXX)

include(FetchContent)
FetchContent_Declare(
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG release-1.11.0
)
if(WIN32)
  set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
endif()
FetchContent_MakeAvailable(googletest)

add_executable(hdf5test main.cpp)

This works nicely, googletest is build within my build/_deps/ directory and the final libraries are installed into build/lib/Debug (assuming a debug build here). No surprise here, and also my executable hdf5test (which is just a hello world file for the moment, i.e. no includes) compiles fine into build/Debug/.

I need to compile HDF5 now as well as one of my dependencies requires it in order to be compiled. So my CNakeLists.txt file would look like this (for simplicity, googletest is removed now):

cmake_minimum_required(VERSION 3.16.0)
project(hdf5test LANGUAGES CXX)

include(FetchContent)
FetchContent_Declare(
  hdf5
  GIT_REPOSITORY https://github.com/HDFGroup/hdf5.git
  GIT_TAG hdf5-1_13_1
)
FetchContent_MakeAvailable(hdf5)

add_executable(hdf5test main.cpp)

HDF5 is now built fine as well but messes up the build tree. First of all, the HDF5 libraries are all put into build/_deps/hdf5-build/bin/Debug but are no longer installed into the build/lib/Debug/ folder by default. THe more confusing thing, though, is that my defined executables like hdf5test are put in the same location (build/_deps/hdf5-build/bin/Debug) and this is messing with my build setup. As far as I can see, only HDF5 is changing this behaviour, googletest (and other tested libraries) do not change this.

Does anyone know how to force hdf5 to not change the default build directory and to install the libraries into build/lib/Debug once the build is completed (using fetch content, I can manage to get it installed correctly using the command line only with -DCMAKE_INSTALL_PREFIX) so that my executables are still put into build/Debug?

Note: After HDF5 is built, the CMakeCache.txt file now contains CMAKE_RUNTIME_OUTPUT_DIRECTORY:PATH=path/to/project/build/_deps/hdf5-build/bin

tom
  • 361
  • 3
  • 11
  • ".. and the final libraries are **installed** into build/lib/Debug" - You are using wrong terminology. "Installing" is associated with delivering artifacts to the final location, after which source and build directories could be safely removed. Installing is usually triggered by `install` target. Creation of the library under `build/lib/Debug/` is a usual **building** of the library. – Tsyvarev Apr 15 '22 at 13:07
  • hdf5 is built (through fetch content) into `build/_deps/package-build/bin/Debug/*.so` by default, after which all `*.so` files should be installed (or moved, whatever terminology works for you) into `build/lib/Debug`. You could safely delete everything in `build/_deps/*`, i.e. all compiled libraries and executables and your program will still work (though if you run it again, CMake would just generate a new `build/_deps/` folder). I'm not sure how this comment is helpful though in light of the question – tom Apr 15 '22 at 20:04

0 Answers0