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
- # Include
- /* The following two macros are used to input compilation information through macro definition during compilation.
- Version is replaced with const char version [] = "resivion: xxxx"
- Buildtime is replaced with const char buildtime [] = "buildtime: yyyymmdd"
- */
- Version;
- Buildtime;
- Void print_version ()
- {
- Printf ("% s/n", version );
- }
- Void print_buildtime ()
- {
- Printf ("% s/n", buildtime );
- }
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
- # Include
- Int main ()
- {
- Print_version ();
- Print_buildtime ();
- }
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:
- Version = "const char version [] =/" Revision: 1.2.2 /""
- Buildtime = "const char buildtime [] =/" buildtime: 'date + % Y % m % D '/""
- Cflags =-C-g-dversion = "$ version"-dbuildtime = "$ buildtime"
- Testversion: Test. o version. o
- CC-o $ @ $?
- . C. O:
- $ (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:
- $ Testversion
- Revision: 1.2.2
- Buildtime: 20100402
- $
Run the strings command to View Details:
- $ String testversion | grep Revision
- Revision: 1.2.2
- $ Strings testversion | grep buildtime
- 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!