Is there a way to automatically have #define played in every source file

I want 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 any way to do this without manually copying? Compiler option?

+2
c ++ macros c-preprocessor memory-leaks
source share
6 answers

The /D command line option can be used to specify preprocessor characters. However, I don't know if it can be used to define macros with arguments, but this should be easy to verify.

Edit: Otherwise, the /FI ("force include") option should allow you to do what you want. Quoting MSDN documentation :

This option has the same effect as specifying a file with double quotes in the #include directive on the first line of each source file [...].

You can then put your #define in this forced include file.

+5
source share

I would suggest using this #define . Redefining new not portable, and if you do it this way, you won’t be able to use the new placement from work afterwards. If you “force” this #define before the #include file manually takes effect, you risk incompatibility between the library header files and their source files, and you will get “unexpected” errors in library files that use the new location (often a template / container).

If you are going to override new , make it explicit and leave it in the source.

+4
source share

You can paste this #define in stdafx.h or common.h or any other header file that will be included in each source file.

+2
source share

Compiler option?

Yes, you can configure the define list in the project properties (either in the "Preprocessor" or "Advanced" section, as far as I remember). These define will be present in every source file.

+1
source share

You can put #define in the h file, but without putting the #ifndef guard in the h file. Then the #include file in each of your source files.

I do not approve of the redefinition of new , BTW.

+1
source share

You can simply define your own global new operator somewhere in your code and compile it conditionally. Do not forget to include all 4 options for the new ones (simple and an array, one with and without it) and two options for removal (simple and massive). There is a whole chapter on this subject in my copy of Effective C ++, Third Edition (chapter 8)

 #ifdef MYDEBUG void* operator new(std::size_t size) { <your code here> } void operator delete(void* p) { <your code here> } #endif 
0
source share

All Articles