代码改变世界

[原创]自定义VS2010项目文件vcxproj动态管理inc lib

2011-12-15 20:02  Scott Guthrie Liu  阅读(1251)  评论(0编辑  收藏  举报

.vcxproj 是关键
A guide to .vcxproj and .props file structure
基本框架

.vcxproj基本框架
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns='http://schemas.microsoft.com/developer/msbuild/2003' >

<ItemGroup Label="ProjectConfigurations" />

<PropertyGroup Label="Globals" />

<Import Project="$(VCTargetsPath)\Microsoft.Cpp.default.props" />

<PropertyGroup Label="Configuration" />

<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />

<ImportGroup Label="ExtensionSettings" />

<ImportGroup Label="PropertySheets" />

<PropertyGroup Label="UserMacros" />

<PropertyGroup />

<ItemDefinitionGroup />

<ItemGroup />

<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

<ImportGroup Label="ExtensionTargets" />

</Project>

基本框架解释

基本框架解释
<!-- This is the root node. It specifies the MSBuild version to use

and also the default target to be executed when this file is passed

to MSBuild.exe
-->

<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns='http://schemas.microsoft.com/developer/msbuild/2003' >

<!-- This contains the project configurations known to the project

(such as Debug|Win32 and Release|Win32).
-->

<ItemGroup Label="ProjectConfigurations" />

<!-- This contains project level settings such as ProjectGuid,

RootNamespace, etc. These properties are not normally overridden

elsewhere in the project file. This group is not configuration

dependent and so only one Globals group generally exists in the

project file.
-->

<PropertyGroup Label="Globals" />

<!-- This property sheet contains the default settings for a VC++

project. It contains definitions of all the project settings such

as Platform, PlatformToolset, OutputPath, TargetName, UseOfAtl,

etc. and also all the item definition group defaults for each known

item group. In general, properties in this file are not tool-specific.
-->

<Import Project="$(VCTargetsPath)\Microsoft.Cpp.default.props" />

<!-- This property group has an attached configuration condition

(such as Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'")

and comes in multiple copies, one per configuration. This property

group hosts configuration-wide properties. These properties control

the inclusion of system property sheets in Microsoft.Cpp.props.

E.g. if we define the property <CharacterSet>Unicode</CharacterSet>,

then the system property sheet microsoft.Cpp.unicodesupport.props

will be included (as can be seen in the Property Manager). Indeed,

in one of the imports of Microsoft.Cpp.props, we can see the line:

<Import Condition="'$(CharacterSet)' == 'Unicode'" Project="$(VCTargetsPath)\microsoft.Cpp.unicodesupport.props"/>
-->

<PropertyGroup Label="Configuration" />

<!-- This property sheet (directly or via imports) defines the

default values for many tool-specific properties such as the

compiler’s Optimization, WarningLevel properties, Midl tool’s

TypeLibraryName property, etc. In addition, it imports various

system property sheets based on configuration properties defined

in the property group immediately above.
-->

<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />

<!-- This group contains imports for the property sheets that

are part of Build Customizations (or Custom Build Rules as this

feature was called in earlier editions). A Build Customization

is defined by up to three files – a .targets file, a .props file

and .xml file. This import group contains the imports for the

.props file.
-->

<ImportGroup Label="ExtensionSettings" />

<!-- This group contains the imports for user property sheets.

These are the property sheets you add through the Property

Manager view in VS. The order in which these imports are listed

is relevant and is reflected in the Property Manager. The project

file normally contains multiple instances of this kind of import

group, one for each project configuration.
-->

<ImportGroup Label="PropertySheets" />

<!-- UserMacros are as variables used to customize your build

process. E.g. you can define a user macro to define your custom

output path as $(CustomOutpuPath) and use it to define other

variables. This property group houses such properties. Note that

in VS2010, this group is not populated by the IDE since we do not

support user macros for configurations (we do for property sheets

though).
-->

<PropertyGroup Label="UserMacros" />

<!-- This property group normally comes with a configuration

condition attached. You will also see multiple instances of the

property group, one per configuration. It differs in its identity

from the other property groups above by the fact that it does

not have a label (to look at it in another way, it has a label

that is equal to the empty string). This group contains project

configuration level settings. These settings apply to all files

that are part of the specified item group. Build Customization

item definition metadata also gets initialized here.
-->

<PropertyGroup />

<!-- Similar to the property group immediately above, but it

contains item definitions and item definition metadata instead

of properties.
-->

<ItemDefinitionGroup />

<!-- Contains the items (source files, etc.) in the project.

You will generally have multiple item groups – one per item

type.
-->

<ItemGroup />

<!-- Defines (directly or via imports) VC++ targets such as

build, clean, etc.
-->

<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

<!-- This group contains imports for the Build Customization

target files.
-->

<ImportGroup Label="ExtensionTargets" />

</Project>

参考王志科 http://www.cnblogs.com/WonKerr/tag/VCXPROJ/

[转]Visual Studio 2010 C++ 工程文件解读
一、工程文件分为三个主要部分:初始化配置信息、默认配置信息和本地配置信息;
二、配置信息全部采用属性文件的方式进行组织,并且可以通过“导入”进行引用;
三、属性文件中可以包含工程属性、编译属性和链接属性的配置;
四、所有的配置项可以通过使用过滤条件来决定配置是否生效;
五、通过内置的表达式可以访问注册表或者调用.NET函数;
首先来说工程文件的组成
1、工程文件的起始部分是 ProjectConfigurations , 里面包含 Configuration(Debug 或者 Release)和 Platform(Win32、x64等),这个部分配置了工程编译目标的个数,默认配置只有两个:Win32 平台下的 Debug 和 Release;
2、其后是一切全局属性的配置,关键是 Keyword 属性,该属性决定了很多后续的默认属性文件的导入行为,例如是否导入 MFC 的默认属性设置;
3、导入 C++默认属性文件 Microsoft.Cpp.Default.props , 该文件在 C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0 目录下( Windows 64 上的默认安装位置,在你的机器上可能会有所不同,可以通过注册表 HKEY_LOCAL_MACHINE\Software\Microsoft\MSBuild\ToolsVersions\4.0 中的属性 VCTargetsPath 找到位置);
4、根据编译的配置不同进行一些本工程的特定配置,例如“ConfigurationType”、“CharacterSet”和"WholeProgramOptimization"
5、导入C++的属性文件 Microsoft.Cpp.props , 和默认属性文件 Microsoft.Cpp.Default.props 在相同的位置
6、根据编译的配置不同导入不同的用户配置属性文件,主要是根据平台的不同进行属性文件的导入,路径为 %LOCALAPPDATA%\Microsoft\MSBuild\v4.0\,这些文件没有配置任何的内容,如果你删除它们,那么在打开 C++ 工程文件时,IDE 会自动的创建它们;请注意关键就在这里,这些文件的目的就是为了用户可以对C++工程的属性进行自定义,并且是“用户”级别的,也就是说在这些文件中增加的属性将对你所有的工程起作用;
7、根据编译的配置不同对“编译”、“连接”、“库”等选项进行工程的本地配置,例如:“警告等级”、“优化选项”和“宏”的定义等;
8、工程中包含的文件,例如 ClCompile 包含的是需要进行 C/C++ 编译的文件,ResourceCompile 包含的是需要 RC 进行编译的资源文件,等等;
9、导入C++的编译目标属性定义文件“Microsoft.Cpp.targets”,和默认属性文件的位置相同;
好了,到此为止,工程文件的9个组成部分已经大致的分析出来了,如果对 Microsoft.Cpp.Default.props 、Microsoft.Cpp.props 和 Microsoft.Cpp.targets 进行简单分析就会发现,它们都是首先根据编译的目标平台导入对应的平台定义的文件,然后是自己的属性定义,并且这些属性都会在没有定义的情况下才会生效,避免冲掉前面的定义;并且还可以发现一个秘密,那就是在编译系统还为每一个工程提供了一个免费的属性定会以文件,*.vcxproj.user ,这个文件如果存在,那么将会被自动导入;
通过以上分析,我们可以发现C++的编译系统其实除了工程文件本身之外(毕竟不可能把所有的编译选项都放到工程文件中)还另外提供来三个级别的属性配置文件:
1、工程级别的,就是 *.vcxproj.user 文件;
2、用户级别的,就是 Microsoft.Cpp.$(Platform).user.props 文件
3、系统级别的,就是 前面提供到 Microsoft.Cpp.Default.props 、Microsoft.Cpp.props 和 Microsoft.Cpp.targets 文件;

实际使用可以通过

VS2010 "生成" -> "配置管理器" -> "活动解决方案配置(C):"

修改增加

考虑AutoCAD版本、ObjectARX版本众多,准备实现一个XML生成工具,根据新建的工程名,生成一个