如何在编译时将正确的版本信息注入资源? [英] How to inject the right version information into the resources at compile time?

查看:177
本文介绍了如何在编译时将正确的版本信息注入资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶地发现,显然无法在资源文件(.rc)中导入C预定义的宏,因为Resource Compiler无法处理它们.

I was surprised to discover that apparently it is not possible to import C predefined macros inside the resource files (.rc) because Resource Compiler is not able to deal with them.

我试图将版本信息放入version.h中,该信息将由构建系统生成/更新.该文件应该包含在resource.rc中,因此,在构建资源时,所有构建的文件中始终会得到相同的版本.

I was trying to put the version information inside a version.h that would be generated / updated by the build system. This file was supposed to be included from the resource.rc so when you build the resources you will always get the same versions across all the built files.

似乎这与RC_INVOKED和此错误 http://connect.microsoft.com/VisualStudio/feedback/details/532929/rc4011-warnings-from-vc10-rc -已按按设计要求"关闭.

It seems that this has something to do with RC_INVOKED and this bug http://connect.microsoft.com/VisualStudio/feedback/details/532929/rc4011-warnings-from-vc10-rc -- which is closed as "as-designed".

我该如何解决这个问题?

How can I solve this problem?

是修补最终exe以便更新版本信息的唯一选择吗? ...我宁愿不这样做,而采用更标准的方式.

Is the only option to patch the final exe in order to update the version information? ... I would prefer not do to this and use a more standard way for this.

推荐答案

资源编译器可以很好地处理包含和预处理程序定义.例如,在包括Windows.h方面并不能很好地处理.但是我想不出任何的充分理由,为什么您需要在资源编译器使用的文件中使用它.只需使用不包含引起警告的任何内容的头文件,然后定义所需的内容即可.例如,我们在此处使用的典型版本控制可以做到这一点,并且效果很好:有一个 single 主.rc文件,它看起来像这样:

The resource compiler deals just fine with includes and preprocessor definitions. It just does not deal well with including Windows.h for instance. But I cannot think of any good reason why you'd need that in a file that gets consumed by the resource compiler. Just use a header file that does not include anything causing the warning, and just define what you need. As an example the typical versioning we use here does this and works great: there's a single master .rc file with that looks something like this:

#include <winver.h>

#define stringize( x )        stringizei( x )
#define stringizei( x )       #x

#ifdef VRC_INCLUDE
  #include stringize( VRC_INCLUDE )
#endif

#ifdef _WIN32
  LANGUAGE 0x9,0x1
  #pragma code_page( 1252 )
#endif

1 VERSIONINFO
 FILEVERSION    VRC_FILEVERSION
 PRODUCTVERSION VRC_PRODUCTVERSION
 FILEFLAGSMASK  0x1L
 FILEFLAGS      VS_FF_DEBUG
 FILEOS         VOS__WINDOWS32
 FILETYPE       VRC_FILETYPE
BEGIN
  BLOCK "StringFileInfo"
  BEGIN
    BLOCK "040904E4"
    BEGIN
      VALUE "CompanyName",      stringize( VRC_COMPANYNAME )
      VALUE "FileDescription",  stringize( VRC_FILEDESCRIPTION )
      VALUE "FileVersion",      stringize( VRC_FILEVERSION )
      VALUE "LegalCopyright",   stringize( VRC_COPYRIGHT )
      VALUE "InternalName",     stringize( VRC_ORIGINALFILENAME )
      VALUE "OriginalFilename", stringize( VRC_ORIGINALFILENAME )
      VALUE "ProductName",      stringize( VRC_PRODUCTNAME )
      VALUE "ProductVersion",   stringize( VRC_PRODUCTVERSION )
    END
  END
  BLOCK "VarFileInfo"
  BEGIN
    VALUE "Translation", 0x409, 1200
  END
END

从这里开始,可能性几乎是无限的.将VRC_INCLUDE定义为包含所有VRC_...定义的包含文件的完整路径:

From here on the possibilities are pretty much unlimited. Either define VRC_INCLUDE to the full path of an include file containing all the VRC_... definitions:

rc /d VRC_INCLUDE=$(VersionMainInclude) ... version.rc

或提供所有定义

rc /d VRC_COMPANYNAME=mycompany ... version.rc

或两者的组合.

为了向您展示各种可能性,以下是我目前正在对所有使用git版本化的项目进行的操作:

To show you the possibilities, here's what I'm currently doing for all projects versioned with git:

  • 每个项目都有一个version.h#仅定义一个简短的VRC_FILEDESCRIPTION和VRC_FILEVERSION
  • 有一个主版本.h#定义VRC_COMPANYNAME/VRC_COPYRIGHT/...
  • 该项目包括一个.targets文件,该文件在预构建事件中创建一个version.res.
  • msbuild prebuild事件处理了一些有趣的事情:它创建了一个新的临时头文件,将其他两个文件合并在一起,并使用了简短的git SHA和当前数据,并将其附加到文件描述字符串中,以便最终看起来像

  • every project has a version.h #defining just a short VRC_FILEDESCRIPTION and VRC_FILEVERSION
  • there's a master version.h #defining VRC_COMPANYNAME/VRC_COPYRIGHT/...
  • the project includes a .targets file that creates a version.res in a prebuild event
  • the msbuild prebuild event takes care of the interesting stuff: it creates a new temporary header file combining the other two, takes the short git SHA and the current data and appends that to the file description string so it ends up looking like

Foo Dll [12e454re 30/07/2013]

这篇关于如何在编译时将正确的版本信息注入资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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