Custom Preprocessor as Custom Build Step

I was reading an article in GameDeveloper recently that suggested an

interesting method of using strings as object IDs. Since strings are

expensive to compare at runtime, hashes of those strings are often used

for comparison and identification. The article suggested using a

custom "macro" called HASH() which would be evaluated to a CRC hash

value during the build. For example:

//in the source code

Load(HASH("MDL_MECH01_LARGE"));


//the same line after the custom build step:

Load(0x334BDCF8);

The benefit of this method is that the programmer doesn't need to worry

about the hashes of the strings, and there are no hashes done at

runtime. How are situations like this normally handled with custom

build steps? Do I have to add two copies of all of the source files to

my projects (the input and the output)? Or is it possible for me to

work with the VS preprocessor outputs before they are compiled?

Thanks for any help,

Thaedrys

PS: I'm working in VS 2005 and not using the .NET libraries.

[1610 byte] By [Thaedrys] at [2007-12-18]
# 1

to do this with a custom build step (or custom build rule, which is basically a custom build step that is designed to automatically run on files of a given extension), you would need to have have both input and output files as members of the project. this is a bit unpleasant when you are actually processing the C/C++ source files, because it requires the initial files to have a different extension than .c or .cpp, which means that the editor etc. don't particularly like your source files :(

another way to accomplish this might be to have a pre-build event that runs your pre-processor on all your source files. since the pre-build event will only run when something in the project is out-of-date, this should work, but you'd want to be careful to only pre-process the files that are out-of-date, or else you'll end up having to re-compile everything...

hope that gives you some ideas; let us know if you have any further questions,

josh

VC++ project system developer

joshep at 2007-9-8 > top of Msdn Tech,Visual C++,Visual C++ General...
# 2

You might try using a template meta program instead. I think the following article on CodeProject even provides a CRC implementation.

http://www.codeproject.com/cpp/crc_meta.asp

- Jeremy Boschen

JeremyBoschen at 2007-9-8 > top of Msdn Tech,Visual C++,Visual C++ General...
# 3
Josh,

Thanks for clearing that up for me. I really would rather not have to keep track of both inputs and outputs. And with pre-processing of my source files prior to the build..wouldn't that overwrite the original source? (unless I had an input and output version in the project that is). It's a shame the preprocessor and compiler aren't separate programs so I could just sandwich my preprocessor between them.

Thanks again for your help,
Thaedrys

Thaedrys at 2007-9-8 > top of Msdn Tech,Visual C++,Visual C++ General...
# 4
Jeremy,

Thanks for the link, I think I might try using template metaprogramming. It looks like that might be my best bet for portability and ease of use (granted I don't get any of those vague compiler errors ;)).

-Thaedrys

Thaedrys at 2007-9-8 > top of Msdn Tech,Visual C++,Visual C++ General...