Remove linker flag needed by library, but not by test programs

I am migrating a project from autotools to CMake. One part is a static library with version information and a few testprograms in a subdirectory. So I tried:

add_library(wraster STATIC ${SOURCES})
target_link_libraries(wraster PRIVATE -version-info=${WRASTER_VERSION})

and in the subdirectory:

add_executable(testdraw testdraw.c)
target_link_libraries(testdraw PRIVATE wraster)

The library gets built, but unfortunately CMake adds the linker flags from the library also to the testprograms which causes an error, because of the version information:

cc: error: unrecognized command-line option ‘-version-info=6:0:0’

(The compiler flags are set for the programs as well)
How can I avoid this without removing the version info?

TIA and best regards - Andreas

  • First, to set link options, use target_link_options rather than target_link_libraries.
  • Second, target_link_options is not usable with static libraries.

In your case, you have to use STATIC_LIBRARY_OPTIONS target property:

add_library(wraster STATIC ${SOURCES})
set_property (TARGET wraster PROPERTY STATIC_LIBRARY_OPTIONS "-version-info=${WRASTER_VERSION}")