SoFunction
Updated on 2025-03-05

How to use compile time as version number identification by VC

Common methods are divided into two steps:

1. Get the compile time;

2. Set the base time to use twice the total number of days from the base time to the version number. The initial value can be added if appropriate;

There are two ways to implement the first step:

1. Use system macros directly:CString OcxTime = __DATE__;
2. Call the batch file.bat in the Pre-link Step of the compile option:

@echo off
echo #pragma once>""
echo #define APP_VER_NUM"%data%">>""

Additional to other netizens

Use macros __DATE__ and __TIME__

CString strVersion,strBuildTime; 
strBuildTime = __DATE__; 
(" "); 
(__TIME__); 
COleVariant vtime(strBuildTime); 
(VT_DATE); 
COleDateTime dateTime=vtime; 
SYSTEMTIME systime; 
VariantTimeToSystemTime(dateTime, &systime); 
CTime buildTime(systime); 
strVersion = (" Builded %Y-%m-%d %H:%M:%S "); 

It's the supplement of classmate

Generally speaking, the formal software released will contain the svn version number of the program, the software's compilation version and compilation time, etc., but these things do not exist by default and require some steps to process them. There are several specific methods. What is always the same is to use the SVN's own instructions to obtain the version number and current time, fill in the placeholder in the template, and finally overwrite the resource file with the replaced template. Here we will only talk about the simplest one:
a. First, you need to add a resource file to the VC program. Assuming that the program name is Test, the resource file is
b. Add a new resource item - version to the resource file. The resource file is opened in Notepad, and the following code has been added:

VS_VERSION_INFO VERSIONINFO
 FILEVERSION 1,0,0,1
 PRODUCTVERSION 1,0,0,1
 FILEFLAGSMASK 0x17L
#ifdef _DEBUG
 FILEFLAGS 0x1L
#else
 FILEFLAGS 0x0L
#endif
 FILEOS 0x4L
 FILETYPE 0x1L
 FILESUBTYPE 0x0L
BEGIN
  BLOCK "StringFileInfo"
  BEGIN
    BLOCK "080404b0"
    BEGIN
      VALUE "FileDescription", "Test"
      VALUE "FileVersion", "1, 1, 1, 0"
      VALUE "InternalName", "Test"
      VALUE "LegalCopyright", "Copyright (C) 2015"
      VALUE "OriginalFilename", ""
      VALUE "ProductName", "Microsoft"
      VALUE "ProductVersion", "V1.0"
    END
  END
  BLOCK "VarFileInfo"
  BEGIN
    VALUE "Translation", 0x804, 1200
  END
END

c. Next create a template, use the placeholder WCREV provided by SVN to obtain the version number, and WCNOW to obtain the compile time. The template can be copied directly and renamed to Test.rc2. Just modify the ProductVersion value in the above code:

VALUE "ProductVersion", "V2.1.1.$WCREV$.(Build$WCNOW$)"

d. So when will placeholders be replaced, and then the program's resource file be replaced, and the version number and compilation time are accurately compiled into the program? VS provides a Build Events configuration. We choose Pre-Build Event and add a line of code as follows:

SubWCRev $(ProjectDir) $(ProjectDir)\Test.rc2 $(ProjectDir)\

The code means that before the program is compiled, use SVN script tool SubWCRev to obtain the SVN number and the current compilation time to the project directory, replace the placeholder in the rc2 file, and overwrite it into the rc file.