有没有办法在每个源文件中自动复制#define
我希望以下内容出现在我的 Visual C++ 2005 解决方案中的每个源文件中:
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
有没有办法在不手动复制的情况下执行此操作?编译器选项?
I'd like the following to appear in every source file in my Visual C++ 2005 solution:
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
Is there a way of doing this without manually copying it in? Compiler option?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
命令行选项
/D
可用于定义预处理器符号。不过,我不知道它是否也可以用于定义带参数的宏,但测试它应该很容易。编辑:如果失败,
/FI option
(“强制包含”)应该允许您执行您想要的操作。引用 MSDN 文档:
然后,您可以将
#define
放入该强制包含文件中。The command line option
/D
can be used to define preprocessor symbols. I don't know, though, whether it can also be used to define macros with arguments, but it should be an easy matter to test that.Edit: Failing that, the
/FI
option ("force include") should allow you to do what you want. Quoting the MSDN documentation:You can then put your
#define
s in that forced include file.我建议不要使用这个
#define
。重新定义new
是不可移植的,如果您以这种方式执行此操作,则会阻止随后使用展示位置new
的任何操作。如果您在文件手动#include
生效之前“强制”此#define
,那么您将面临库头文件及其源文件之间不兼容的风险,并且您会感到“惊讶”使用放置new
的库文件中的错误(通常是模板/容器类)。如果您要重新定义
new
,则将其明确化并将其保留在源代码中。I'd advise against using this
#define
. Re-definingnew
is not portable and if you do it in this way then you prevent anything subsequently using a placementnew
from working. If you 'force' this#define
before a file's manually#include
s take effect then you risk incompatibilities between library header files and their source files and you will get 'surprise' errors in library files that use placementnew
(frequently template/container classes).If you are going to redefine
new
, then make it explicit and leave it in the source.您可以将该
#define
插入到 stdafx.h 或 common.h 或包含在每个源文件中的任何其他头文件中。You could insert that
#define
into stdafx.h or common.h or any other header file that gets included into each source file.是的,您可以在项目属性中自定义定义列表(据我所知,在“预处理器”或“高级”下)。这些
define
将出现在每个源文件中。Yes, you can customize a list of
define
s in the project properties (either under “Preprocessor” or “Advanced,” as far as I remember). Thesedefine
s will be present in each source file.您可以将
#define
放入h
文件中,但无需将#ifndef
保护放入h
中文件。然后在每个源文件中#include
该文件。顺便说一句,我不赞成重新定义
new
。You could put the
#define
s into anh
file, but without putting the#ifndef
guard in theh
file. Then#include
the file in each of your source files.I am not endorsing redefining
new
, BTW.您可以在代码中的某个位置定义自己的全局 new 运算符并有条件地编译它。不要忘记包含 new 的所有 4 种变体(普通和数组一,带或不带 no throw)和删除的两种变体(普通和数组一)。我的《Effective C++,第三版》(第 8 章)中有整整一章讨论这个问题
You can just define your own global new operator somewhere in your code and compile it conditionally. Do not forget to include all 4 variations of new( plain and array one with and without nothrow) and two variations of delete(plain and array one). There is a whole chapter on the matter in my copy of Effective C++, Third Edition (Chapter 8)