target_link_libraries( … … …)
https://cmake.org/cmake/help/latest/command/target_link_libraries.html#target-link-libraries
target_link_libraries为target指定链接的库,有时候想要携带库本身的编译参数。保证看到的头文件是一致的。
可以使用方法:
get_target_property(defs some_lib COMPILE_DEFINITIONS)target_compile_definitions(target PUBLIC ${defs})
为了方便使用,可以封装函数:
function(target_link_libraries_with_compile_definitions target)set(options)set(oneValueArgs LINK_VISIBILITY DEPEND_VISIBILITY EXPORT_VISIBILITY)set(multiValueArgs LIBRARIES)cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})target_link_libraries(${target} ${ARG_LINK_VISIBILITY} ${ARG_LIBRARIES})set(compile_definitions)foreach (library ${libraries})get_target_property(defs ${library} ${ARG_DEPEND_VISIBILITY})set(compile_definitions ${compile_definitions} ${defs})endforeach ()target_compile_definitions(${target} ${ARG_EXPORT_VISIBILITY} ${compile_definitions})
endfunction()function(target_link_libraries_with_definitions target)# param: visibilityset(visibility ${ARGV1})# param: librariesset(visibilities PUBLIC PRIVATE INTERFACE)if (${visibility} IN_LIST visibilities)list(SUBLIST ARGV 2 -1 libraries)else ()list(SUBLIST ARGV 1 -1 libraries)endif ()target_link_libraries_with_compile_definitions(${target}LINK_VISIBILITY PUBLICDEPEND_VISIBILITY COMPILE_DEFINITIONSEXPORT_VISIBILITY PUBLICLIBRARIES ${libraries})
endfunction()