[CMake] Proposal: extra option PRIVATE for target_link_libraries

Michael Wild themiwi at gmail.com
Wed Dec 23 07:09:33 EST 2009


On 23. Dec, 2009, at 12:08 , Marcel Loose wrote:

> Hi all,
> 
> I suggested this in the quite long thread "third party library
> dependencies", but it may have been overlooked. Hence, I started a new
> thread.
> 
> Upon (re)reading the Mandriva page
> http://wiki.mandriva.com/en/Overlinking, I was thinking: maybe the issue
> of overlinking can be solved more or less the same way as pkg-config
> does: i.e. by defining private dependencies. This could be an extra
> option to target_link_libraries. 
> Something like:
> 
>  target_link_libraries(mylib public1 public2 PRIVATE private1 private2)
> 
> This would tell CMake that mylib directly depends on public1 and public2
> and should only link in these two libraries when these are shared
> object libraries; otherwise private1 and private2 would also need to be
> added on the link line.
> 
> The big hurdle to take, of course, is to detect in a
> platform-independent way whether the given library is shared or static.
> However, a lot of this knowledge is already available in the diverse
> Modules/Platform macros, so my feeling is that this should be feasible.
> 
> Best regards,
> Marcel Loose.
> 

You would also need a PUBLIC keyword, and then require that all FindXXX.cmake modules prefix their libraries with PUBLIC and PRIVATE. If only the PRIVATE libraries where prefixed, the following would not work if A_LIBRARIES contains PRIVATE:

find_package(A)
find_package(B)
add_library(C source.c)
target_link_libraries(C ${A_LIBRARIES} ${B_LIBRARIES})

Because then all the B_LIBRARIES would be considered to be private "details" of the public A_LIBRARIES... Also, there's no way to tell CMake which of the private libraries belongs to which of the public libraries.

I think it would be better if a FindXXX.cmake module marked the private libraries as a property of the public libraries and then let CMake take it from there (although as of now I wouldn't know on what to set the property, except if the module created an IMPORTED target for each of the public libraries, but that bears the possibility of target name collisions with the importing project).

Michael


More information about the CMake mailing list