VS开发用命令行自动编译工程

时间:2022-09-29 17:42:33

最近在研究工程的自动化配置和编译部署,找到一篇描述VC命令行编译的帖子,经试验在vs2008 下也是OK的,这样结合SVN可以用脚本做一系列自动化操作,特转录如下:
PS:补充,试用了一段时间,配置好了工程,自动化build是没问题,现在有个问题是即使工程中有文件被更新了,这个命令貌似无法检测到文件过期,还是维持上次build后的内容,如果每次都用rebuild的话就太慢了,折腾了半天,还是没有找到解决的办法,有知道解决办法的高手麻烦留个言,不胜感激~~

原文:http://blog.chinaunix.net/u2/89624/showart_1903053.html

 

msdev是visual studio 6.0的IDE程序, devenv是visual studio 2005的IDE程序
两种IDE程序, 既可以工作在图形模式下,也可以工作在命令模式下.
其中, 直接运行msdev 和 devenv将打开vs6和vs2005的IDE界面. 命令行则支持不同的选项.

Msdev的命令行用法:
Usage:
  MSDEV [myprj.dsp|mywksp.dsw]  - load project/workspace
        [<filename>]            - load source file
        /?                      - display usage information
        /EX <macroname>         - execute a VBScript macro
        /OUT <filename>         - redirect command line output to a file
        /USEENV                 - ignore tools.options.directories settings
        /MAKE [<target>] [...]  - build specified target(s)
              [<project> - <platform> <configname>]
              [[<project>|ALL] - [DEBUG|RELEASE|ALL]]
              /CLEAN            - delete intermediate files but don't build
              /REBUILD          - clean and build
              /NORECURSE        - don't build dependent projects
比如我对BerkeleyDB而言, 4.7.25自带了一个Berkeley_DB.dsw, 此工程文件由包含很多的子项目.
编译所有项目Debug版本为: 
  msdev Berkeley_DB.dsw /make "all - Win32 Debug x86"
其中有一个project有许多其他projects组成,就是build_all, 包含了C/C++的动态库,各种工具程序,还有各种示例程序. 编译此project Debug版本就为:
  msdev Berkeley_DB.dsw /make "build_all - Win32 Debug x86"
如果还想编译Java的动态库, 在vc6中设置好java后, 运行如下命令即可:
  msdev Berkeley_DB.dsw /make "db_java - Win32 Debug x86"
在工程文件外,还有一个用来衡量性能的程序, test_micro,编译该程序的命令为:
 msdev test_micro.dsp /make "test_micro - Win32 Debug x86"
即,对于项目文件而言, 其只有一个project, 就是其本身.

对于自动化程序而言, msdev有一个弊端,就是程序返回值.  当项目文件/工程文件不存在的时候, msdev的返回值居然是0!, 和编译成功的一样, 也许msdev认为不存在就是成功吧.


devenv命令的格式就长了一点, 基本语法是:
devenv  [solutionfile | projectfile | anyfile.ext]  [switches]
比如, 为了方便VC2005以上的编译, BerkeleyDB将会同时提供vc6所用的dsw文件同时, 也会推出一个sln文件,这sln文件就是solutionfile
使用此文件编译BerekleyDB默认配置的Debug版本为:
 devenv Berkeley_DB.sln /build "Debug|Win32"
只编译db的动态库则为:
 devenv Berkeley_DB.sln /build "Debug|Win32" /project db
编译test_micro则复杂点,因为vc2005默认的project file是.vcproj的,而不是.dsp格式的,而test_micro只有.dsp格式的故而,需要先转化为.vcproj格式的, 可以用VCBuild工具:
VCBuild test_micro.dsp /upgrade
这样就产生了test_micro.vcproj
用devenv编译就是:
 devenv test_micro.vcproj /build "Debug x86|Win32"
  之所有配置与上面不同在与, test_micro中是适用于VC6的配置, 转化过来以后, 配置是保留的.

与msdev不同, devenv将文件不存在视为编译失败. 更加适合程序处理.


附:devenv的命令行帮助
Microsoft (R) Visual Studio Version 8.0.50727.762.
Copyright (C) Microsoft Corp 1984-2005. All rights reserved.

Use:
devenv  [solutionfile | projectfile | anyfile.ext]  [switches]

The first argument for devenv is usually a solution file or project file.
You can also use any other file as the first argument if you want to have the
file open automatically in an editor. When you enter a project file, the IDE
looks for an .sln file with the same base name as the project file in the
parent directory for the project file. If no such .sln file exists, then the
IDE looks for a single .sln file that references the project. If no such single
.sln file exists, then the IDE creates an unsaved solution with a default .sln
file name that has the same base name as the project file.

Command line builds:
devenv solutionfile.sln /build solutionconfig [ /project projectnameorfile [ /projectconfig name ] ]
Available command line switches:

/Build        Builds the solution or project with the specified solution
        configuration. For example "Debug". If multiple platforms
        are possible, the configuration name must be enclosed in quotes
        and contain platform name. For example: "Debug|Win32".
/Clean        Deletes build outputs.
/Command    Starts the IDE and executes the command.
/Deploy        Builds and then deploys the specified build configuration.
/Edit        Opens the specified files in a running instance of this
        application. If there are no running instances, it will
        start a new instance with a simplified window layout.
/LCID        Sets the default language in the IDE for the UI.
/Log        Logs IDE activity to the specified file for troubleshooting.
/NoVSIP        Disables the VSIP developer's license key for VSIP testing.
/Out        Appends the build log to a specified file.
/Project    Specifies the project to build, clean, or deploy.
        Must be used with /Build, /Rebuild, /Clean, or /Deploy.
/ProjectConfig    Overrides the project configuration specified in the solution
        configuration. For example "Debug". If multiple platforms are
        possible, the configuration name must be enclosed in quotes
        and contain platform name. For example: "Debug|Win32".
        Must be used with /Project.
/Rebuild    Cleans and then builds the solution or project with the
        specified configuration.
/ResetAddin    Removes commands and command UI associated with the specified Add-in.
/ResetSettings    Restores the IDE's default settings, optionally resets to
        the specified VSSettings file.
/ResetSkipPkgs    Clears all SkipLoading tags added to VSPackages.
/Run        Compiles and runs the specified solution.
/RunExit    Compiles and runs the specified solution then closes the IDE.
/SafeMode    Launches the IDE in safe mode loading minimal windows.
/Upgrade    Upgrades the project or the solution and all projects in it.
        A backup of these files will be created as appropriate.  Please
        see Help on 'Visual Studio Conversion Wizard' for more
        information on the backup process.

Product-specific switches:

/debugexe    Open the specified executable to be debugged. The
        remainder of the command line is passed to this
        executable as its arguments.
/useenv        Use PATH, INCLUDE, LIBPATH, and LIB environment variables
        instead of IDE paths for VC++ builds.

To attach the debugger from the command line, use:
    VsJITDebugger.exe -p <pid>

 

附:

vs2005 devenv.exe命令行帮助中文:

用法: 

devenv  [solutionfile | projectfile | anyfile.ext]  [switches]

 

devenv 的第一个参数通常是一个解决方案文件或项目文件。

如果您希望在编辑器中自动打开文件,

也可以使用任何其他文件作为第一个参数。当您输入项目文件时,IDE

会在项目文件的父目录中查找与该项目文件具有相同

基名称的 .sln 文件。如果不存在这样的 .sln 文件,

IDE 将查找引用该项目的单个 .sln 文件。如果不存在这样的单个

.sln 文件,则 IDE 将创建一个具有默认 .sln 文件名且未保存

的解决方案,其基名称与项目文件的基名称相同。

 

命令行编译:

devenv solutionfile.sln /build solutionconfig [ /project projectnameorfile [ /projectconfig name ] ]

可用的命令行开关:

 

/Build使用指定的解决方案配置生成解决方案或

项目。例如“Debug”。如果可能存在多个平台,

则配置名称必须用引号括起来

并且包含平台名称。例如“Debug|Win32”。

/Clean删除生成结果。

/Command启动 IDE 并执行该命令。

/Deploy生成并部署指定的生成配置。

/Edit在此应用程序的运行实例中打开

指定文件。如果没有正在运行的实例,

则启动一个具有简化窗口布局的新实例。 

/LCID设置 IDE 中用于用户界面的默认语言。

/Log将 IDE 活动记录到指定的文件以用于故障排除。

/NoVSIP禁用用于 VSIP 测试的 VSIP 开发人员许可证密钥。

/Out将生成日志追加到指定的文件中。

/Project指定生成、清理或部署的项目。

必须和 /Build、/Rebuild、/Clean 或 /Deploy 一起使用。

/ProjectConfig重写解决方案

配置中指定的项目配置。例如“Debug”。如果可能存在

多个平台,则配置名称必须用引号括起来

并包含平台名称。例如“Debug|Win32”。

必须和 /Project 一起使用。

/Rebuild先清理,然后使用指定配置生成

解决方案或项目。

/ResetAddin移除与特定外接程序关联的命令和命令用户界面。

/ResetSettings恢复 IDE 的默认设置,还可以重置为

指定的 VSSettings 文件。

/ResetSkipPkgs清除所有添加到 VSPackages 的 SkipLoading 标记。

/Run编译并运行指定的解决方案。

/RunExit编译并运行指定的解决方案然后关闭 IDE。

/SafeMode以安全模式启动 IDE,加载最少数量的窗口。

/Upgrade升级项目或解决方案以及其中的所有项目。

并相应地创建这些文件的备份。有关备份

过程的更多信息,请参见

“Visual Studio 转换向导”上的帮助。

 

产品特定的开关: 

 

/debugexe       打开要调试的指定可执行文件。

                命令行的其余部分作为它的参数

                传递到此执行文件。

/useenv使用 PATH、INCLUDE、LIBPATH 和 LIB 环境变量

而不是使用 VC++ 生成的 IDE 路径。

 

要从命令行附加调试器,请使用: 

VsJITDebugger.exe -p <pid>