有没有办法在VC ++中使用预编译头,而不需要stdafx.h? [英] Is there a way to use pre-compiled headers in VC++ without requiring stdafx.h?

查看:248
本文介绍了有没有办法在VC ++中使用预编译头,而不需要stdafx.h?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆遗留代码,我需要编写单元测试。它使用预编译的头部,所以几乎所有的.cpp文件在stdafx.h有一个依赖,这使得很难打破依赖以写测试。



我的第一本能是删除所有这些stdafx.h文件,这些文件大部分包含#include指令,并根据需要将这些#include直接放在源文件中。



这将使得有必要关闭预编译的头,因为它们依赖于具有像stdafx.h的文件来确定预编译的头停止的位置。



是有一种方法来保持预编译头没有stdafx.h依赖?是否有更好的方法来处理这个问题?

解决方案

是的,有更好的方法。



问题,IMHO,与预编译头的向导样式是,他们鼓励不必要的耦合,使重用代码比它应该更困难。此外,编写的只是在stdafx.h风格的一切都很容易是一个痛苦的维护,因为改变任何头文件中的任何东西可能导致整个代码库每次重新编译。这可以使简单的重构永远,因为每个更改和重新编译周期需要远远长于它应该。



更好的方式,再次IMHO,是使用#pragma hdrstop和/ Yc和/ Yu。这使您能够轻松地设置使用预编译头的构建配置,并且还可以构建不使用预编译头的配置。使用预编译头的文件与源文件中的预编译头本身没有直接相关性,这使得它们可以在使用或不使用预编译头的情况下构建。项目文件确定哪个源文件构建预编译头,并且每个源文件中的#pragma hdrstop行确定哪些包含来自预编译头(如果使用),哪些是直接从源文件取得的...这意味着,当进行维护时,您将使用不使用预编译头的配置,并且只使用在头文件更改将重建后需要重建的代码。当完成构建时,可以使用预编译头配置来加快编译过程。使用非预编译头构建选项的另一个好处是,它确保您的cpp文件只包括他们需要的,并包括他们需要的一切(如果你使用预编译头的向导样式,那是很难的。 / p>

我写了一些关于这是如何工作的: http://www.lenholgate.com/blog/2004/07/fi-stlport-precompiled-headers-warning-level-4- and-pragma-hdrstop.html (忽略关于/ FI的东西),我有一些示例项目使用#pragma hdrstop和/ Yc / Yu方法在这里构建: http://www.lenholgate.com/blog/2008/04/practical-testing- 16 --- fixing-a-timeout-bug.html



当然,从向导样式预编译头使用到更受控风格通常是不平凡的...


I've got a bunch of legacy code that I need to write unit tests for. It uses pre-compiled headers everywhere so almost all .cpp files have a dependecy on stdafx.h which is making it difficult to break dependencies in order to write tests.

My first instinct is to remove all these stdafx.h files which, for the most part, contain #include directives and place those #includes directly in the source files as needed.

This would make it necessary to turn off pre-compiled headers since they are dependent on having a file like stdafx.h to determine where the pre-compiled headers stop.

Is there a way to keep pre-compiled headers without the stdafx.h dependencies? Is there a better way to approach this problem?

解决方案

Yes, there is a better way.

The problem, IMHO, with the 'wizard style' of precompiled headers is that they encourage unrequired coupling and make reusing code harder than it should be. Also, code that's been written with the 'just stick everything in stdafx.h' style is prone to be a pain to maintain as changing anything in any header file is likely to cause the whole codebase to recompile every time. This can make simple refactoring take forever as each change and recompile cycle takes far longer than it should.

A better way, again IMHO, is to use #pragma hdrstop and /Yc and /Yu. This enables you to easily set up build configurations that DO use precompiled headers and also build configurations that do not use precompiled headers. The files that use precompiled headers don't have a direct dependency on the precompiled header itself in the source file which enables them to be build with or without the precompiled header. The project file determines what source file builds the precompiled header and the #pragma hdrstop line in each source file determines which includes are taken from the precompiled header (if used) and which are taken directly from the source file... This means that when doing maintenance you would use the configuration that doesn't use precompiled headers and only the code that you need to rebuild after a header file change will rebuild. When doing full builds you can use the precompiled header configurations to speed up the compilation process. Another good thing about having the non-precompiled header build option is that it makes sure that your cpp files only include what they need and include everything that they need (something that is hard if you use the 'wizard style' of precompiled header.

I've written a bit about how this works here: http://www.lenholgate.com/blog/2004/07/fi-stlport-precompiled-headers-warning-level-4-and-pragma-hdrstop.html (ignore the stuff about /FI) and I have some example projects that build with the #pragma hdrstop and /Yc /Yu method here: http://www.lenholgate.com/blog/2008/04/practical-testing-16---fixing-a-timeout-bug.html .

Of course, getting from the 'wizard style' precompiled header usage to a more controlled style is often non-trivial...

这篇关于有没有办法在VC ++中使用预编译头,而不需要stdafx.h?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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