Consecutive target_link_libraries with and without PUBLIC

cmake requires consecutive target_link_libraries invocations to use same syntax. E.g. the following will not work;

target_link_libraries(target PUBLIC A)
target_link_libraries(target B)

Any way to get around that?

target_link_libraries(target PUBLIC A)
target_link_libraries(target PRIVATE B)

You may not know that whether PUBLIC/PRIVATE was used, they may be called from different contexts.

Consider the following;

function(my_add_library library)
   add_library(${library} ${ARGN})
   target_link_libraries(${library} PRIVATE C)

my_add_library(A ...)
target_link_libraries(A B)

But I assume you always know how you want to link B, right? Or are you in a situation where you’re trying to express “link B into target with the same visibility with which A was linked into it”? That would be a very unusual requirement, I’d say.

Note that mixing visibilities is perfectly fine, even in the same command:

target_link_libraries(target PUBLIC A PRIVATE B C PUBLIC D)

thanks my issue has been fixed.