Linking CMake interface libraries with object libraries
Starting from cmake 3.13, object libraries can "link" to other libraries to inherit their usage requirements (doc).
So the example CMakeLists.txt from the question should get the correct definition at compile time.
Don't forget to set cmake_required(VERSION 3.13) if you use this!
Just copy the compile definitions from the interface library directly via properties. The information is there, just no direct support for it via the usual commands:
cmake_minimum_required(VERSION 3.1)
project(my_question)
add_library(interface_lib INTERFACE)
target_compile_definitions(interface_lib INTERFACE MY_DEF)
add_library(object_lib OBJECT a.cpp)
target_compile_definitions(object_lib PUBLIC
$<TARGET_PROPERTY:interface_lib,INTERFACE_COMPILE_DEFINITIONS>)
add_library(main_lib b.cpp)
target_sources(main_lib PRIVATE
$<TARGET_OBJECTS:object_lib>)
Note target_sources()
was first introduced in version 3.1 not 3.0.1 it seems. It may be a good idea to update your cmake_minimum_required
version.