簡體   English   中英

cmake如何判斷target_link_libraries項是庫名還是目標?

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

我在 cmake target_link_libraries function 中遇到了一些麻煩。 在我的情況下,有三個這樣的項目,這意味着 A 依賴於 B,B 依賴於 C

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

我為 B 寫了一個 CMakeLists.txt,如下所示:

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

它運行良好,成功編譯。 A的cmake文件如下:

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?

所以有人知道如何解決嗎?

我終於解決了這個問題。 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 的 PRIVATE 標志毫無用處。

為了幫助A找到C::C,我應該在B的config中使用find_depecncy(C)。cmake讓B知道什么時候使用find_package()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM