Visual C ++ 2005でビルドした実行可能ファイルにコンパイルタイムスタンプ情報を挿入するにはどうすればよいですか?プログラムを実行すると、次のような出力ができるようになります。
このビルドXXXXは、dd-mm-yy、hh:mmでコンパイルされました。
ここで、日付と時刻はプロジェクトが構築された時刻を反映しています。再コンパイルされない限り、プログラムが連続して呼び出されるたびに変更されないようにする必要があります。
Visual C ++ 2005でビルドした実行可能ファイルにコンパイルタイムスタンプ情報を挿入するにはどうすればよいですか?プログラムを実行すると、次のような出力ができるようになります。
このビルドXXXXは、dd-mm-yy、hh:mmでコンパイルされました。
ここで、日付と時刻はプロジェクトが構築された時刻を反映しています。再コンパイルされない限り、プログラムが連続して呼び出されるたびに変更されないようにする必要があります。
正確な形式ではありませんが、DATEは Mmm dd yyyy の形式になり、 TIMEは hh:mm:ss の形式になります。このような文字列を作成し、意味のある印刷ルーチンで使用できます。
const char *buildString = "This build XXXX was compiled at " __DATE__ ", " __TIME__ ".";
(別の回答に注意してください: TIMESTAMPは、ビルドの日付/時刻ではなく、ソース ファイルの変更の日付/時刻のみを吐き出します。)
__DATE__
__TIME__
C99 の標準の一部として事前定義されているため、利用できるはずです。それらはプリプロセッサで一度実行されます。
ええと... Visual C++ には、 というビルトイン シンボルがあり__ImageBase
ます。具体的には:
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
実行時にそれを調べて、PE ヘッダーのタイムスタンプを判断できます。
const IMAGE_NT_HEADERS *nt_header= (const IMAGE_NT_HEADERS *)((char *)&__ImageBase + __ImageBase.e_lfanew);
nt_header->FileHeader.TimeDateStamp
1970 年 1 月 1 日からの秒数であるタイムスタンプを取得するために使用します。
__TIME__
and __DATE__
can work, however there are some complications.
If you put these definitions in a .h file, and include the definitions from multiple .c/.cpp files, each file will have a different version of the date/time based on when it gets compiled. So if you're looking to use the date/time in two different places and they should always match, you're in trouble. If you're doing an incremental build, one of the files may be rebuilt while the other is not, which again results in time stamps that could be wildly different.
A slightly better approach is to make GetBuildTimeStamp() prototypes in a .h file, and put the __TIME__
and __DATE__
macros in the implementation(.c/.cpp) file. This way you can use the time stamps in multiple places in your code and they will always match. However you need to ensure that the .c/.cpp file is rebuilt every time a build is performed. If you're doing clean builds then this solution may work for you.
If you're doing incremental builds, then you need to ensure the build stamp is updated on every build. In Visual C++ you can do this with PreBuild steps - however in this case I would recommend that instead of using __DATE__
and __TIME__
in a compiled .c/.cpp file, you use a text-based file that is read at run-time during your program's execution. This makes it fast for your build script to update the timestamp (no compiling or linking required) and doesn't require your PreBuild step to understand your compiler flags or options.
I think, the suggested solutions to use DATE, TIME or TIMESTAMP would be good enough. I do recommend to get a hold of a touch program to include in a pre-build step in order to touch the file that holds the use of the preprocessor variable. Touching a file makes sure, that its timestamp is newer than at the time it was last compiled. That way, the date/time in the compiled file is changed as well with each rebuild.
Visual C++__TIMESTAMP__
は、ほぼ正確に必要なものをサポートしています。そうは言っても、ビルド タイムスタンプの難しい部分はそれらを最新の状態に保つことです。つまり、__TIMESTAMP__
すべての再ビルドで使用されるファイルをコンパイルすることです。ただし、Visual C++ でこれを設定する方法があるかどうかはわかりません。