最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

c - How to export a static library linked to an interface library? - Stack Overflow

matteradmin9PV0评论

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).

Post a comment

comment list (0)

  1. No comments so far