简体   繁体   中英

How cmake judging target_link_libraries item is a library name or a target?

I have some troubles in cmake target_link_libraries function. In my case,there are three projects like this,which means A depends on B,B depends on C

A(exec) --> B(static library) --> C(static library alias C::C)

I write a CMakeLists.txt for B like this:

find_package(C REUQIRED)
add_library(B ...)
target_link_libraries(B PRIVATE C::C)

And it works well,complie successfully. A's cmake file like this:

find_package(B REQUIRED)
add_exectuable(A ...)
target_link_libraries(A B)

It reports error when linking A,cmake set a linker flags "-lC::C",and then ld said C::C not exist. How it happend?I think C::C is a target and A should know it,but cmake think it is a library name.And I think A should not know B depends on C,it's a private library.

I don't want to write find_packge(C) in A's cmake,because C is a static library,I think B should handle all dependcies of C,that's right?

So anyone knows how to fix it?

I solved this problem finally. In my case,B using C's funtion in template code,and B does not instantiate this template,so C::C would not link into B's static library.But A would instantiate B's template,so A need C::C,that's why B's PRIVATE flag was useless.

In order to help A found the C::C,I should use find_depecncy(C) in B's config.cmake to let A knows B's depency when use find_package(B).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM