[英]CMake: How to create a file with make command
I need to create the following always-out-of-date file: ${CMAKE_BINARY_DIR}/version.hpp.in 我需要创建以下永远过时的文件:$ {CMAKE_BINARY_DIR} /version.hpp.in
before any target is executed in the make file. 在make文件中执行任何目标之前。 The content of file is exactly as the following:
文件内容完全如下:
#define RELEASE_VERSION "${RELEASE}"
#define RELEASE_VERSION“$ {RELEASE}”
#define MAJOR_VERSION "${MAJOR}"#define MAJOR_VERSION“$ {MAJOR}”
#define MINOR_VERSION "${MINOR}"#define MINOR_VERSION“$ {MINOR}”
#define PATCH_VERSION "${PATCH}"#define PATCH_VERSION“$ {PATCH}”
#define REVISION "${REVISION}"#define REVISION“$ {REVISION}”
#define SVNPATH "${SVNPATH}"#define SVNPATH“$ {SVNPATH}”
I've got the following piece of code in my CMake file, but it's executed only after running the cmake
command: 我的CMake文件中有以下代码,但只有在运行
cmake
命令后才会执行:
FILE(WRITE ${CMAKE_BINARY_DIR}/version.hpp.in
"#define RELEASE_VERSION \"${RELEASE}\"\n"
"#define MAJOR_VERSION \"${MAJOR}\"\n"
"#define MINOR_VERSION \"${MINOR}\"\n"
"#define PATCH_VERSION \"${PATCH}\"\n"
"#define REVISION \"${REVISION}\"\n"
"#define SVNPATH \"${SVNPATH}\"\n"
)
I want to generate the version.hpp.in
file each time I run the make
command. 我想在每次运行
make
命令时生成version.hpp.in
文件。 How can I do that? 我怎样才能做到这一点?
You can use add_custom_command
(and perhaps add_custom_target
) to do this sort of thing. 您可以使用
add_custom_command
(可能还有add_custom_target
)来执行此类操作。
Since you're asking about building version symbols into your program, I will tell you about a slightly different method that I use. 既然您要求在程序中构建版本符号,我会告诉您一个与我使用的略有不同的方法。 I asked a question related to it here, with some code: Embed Git revision in executable during remote build with NetBeans
我在这里提出了一个与之相关的问题,其中包含一些代码: 在使用NetBeans进行远程构建期间,将Git修订版嵌入可执行文件中
The concept I use is to generate actual symbols in an object file directly from a textual file which I write at build (or git pull
) time. 我使用的概念是直接从我在build(或
git pull
)时间写的文本文件中生成目标文件中的实际符号。 Then I link this file into the executable. 然后我将此文件链接到可执行文件中。
You can use configure_file()
for this: 您可以使用
configure_file()
:
configure_file(version.hpp.in ${DESTPATH}/version.hpp)
Where DESTPATH
is optional and set to the path you want that file. 其中
DESTPATH
是可选的,并设置为您希望该文件的路径。
If the file is modified the build system will re-run CMake to re-configure the file and generate the build system again.
如果文件被修改,构建系统将重新运行CMake以重新配置文件并再次生成构建系统。
(Source: Documentation, see below) (来源:文档,见下文)
File version.hpp
: 文件
version.hpp
:
#define RELEASE_VERSION "${RELEASE}"
#define MAJOR_VERSION "${MAJOR}"
#define MINOR_VERSION "${MINOR}"
#define PATCH_VERSION "${PATCH}"
#define REVISION "${REVISION}"
#define SVNPATH "${SVNPATH}"
Syntax: 句法:
configure_file(<input> <output>
[COPYONLY] [ESCAPE_QUOTES] [@ONLY]
[NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])
Documentation: 文档:
# 'example' is the targetname here, eg. using add_executable(example example.cpp)
add_custom_command(TARGET example PRE_BUILD COMMAND <CMAKE COMMAND HERE>)
#define VERSION ${VERSION}
Just for testing … 仅供测试......
cmake_minimum_required(VERSION 2.8.12)
set(VERSION 1.10)
set(TEMPLATE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
add_executable(example example.cpp)
add_custom_command(TARGET example PRE_BUILD
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/SetVersion.cmake)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/../Version.h.in ${CMAKE_CURRENT_SOURCE_DIR}/../Version.h)
message("Version set") # For testing only
Now run cmake
and make
as usual and check if HELLO
is printed on make
. 现在运行
cmake
,并make
像往常一样,并检查是否HELLO
印在make
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.