Use # define macros to facilitate Version Control

Source: Internet
Author: User
Reprinted from:

Http://www.yonsm.net/read.php? 141

The version number may be used in many places in a VC project, such as the about dialog box and version resource. Changing these values one by one for each version is not only troublesome, but also contrary to the unique principle.

Clever Use of macro definition can solve this problem well.

Let's take a look at several pre-processing operators (Preprocessor Operators

):

Operator Action
Stringizing operator (#)
Causes the corresponding actual argument to be enclosed in double quotation marks
Charizing operator (#@)
Causes the corresponding argument to be enclosed in single quotation marks and to be treated as a character (Microsoft specific)
Token-pasting operator (##)
Allows tokens used as actual arguments to be concatenated to form other tokens

Simply put, # Is to enclose the parameter with quotation marks to form a string; # @ enclose the parameter with single quotation marks to form a character; # Is a bonding character. These operators are obscure in terms of language. Let's take a closer look at the msdn example and have a good understanding.

The following is an example of how to define a Unified Version macro.

Assume that the project contains the resource script file resource. RC, and the unique version number is stored in define. h. Other CPP files that require version numbers contain this file.

Let's take a look at define. h:

#define VER_Major    2
#define VER_Minor 2
#define VER_Release 137
#define VER_Build 1762
#define VER_Version MAKELONG(MAKEWORD(VER_Major, VER_Minor), VER_Release)

#define _Stringizing(v) #v
#define _VerJoin(a, b, c, d) _Stringizing(a.b.c.d)

#define STR_BuildDate TEXT(__DATE__)
#define STR_BuildTime TEXT(__TIME__)
#define STR_BuilDateTime TEXT(__DATE__) TEXT(" ") TEXT(__TIME__)
#define STR_Version TEXT(_VerJoin(VER_Major, VER_Minor, VER_Release, VER_Build))

 

If the preceding definition is available, ver_major, ver_minor, ver_release, and ver_build can be conveniently used.
As the version number; str_version as the version string (in the above example, after the str_version macro is expanded
Text ("2.2.137.1763") tchar string ). These macro definitions can be directly used in any CPP file.

In resource script resource. RC, if we need version resources, we can first define some Macros in define. H, for example:

# Define str_appname text ("cutefoto ")
# Define str_author text ("yonsm ")
# Define str_corporation text ("yonsm. Net ")
# Define str_web text ("yonsm.reg365.com ")
# Define str_email text ("Yonsm@163.com ")
# Define str_weburl text ("http: //") str_web
# Define str_emailurl text ("mailto:") str_email text ("? Subject = ") str_appname


# Ifdef _ CHS
# Define str_description text ("smart batch scan photo cutting tool. ")
# Define str_copyright text ("Copyright (c) 2002-2004") str_corporation text (", retain all rights. ")
# Else // _ CHS
# Define str_description text ("picture Cutting Tool for batch-scanned photo .")
# Define str_copyright text ("Copyright (c) 2002-2004") str_corporation text (". All Rights Reserved .")
# Endif // _ CHS

 

 

At this time, we add version resources in resouurc. RC -- Note: Do not add them directly; otherwise, Vs will be changed to the actual value, so that we cannot achieve our goal. A good solution is:

  1. In vs IDE, double-click resource. RC To Go To The resource view, right-click resource. RC, and select "resource inclusion". The "Read-Only Symbolic Instruction" contains define. h:

    #define APSTUDIO_HIDDEN_SYMBOLS
    //……
    #include "Define.h"
    #include "Windows.h"
    //……
    #undef APSTUDIO_HIDDEN_SYMBOLS
  2. In the "compile-time instruction" field, enter:
    LANGUAGE LANG_NEUTRAL, SUBLANG_DEFAULT
    #pragma code_page(DEFAULT)

    VS_VERSION_INFO VERSIONINFO
    FILEVERSION VER_Major, VER_Minor, VER_Release, VER_Build
    PRODUCTVERSION VER_Major, VER_Minor, VER_Release, VER_Build
    FILEFLAGSMASK VS_FFI_FILEFLAGSMASK
    FILEFLAGS 0
    FILEOS VOS__WINDOWS32
    FILETYPE 0
    FILESUBTYPE VFT2_UNKNOWN
    BEGIN
    BLOCK "StringFileInfo"
    BEGIN
    BLOCK "040004e4"
    BEGIN
    VALUE "FileVersion", STR_Version
    VALUE "FileDescription", STR_Description
    VALUE "ProductName", STR_AppName
    VALUE "ProductVersion", STR_Version
    VALUE "CompanyName", STR_Corporation
    VALUE "LegalCopyright", STR_Copyright
    VALUE "Comments", "Powered by " STR_Author "."
    VALUE "OriginalFilename", STR_AppName EXT_Exe
    END
    END
    BLOCK "VarFileInfo"
    BEGIN
    VALUE "Translation", 0x0400, 0x04e4
    END
    END

 

OK. You only need to modify define. h to change the version number or program information.

Continue ......

Define. in H, assume that ver_release and ver_build indicate the release and build numbers respectively. If we set these two values to indicate the number of times we compiled and the total number of times we compiled using the release method, I wrote a small tool BPP.

(Build ++ ):

First put bpp.exe under the project directory, and then in the VC project attribute, debug
Method: Enter "BPP define. H" in "generate event"-> "pre-generated event"-> "command line". The release mode is "BPP ".
-Release define. h ". In this way, every VC compilation calls bpp.exe to make ver_build or ver_reelase
Auto increment. Cool and cool ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.