Write compilation information to binary Execution Code through macro definition

Source: Internet
Author: User

From: http://bbs.chinaunix.net/thread-1685440-1-1.html

ManyProgramPersonnel have encountered such a problem: the system has been running online for a long time, and many of the programs have been modified and upgraded many times, the information in the log is totally different from the existing program! How can I view the program information (such as the version number and Compilation Time) to determine the exact version of the program? Many version management tools (such as CVS and SVN) can help you manage the source code versions, but do not write the compilation information to the binary Execution Code during compilation. Therefore, it is still necessary to implement it through other means.
Here I provide a way to write compilation information in an executable program. I hope it will be helpful to you.
This example consists of two programs: Test. C and version. C. Version. C is used to compile information records, as long as the existingCodeWithout modifying the existing code, you can manage the version information of the binary Execution Code.
Version. c

    1. # Include
    2. /* The following two macros are used to input compilation information through macro definition during compilation.
    3. Version is replaced with const char version [] = "resivion: xxxx"
    4. Buildtime is replaced with const char buildtime [] = "buildtime: yyyymmdd"
    5. */
    6. Version;
    7. Buildtime;
    8. Void print_version ()
    9. {
    10. Printf ("% s/n", version );
    11. }
    12. Void print_buildtime ()
    13. {
    14. Printf ("% s/n", buildtime );
    15. }

Print_version () and print_buildtime () functions are not mandatory. They only make version. C look "more like a program" and provide a means to output compilation information in an external program.
Test. c

    1. # Include
    2. Int main ()
    3. {
    4. Print_version ();
    5. Print_buildtime ();
    6. }

The two functions called in test. C are only used for testing. The actual code can be replaced in the program of the official version.
Add the following content to makefile:

    1. Version = "const char version [] =/" Revision: 1.2.2 /""
    2. Buildtime = "const char buildtime [] =/" buildtime: 'date + % Y % m % D '/""
    3. Cflags =-C-g-dversion = "$ version"-dbuildtime = "$ buildtime"
    4. Testversion: Test. o version. o
    5. CC-o $ @ $?
    6. . C. O:
    7. $ (CC) $ (cflags) $ <

If you use a version management tool, you can use the tool to dynamically read version (or revision) information and assign the information to the version variable. You can define macro-defined formats by yourself, such as writing the exact Compilation Time.
After compilation, execute:

    1. $ Testversion
    2. Revision: 1.2.2
    3. Buildtime: 20100402
    4. $

Run the strings command to View Details:

    1. $ String testversion | grep Revision
    2. Revision: 1.2.2
    3. $ Strings testversion | grep buildtime
    4. Buildtime: 20100402

This result is what I really want! You only need to check the string in the binary Execution Code to determine the program version!

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.