在 VisualStudio 中使用 Makefile 的好技巧?
我知道构建项目的理想方法是不需要基于 IDE 的项目文件,因为理论上它会导致自动化等方面的各种麻烦。 但我还没有开发过一个在 Windows 上编译、不依赖于 VisualStudio 项目的项目(好吧,显然一些开源的东西是用 Cygwin 完成的,但我在这里只是笼统地说)。
另一方面,如果我们只使用 VS 运行 makefile,我们就失去了编译选项窗口的所有好处,并且维护外部 makefile 变得很痛苦。
那么使用 VS 的人实际上是如何处理外部 makefile 的呢? 我还没有找到一个无痛的系统来做到这一点......
或者实际上大多数人都不会这样做,尽管它被宣传为良好的做法?
I know the ideal way to build projects is without requiring IDE based project files, since it theoretically causes all sort of trouble with automation and what not. But I've yet to work on a project that compiles on Windows that doesn't depend on the VisualStudio project (Ok, obviously some Open Source stuff gets done with Cygwin, but I'm being general here).
On the other hand if we just use VS to run a makefile, we loose all the benefits of the compile options window, and it becomes a pain to maintain the external makefile.
So how do people that use VS actually handle external makefiles? I have yet to find a painless system to do this...
Or in reality most people don't do this, although its preached as good practice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
看看 MSBuild!
(我想添加一个示例,但这个编辑器完全搞乱了 XML...抱歉)
Take a look at MSBuild!
(I wanted to add a sample but this edior totally messed up the XML... sorry)
理想情况下也许是这样,但实际上并非如此。
作为构建大师,Makefile 是我的首选,但是,开发人员将所有时间都花在 Visual Studio IDE 中,当他们进行更改时,更改的是 vcproj 文件,而不是 makefile。 因此,如果我使用 makefile 进行全局构建,很容易与 8 或 10 个其他人正在运行的项目/解决方案文件不同步。
我能够与整个团队保持同步的唯一方法是直接在构建过程脚本中对解决方案文件运行 devenv.exe。
我的构建中的 makefile 很少,它们位于预构建或自定义构建部分或单独的实用程序项目中。
Ideally perhaps, in practice no.
Makefiles would be my preference as the build master, however, the developers spend all their time inside the visual studio IDE and when they make a change, it's to the vcproj file, not the makefile. So if I'm doing the global builds with makefiles, it's too easily put out of synch with the project/solution files in play by 8 or 10 others.
The only way I can stay in step with the whole team is to run devenv.exe on the solution files directly in my build process scripts.
There are very few makefiles in my builds, where there are they are in the pre-build or custom build sections or a separate utility project.
一种可能性是使用 CMake - 您用脚本描述您的项目是如何进行的待构建,CMake 会为您生成 Visual Studio 解决方案/项目文件。
如果您需要从命令行或在持续集成工具中构建项目,则可以使用 CMake 为 NMake 生成 Makefile。
如果您的项目是一个跨平台项目 - 您可以运行 CMake 来为您选择的工具链生成 makefile。
一个简单的 CMake 脚本如下所示:
将这两行与 makefile 或在您最喜欢的 IDE 中设置简单项目的方式进行比较。
简而言之,CMake 不仅跨平台支持您的项目,还使其跨 IDE。 如果您只想使用 eclipse 或 KDevelop 或 codeblocks 测试您的项目,只需运行 CMake 即可生成相应的项目文件。
嗯,在实践中并不总是那么容易,但 CMake 的想法确实很震撼。
例如,如果您考虑将 CMake 与 Visual Studio 一起使用,则需要进行一些调整才能获得熟悉的 VS 项目感觉,主要障碍是组织头文件和源文件,但这是可能的 - 检查 CMake wiki(并通过编写一个简短的脚本,您甚至可以简化此任务)。
One possibility is to use CMake - you describe with a script how you project is to be built, and CMake generates the Visual Studio solution/project files for you.
And if you need to build your project from the command line, or in a continuous integration tool, you use CMake to generate a Makefile for NMake.
And if you project is a cross-platform one - you can run CMake to generate the makefiles for the toolchain of your choice.
A simple CMake script looks like this:
Compare these two lines with a makefile or the way you setup a simple project in your favorite IDE.
In a nutshell CMake does not only cross-platform-enables your project it also makes it cross-IDE. If you like to just test your project with eclipse or KDevelop, or codeblocks, just run CMake to generate the corresponding project files.
Well, in practice it is no always so easy, but the CMake idea just rocks.
For example, if you consider using CMake with Visual Studio there is some tweaking required to obtain the familiar VS project feeling, main obstacle is to organize your header and source files, but it is possible - check the CMake wiki (and by writting a short script you might even simplify this task).
我们使用 NAnt 脚本,该脚本在编译步骤中调用 MSBuild。 使用 NAnt 使我们能够执行构建前和构建后的任务,例如设置版本号以匹配源代码控制修订号、整理代码覆盖率信息、组装和压缩部署源。 但实际上,其核心仍然是 MSBuild 进行编译。
您可以将 NAnt 构建作为自定义工具集成到 IDE 中,以便它既可以在构建服务器或持续集成服务器上使用,也可以由开发人员以相同的方式使用。
We use a NAnt script, which at the compile step calls MSBuild. Using NAnt allows us to perform both pre- and post-build tasks, such as setting version numbers to match source control revision numbers, collating code coverage information, assembling and zipping deployment sources. But still, at the heart of it, it's MSBuild that's actually doing the compiling.
You can integrate a NAnt build as a custom tool into the IDE, so that it can be used both on a build or continuous integration server and by the developers in the same way.
就我个人而言,我使用 Rake 来调用 msbuild 在我的解决方案或项目上。 对于常规开发,我使用 IDE 及其提供的所有优势。
设置 Rake 以便我可以编译、编译和运行测试,或者编译运行测试并创建可部署的工件。
一旦有了构建脚本,就可以很容易地开始做一些事情,比如设置持续集成并使用它来自动化部署。
如果您按照以下步骤进行设置。
Personally, I use Rake to call msbuild on my solution or project. For regular development I use the IDE and all the benefits that provides.
Rake is set up so that I can just compile, compile and run tests or compile run tests and create deployable artifacts.
Once you have a build script it is really easy to start doing things like setting up continuous integration and using it to automate deployment.
You can also use most build tools from within the IDE if you follow these steps to set it up.
我们使用 devenv.exe(与启动 IDE 相同的 exe)从构建脚本(或命令行)构建我们的项目。 指定 /Build 选项时,不会显示 IDE,并且所有内容都会写回控制台(如果指定 /Out 选项,则写回日志文件)
请参阅 http://msdn.microsoft.com/en-us/library/xee0c8y7(VS.80).aspx 了解更多信息信息
示例:
devenv.exe [解决方案文件名称] /Build [项目名称] /Rebuild "Release|Win32" /Out Solution.log
其中“Release|Win32”是解决方案和 Solution.log 中定义的配置是获取编译器输出的文件(当您需要找出编译中出了什么问题时,这非常方便)
We use the devenv.exe (same exe that launches the IDE) to build our projects from the build scripts (or the command line). When specifying the /Build option the IDE is not displayed and everything is written back to the console (or the log file if you specify the /Out option)
See http://msdn.microsoft.com/en-us/library/xee0c8y7(VS.80).aspx for more information
Example:
devenv.exe [solution-file-name] /Build [project-name] /Rebuild "Release|Win32" /Out solution.log
where "Release|Win32" is the configuration as defined in the solution and solution.log is the file that gets the compiler output (which is quite handy when you need to figure out what went wrong in the compile)
我们有一个程序可以解析 vcproj 文件并从中生成 makefile 片段。 (其中包括文件列表和#defines,并且对自定义构建步骤有一些有限的支持。)然后,这些片段包含在主 makefile 中,该主 makefile 执行通常的 GNU make 操作。
(这都是我们的目标系统之一;它的工具没有对 Visual Studio 的本机支持。)
这不需要大量的工作。 一天的时间来设置它,然后可能总共需要一两天的时间来解决一些不立即明显的问题。 它工作得相当好:编译器设置由主 makefile 控制(不再摆弄那些小文本框),但任何人都可以以通常的方式添加新文件并定义到构建中。
也就是说,Visual Studio 处理构建配置所固有的组合问题仍然存在。
We have a program that parses the vcproj files and generates makefile fragments from that. (These include the list of files and the #defines, and there is some limited support for custom build steps.) These fragments are then included by a master makefile which does the usual GNU make stuff.
(This is all for one of the systems we target; its tools have no native support for Visual Studio.)
This didn't require a huge amount of work. A day to set it up, then maybe a day or two in total to beat out some problems that weren't obvious immediately. And it works fairly well: the compiler settings are controlled by the master makefile (no more fiddling with those tiny text boxes), and yet anybody can add new files and defines to the build in the usual way.
That said, the combinatorical problems inherent to Visual Studio's treatment of build configurations remain.
为什么您想要“在不依赖于 VisualStudio 项目的 Windows 上编译”的项目? 您已经有了一个解决方案文件 - 您可以将其与控制台构建一起使用。
如果您的构建系统不像我们的那么复杂,我建议您将 msbuild 与 makefile、nant 甚至简单的批处理文件结合使用...
我缺少什么吗?
Why would you want to have project that "compiles on Windows that doesn't depend on the VisualStudio project"? You already have a solution file - you can just use it with console build.
I'd advise you to use msbuild with conjunction with makefile, nant or even simple batch file if your build system is not as convoluted as ours...
Is there something I'm missing?
这段代码怎么样?
您可以在这里找到其余部分:http ://code.google.com/p/projectpilot/source/browse/trunk/Flubu/Builds/BuildRunner.cs
How about this code?
You can find the rest of it here: http://code.google.com/p/projectpilot/source/browse/trunk/Flubu/Builds/BuildRunner.cs
我自己还没有尝试过,但是 Microsoft 有一个名为 NMake 的 Make 实现,它似乎具有 Visual Studio 集成:
I haven't tried it myself yet, but Microsoft has a Make implementation called NMake which seems to have a Visual Studio integration:
自 VS2005 起,Visual Studio 使用“msbuild”来定义和运行构建。 当您在 Visual Studio 设计器中摆弄项目设置时 - 假设您打开或关闭 XML 文档生成,或者添加新的依赖项,或者添加新项目或程序集引用 - Visual Studio 将更新 .csproj (或 . vbproj 等)文件,这是一个 msbuild 文件。
与之前的 Java ant 或 Nant 一样,msbuild 使用 XML 模式来描述项目和构建。 当您执行“F6”构建时,它会从 VS 运行,您也可以从命令行运行它,而无需打开 VS 或运行 devenv.exe。
因此,使用 VS 工具进行开发,使用命令行 msbuild 进行自动构建 - 相同的构建和相同的项目结构。
Visual Studio since VS2005, uses "msbuild" to define and run builds. When you fiddle with project settings in the Visual Studio designer - let's say you turn XML doc generation on or off, or you add a new dependency, or you add a new project or Assembly reference - Visual Studio will update the .csproj (or .vbproj, etc) file, which is an msbuild file.
Like Java's ant or Nant before it, msbuild uses an XML schema to describe the project and build. It is run from VS when you do a "F6" build, and you can also run it from the command line, without ever opening VS or running devenv.exe.
So, use the VS tool for development and command-line msbuild for automated builds - same build, and same project structure.