Visual Studio 2010:使用define宏指定输出可执行文件

Visual Studio 2010: Using define macro to specify output executable

本文关键字:输出 可执行文件 define Studio 2010 使用 Visual      更新时间:2023-10-16

我需要使用在头文件中定义的#define宏,以便在输出文件的规范中使用。

为了指定我需要的内容,有一个带有#define VERSION 12条目的header.hpp文件,并希望输出myProgram_12.exe。有没有一种方法可以在Visual Studio 2010中实现这一点,而无需手动更改项目属性。

如果版本号的权威信息来源是#define,那么恐怕没有简单的解决方案,但可以用一个丑陋的变通方法来完成。

您可以创建一个工具,从标题中提取版本号,并将其写入Visual Studio属性表(.vsprops)文件,该文件将包含在您的项目中(在Property Manager中)。该工具实际上可以是一个C++程序,它将#include作为标头(或者至少启动这样一个程序来获得数字)。

当然,该工具可能应该缓存值,并且只在值不同时重新写入属性表,这样当您将该工具挂接到构建链中时,就不会得到不断的重建。

我可以建议您使用以下代码,在.h文件中

#define VERSION2

在.cpp文件中

' add reference to Microsoft.VisualStudio.VCProjectEngine
Imports EnvDTE
Imports Microsoft.VisualStudio.VCProjectEngine
Public Module Module1
    Sub Test()
        Dim prj As VCProject
        Dim cfgs, tools As IVCCollection
        Dim cfg As VCConfiguration
        Dim tool As VCLinkerTool
        prj = DTE.Solution.Projects.Item(1).Object
        cfgs = prj.Configurations
        cfg = cfgs.Item(1)
        tool = cfg.Tools("VCLinkerTool")
       #ifdef VERSION2
        tool.OutputFile = "$(ProjectName).exe"
        #ifdef VERSION3
        tool.OutputFile = "$(ProjectName1).exe"
    End Sub
End Module

它的VB代码,但您可以使用此链接轻松地将其转换为VC++代码如何:为项目模型扩展性编译示例代码