首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在cmake中使用cppclean

如何在cmake中使用cppclean
EN

Stack Overflow用户
提问于 2021-12-14 13:29:51
回答 1查看 114关注 0票数 0

在ubuntu上的cmake项目中,我试图使用CPPClean为所有目标查找未使用/不必要的头。然而,我不知道如何将它集成到我的项目中。

CPPClean的用法是

代码语言:javascript
复制
cppclean --include-path=directory1 --include-path=directory2 <path>

我想有一个目标,我可以建立,确实运行can在每一个目标,我标记。类似于:

代码语言:javascript
复制
add_library(foo_target
        src/foo.cpp     include/foo.hpp)

target_include_directories(foo_target PUBLIC include)

target_link_libraries(foo_target PUBLIC bar)

cppclean(foo_target) #this function adds the target to some kind of global list?

然后,执行the的构建目标必须执行以下操作

对于所有被标记的目标:

  • 获取目标
  • 的所有包含dirs,获取链接到
  • 的库的所有包含dirs,在目标主dir上运行cpp洁净逗号(我们可以假设它安装在系统上),并使用我们刚刚确定的

的包含dir。

如果这是一种下降的方式,有什么想法吗?我将如何实现这最后一部分?特别是如何让一个目标执行(未知)数量的命令?

EN

回答 1

Stack Overflow用户

发布于 2021-12-14 14:22:45

如果这是一种下降的方式,

有什么想法吗?

好的。

我将如何实现最后一部分?

好吧,就像您指定的那样,您将在目标"main dir“上运行cppclean命令,其中包含以前点的dirs。

,尤其是如何让一个目标执行(未知)数量的命令?

“目标”代表要做的事情。foo_target构建了目标,它不会做任何其他事情。用add_custom_target()定义另一个目标。

代码语言:javascript
复制
function(cppclean name target)
   # get all the include dirs of the target
   get_target_properties....
   # get all the include dirs of the libs that are linked to
   recursively, get target_properties for each library target links to
   See appriopriate stackoverflow thread.
   #  (which we can assume is installed on the system)
   find_program(cppclean NAMES cppclean REQUIRED)
   # run the cppclean commmand on the target main dir with the include dirs that we just determined
   add_custom_target(${name}
      COMMAND ${cppclean} args constructed from above
   )
endfunction()

在cmake中的应用

您还可以生成compile_commands.json,并从中提取每个可执行文件的包含路径。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70349679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档