Skip to content

Instantly share code, notes, and snippets.

@yashi
Created January 5, 2023 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yashi/f147cc961810e9ec0bf9be289a4a276c to your computer and use it in GitHub Desktop.
Save yashi/f147cc961810e9ec0bf9be289a4a276c to your computer and use it in GitHub Desktop.
Interface library with a generated header file
cmake_minimum_required(VERSION 3.25)
project(test)
add_executable(hello hello.c)
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/include/foo.h
# Ninja always implicitly creates target directories
COMMAND mkdir -p include/
COMMAND touch include/foo.h)
add_custom_target(generate_header
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/include/foo.h)
add_library(interface_lib INTERFACE)
# must use add_dependencies to a custom target
# see https://gitlab.kitware.com/cmake/cmake/-/issues/17366
add_dependencies(interface_lib generate_header)
target_include_directories(interface_lib INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/include/)
target_link_libraries(hello interface_lib)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment