cmake对不必要的链接做什么

huangapple go评论57阅读模式
英文:

What does cmake do with Unnecessary links

问题

  1. 我在清理/重构一个相当古老的项目,我意识到他们犯了一个错误(?),在所有的CMakeLists.txt文件中添加了不必要的链接:
LIST(APPEND ${PROJECT_NAME}_LIBRARIES RecursiveLy_generated_list_with_unnecessarystuff)
target_link_libraries(sample PUBLIC ${${PROJECT_NAME}_LIBRARIES})

我用一个仅包含必要链接的列表替换了原列表,编译时间明显减少了。

  1. 所以我想知道,当链接的库的符号未被使用时,cmake会做什么?

  2. 我还清理了包含目录(项目的所有.h文件都被递归添加了(许多是不需要的)),cmake是否认为这些符号被使用,因为它们出现在头文件中,即使这些头文件没有被#include进来?

英文:

I'm cleaning/refactoring a quite old project and I realized they made the mistake(?) of adding unnecessary links to the targets in all the CMakeLists.txt files:

LIST(APPEND ${PROJECT_NAME}_LIBRARIES RecursiveLy_generated_list_with_unnecessarystuff)
target_link_libraries(sample PUBLIC ${${PROJECT_NAME}_LIBRARIES})

I replaced the list by one that contains only the necessary links and the compilation time got noticeably reduced.

  1. So I'm wondering what does cmake do when the symbols of a library that was linked are not used?

  2. I also cleaned the include directories (ALL the .h files of the project were recursively added (many not needed)), did cmake think the symbols were used because they appeared in the headers even if this headers were not #included ?

答案1

得分: 2

> "cmake处理不必要的链接时会执行什么操作"
将正确的命令行选项传递给编译器。

> "当链接的库的符号未被使用时,cmake会执行什么操作"
什么也不做。CMake甚至不知道库的符号是否被使用。CMake不会链接您的代码,编译器会。

> "是否因为这些符号出现在头文件中而未被包含在内,CMake会认为这些符号被使用了吗?"
不,CMake不知道符号在何处被使用。CMake不了解头文件的内容。CMake创建要运行的命令列表。要运行哪些命令取决于CMakeLists.txt中的内容。

英文:

> What does cmake do with Unnecessary links

Passes proper command line options to the compiler.

> what does cmake do when the symbols of a library that was linked are not used?

Nothing. CMake is not even aware that symbols of a library are or are not used. CMake does not link your code, compiler does.

> did cmake think the symbols were used because they appeared in the headers even if this headers were not #included ?

No, CMake is unaware of what symbols are used where. CMake is unaware of the content of header files. CMake creates a list of commands to run. What commands to run depends on what is written in CMakeLists.txt.

huangapple
  • 本文由 发表于 2023年2月9日 00:17:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定