How do I use the C preprocessor to make a substitution with an environment variable

前端 未结 2 1516
暖寄归人
暖寄归人 2020-12-20 14:31

In the code below, I would like the value of THE_VERSION_STRING to be taken from the value of the environment variable MY_VERSION at compile time

相关标签:
2条回答
  • 2020-12-20 15:13

    If I recall correctly, you can use the command line parameter -D with gcc to #define a value at compile time.

    i.e.:

    $ gcc file.c -o file -D"THE_VERSION_STRING=${THE_VERSION_STRING}"
    
    0 讨论(0)
  • 2020-12-20 15:14

    In the code below, I would like the value of THE_VERSION_STRING to be taken from the value of the environment variable MY_VERSION at compile time

    No, you can't do it like this. The only way to extract environment variables is at runtime with the getenv() function. You will need to explicitly extract the value and copy it to pluginRequires.

    If you want the effect of a compile-time constant, then you'll have to specify the definition on the compiler commandline as Seth suggests.

    0 讨论(0)
提交回复
热议问题