prevent gcc from removing an unused variable

前端 未结 5 955
不思量自难忘°
不思量自难忘° 2020-12-16 12:27

In our source files we usually have a version string like that:

static const char srcvers[] = \"VERSION/foo.c/1.01/09.0         


        
5条回答
  •  遥遥无期
    2020-12-16 12:42

    As it seems that all the solutions require some kind of decoration of the version string in the source, it may help to define a macro containing all the necessary syntax and then use this macro in the source or header files whenever need:

    #define SRCVERSION(file, version, data) static const char _ver[] __attribute__((used)) = "VERSION/" file "/" version "/" date;
    

    Then in your source just put

    SRCVERSION("foo.c", "1.01", "09.04.15")
    

    The macro may be in a central project header file or on the compiler's command line.

    That way, at least you do not have to touch all the source files again if you want to change something about the definition.

    Note how the macro definition uses string concatenation to build the final version string. Also it contains the final semicolon so you can remove everything by defining an empty macro if needed.

提交回复
热议问题