让CMake声明目标虚假 [英] Get CMake to declare a target phony

查看:82
本文介绍了让CMake声明目标虚假的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一些 compile 时间常数.对另一个问题的第一个答案使我非常接近.从我的CMakeLists.txt中:

I want to generate some compile time constants. The first answer to another question gets me quite close. From my CMakeLists.txt:

add_library(${PROJECT_NAME} STATIC ${CXX_SRCS} compile_time.hpp)
add_custom_command(OUTPUT compile_time.hpp
    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/compile_time.cmake)

从某种意义上来说,这是有效的,因为我第一次运行 make 时,它会生成 compile_time.hpp ,以便在我运行 make ,而不是 cmake .但是,当我重新运行 make 甚至是 cmake 重做makefile时, compile_time.hpp 并没有被重新制作.

This works in the sense that the first time I run make, it generates compile_time.hpp, so that the values of the variables are defined when I run make and not cmake. But compile_time.hpp is not remade when I rerun make or even cmake to redo the makefiles.

如何将目标 compile_time.cpp 标记为 phony ,以便始终重制目标?我尝试过

How can I make the target compile_time.cpp be marked as phony so that it is always remade? I tried

add_custom_target(compile_time.hpp)

无效.

推荐答案

add_custom_target 创建一个伪"目标:它没有输出,并且始终被构建.要使某些目标依赖于假"对象,请使用 add_dependencies()调用:

add_custom_target creates a "phony" target: It has no output and is always built. For make some target depended from the "phony" one, use add_dependencies() call:

add_custom_target(compile_time
    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/compile_time.cmake
)
# Because we use *target-level* dependency, there is no needs in specifying
# header file for 'add_library()' call.
add_library(${PROJECT_NAME} STATIC ${CXX_SRCS})
add_dependencies(${PROJECT_NAME} compile_time)

头文件扫描将自动检测到

头文件 compile_time.h 中库的依赖性.因为脚本 compile_time.cmake 无条件更新此标头,所以每次都将重建该库.

Library's dependency from the header compile_time.h will be detected automatically by headers scanning. Because script compile_time.cmake updates this header unconditionally, the library will be rebuilt every time.

这篇关于让CMake声明目标虚假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆