I have successfully built and exported a shared library of mine by following the approach of putting the compiler flags into a separate INTERFACE library, as suggested in the CMake tutorial.
I also need an exportable static version of my library but I am struggling to make the things work. Please consider the following CMakeFile.txt
file that is a minimal reproducible example of my problem:
cmake_minimum_required(VERSION 3.21)
project(my_project LANGUAGES C)
# Example of interface library for compiler settings
add_library(compiler_flags INTERFACE)
target_compile_options(compiler_flags INTERFACE "-Wall")
# Real library
add_library(Foo STATIC foo.c) # It works with "SHARED" but not with "STATIC"
target_link_libraries(Foo PRIVATE compiler_flags)
# Install the real library
install(TARGETS Foo EXPORT FooTargets)
# Install the configuration targets
install(
EXPORT FooTargets
NAMESPACE Foo::
FILE FooTargets.cmake
DESTINATION lib/cmake/Foo)
When I run cmake -B build
, I get:
CMake Error: install(EXPORT "FooTargets" ...) includes target "Foo" which requires target "compiler_flags" that is not in any export set.
How can I make it work while keeping the compiler options in a separate INTERFACE library?
PS: I also looked at the static vs shared example in the official tutorial but, in that scenario, the static library is not directly exported (i.e., it is a private component of the exported shared library).