Reputation: 192417
I am converting a makefile project into a Visual Studio VC++ project. It's actually C source code.
One of the statements I have in my makefile is:
echo char * gLibraryBuildSig ="%DATE% %TIME%"; > BuildTimestamp.c
This produces a C source file with a single line in it:
char * gLibraryBuildSig ="Sun 08/23/2009 17:56:05.05";
In the makefile I then compile all the C source with cl.exe, and after linking, delete the BuildTimestamp.c file. This gives me a global symbol that provides the bubild time as a string.
How can I do the same thing in a VS2008 project? Keep in mind it's not MSBuild.
I'm part-way there. To generate a C module at build time in Visual Studio, I just use the pre-build event.
How do I include that generated file into the compile, but also exclude it from source control and project management?
Or, is there a better way to do what I want?
Upvotes: 2
Views: 2699
Reputation: 1008
I might be 10 years late, but I like this simple approach. My solution is
my pre-build step:
echo #define DBJ_BUILD_TIMESTAMP __DATE__ " " __TIME__ > build_time_stamp.inc
That little inc, contains a compile time constant in both C and C++. I usually include it in my main.cpp
#include "build_time_stamp.inc"
Since it is generated on each build, it provokes (re)compilation of main.cpp
Usage might be
printf( "\nBuild time stamp: " DBJ_BUILD_TIMESTAMP );
If you do not what to be bothered by GIT to commit/sync/push, that inc file, after each build, simply do not include it in a project. In any case if you want to use it in some more complex scenario, simply keep it in a global constant:
constexpr auto build_time_stamp = DBJ_BUILD_TIMESTAMP ;
Enjoy ...
Upvotes: 1
Reputation: 135245
Another alternative is to use the preprocessor to include the generated file:
#include "BuildTimeStamp.c"
The file that includes this file can be one of the files in the project under source control.
Upvotes: 0
Reputation: 127447
The compiler (cl.exe) has predefined macros __DATE__
and __TIME__
, as well as __TIMESTAMP__
. You can compile a file containing only these as a pre-link step.
Upvotes: 3