简体   繁体   English

如何节省 C++ DLL 的构建时间

[英]How to save the build time of a C++ DLL

My project is a C++ DLL (cmake project), that expose methods from ac interface to another C# project, I want to add a methode getBuildTime() in the c interface that return the buildtime (the exact date and time) so from the C# project we can know when the DLL was built.我的项目是一个 C++ DLL(cmake 项目),它将方法从 ac 接口公开到另一个 C# 项目,我想在 c 接口中添加一个方法 getBuildTime() 返回构建时间(确切的日期和时间),因此来自 C#我们可以知道 DLL 是什么时候构建的。
Is there a way to do this stuff ?有没有办法做到这一点?
I don't know if this useful or no but I'm using git as a version control system我不知道这是否有用,但我使用 git 作为版本控制系统

Build time in C/C++ code can be obtained by built-in macros __DATE__ and __TIME__ , eg do something like string build_time = __DATE__ " " __TIME__; C/C++ 代码中的构建时间可以通过内置宏__DATE____TIME__ ,例如像string build_time = __DATE__ " " __TIME__; . .

Also file which contains these macros usage needs to be touch ed before each build to change file's modify time.还需要在每次构建之前touch包含这些宏用法的文件以更改文件的修改时间。 touch is a unix command, which can be avaialble in Windows too through Cygwin or other means. touch是一个 unix 命令,它也可以通过Cygwin或其他方式在 Windows 中使用。 This is needed to force it to recompile to use new date.这是强制它重新编译以使用新日期所必需的。 Do file touching by adding touch build_time.cpp command to Pre-Build Events, they should be located in your project config somewhere.通过将touch build_time.cpp命令添加到预构建事件来执行文件触摸,它们应该位于您的项目配置中的某个地方。 Alternatively you may add touch ing to your make file.或者,您可以在 make 文件中添加touch Altertnatively some compile environments and possibly MSVC too can configure which files to rebuild always which is tweaked inside project's settings.或者,某些编译环境和可能的 MSVC 也可以配置始终重建哪些文件,这些文件在项目设置中进行了调整。

Also if you use git version control system then you might want to use last git commit hash and time together or instead of build time above.此外,如果您使用git版本控制系统,那么您可能希望同时使用上次 git commit 哈希和时间,或者代替上面的构建时间。 Git commit hash and time is sometimes better than to use build time like suggested above, because git hash and time remain same on each re-compile before next commit, this ensures that your DLL build time changes only when code changes/committed, this may help to have reproducible DLL compiles. Git 提交哈希和时间有时比使用上面建议的构建时间更好,因为 git 哈希和时间在下一次提交之前的每次重新编译时保持不变,这确保您的 DLL 构建时间仅在代码更改/提交时更改,这可能有助于进行可重现的 DLL 编译。 To support git hash and time do next things:要支持 git hash 和 time,请执行以下操作:

If you have unix echo and bash and git commands in your system eg by installing Cygwin or new native Windows SubSystem for Linux then you may do next things in command script before_build.cmd that you run before build, also this shell script can be run on Unix systems almost without modifications:如果您的系统中有 unix echobashgit命令,例如通过安装Cygwin或用于 Linux 的新本机 Windows 子系统,那么您可以在构建之前运行的命令脚本before_build.cmd中执行下一步操作,也可以在该 shell 脚本上运行Unix 系统几乎没有修改:

linux_echo -n "" > cfg.h

linux_echo -n "#define GIT_COMMIT """ >> cfg.h
linux_bash -c "echo -n $(git rev-parse --short=8 HEAD)" >> cfg.h
linux_echo """" >> cfg.h

linux_echo -n "#define GIT_COMMIT_TIME """ >> cfg.h
linux_bash -c "echo -n $(git show -s --format=%%ci $(git rev-parse --short=8 HEAD) | tr -d '\n')" >> cfg.h
linux_echo """" >> cfg.h

linux_echo -n "#define COMPILE_TIME """ >> cfg.h
linux_bash -c 'echo -n $(date +"%%Y-%%m-%%d %%H:%%M:%%S %%z")' >> cfg.h
linux_echo """" >> cfg.h

and you will get cfg.h like:你会得到cfg.h像:

#define GIT_COMMIT "fe0a7891"
#define GIT_COMMIT_TIME "2020-05-13 17:42:55 +0100"
#define COMPILE_TIME "2020-05-13 18:20:28 +0100"

these config file and macros you can use in your code.您可以在代码中使用这些配置文件和宏。

The C++ preprocessor predefines several macros (backward compatible with C) including __DATE__ and __TIME__ . C++ 预处理器预定义了几个宏(与 C 向后兼容),包括__DATE____TIME__ These expand to string literals containing the date and time of the build (in particular, the date and time that the preprocessor runs on that particular file).这些扩展为包含构建日期和时间的字符串文字(特别是预处理器在该特定文件上运行的日期和时间)。

You may need to force the compilation unit containing them to be rebuilt on every change.您可能需要强制在每次更改时重建包含它们的编译单元。 With make that can be done with .PHONY , with cmake it looks like add_custom_target is the ticket.使用make可以使用.PHONY完成,使用 cmake 看起来add_custom_target是票。

Add a target with no output so it will always be built.添加一个没有输出的目标,所以它总是会被构建。


Besides using __DATE__ and __TIME__ to capture when the compiler ran, the linker will store a timestamp in the PE header of the DLL it creates.除了使用__DATE____TIME__来捕获编译器何时运行之外,链接器还会在它创建的 DLL 的 PE 标头中存储一个时间戳。 There's no configuration necessary to make this happen, but then you have to write code to read the PE header and convert the timestamp to your favorite date and time format.实现这一点不需要任何配置,但是您必须编写代码来读取 PE 标头并将时间戳转换为您喜欢的日期和时间格式。

The preprocessor solution is way simpler, but the following approach may be convenient, if you need to provide other properties of the build system as well.预处理器解决方案更简单,但如果您还需要提供构建系统的其他属性,以下方法可能会很方便。

Create a cmake script file that generates a source file.创建一个生成源文件的 cmake 脚本文件。 Use this script to generate the source file providing the info:使用此脚本生成提供信息的源文件:

generate_timestamp.cmake generate_timestamp.cmake

#parameter: OUTPUT_FILE
string(TIMESTAMP _TIMESTAMP "%Y-%m-%dT%H:%M:%S")
file(WRITE ${OUTPUT_FILE} "#include \"buildtime.h\"
const char* getBuildTime() {
    return \"${_TIMESTAMP}\";
}")

CMakeLists.txt CMakeLists.txt

set(_BUILD_TIME_FILE buildtime.cpp)

add_library(mylib ${_BUILD_TIME_FILE} ...)
set_source_files_properties(${_BUILD_TIME_FILE} PROPERTIES GENERATED 1)

get_filename_component(_BUILD_TIME_FILE_ABSOLUTE ${_BUILD_TIME_FILE} ABSOLUTE)

# generate source using cmake script file
add_custom_target(buildtime_info COMMAND "${CMAKE_BUILD_TOOL}" -D "OUTPUT_FILE=${_BUILD_TIME_FILE_ABSOLUTE}" -P generate_timestamp.cmake)

# generate file before building mylib
add_dependencies(mylib buildtime_info)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM