Use environment variable as compile time constant in C ++

As part of the build process, I need to take the environment variable defined by the script package and use it as a constant in the code at compile time.

For example, suppose I defined an environment variable with the name BUILD_VERSIONand set it to 1.0.0, when it compiles, I want to 1.0.0bake into my code. EG:

Batch file:

set BUILD_VERSION = 1.0.0
; call vs compiler

C ++ file:

const std::string build_version = BUILD_VERSION // Which will result in "1.0.0".

How should I do it?

+4
source share
3 answers

txchelp /D " → " , .

:

enter image description here

script, :

set SVN_BUILD_VERSION=1.0.0

, , :

#define STRINGIZER(arg)     #arg
#define STR_VALUE(arg)      STRINGIZER(arg)
#define BUILD_VERSION_STRING STR_VALUE(BUILD_VERSION)

// ...

const std::string version = BUILD_VERSION_STRING; // Results in "1.0.0".
+4

( , Visual Studio), script, , ++ "1.0.0.0" - "1.0.0.1".

awk.

0

A VERSION_INFO .

The embedded version information can be checked by right-clicking the executable file and checking its properties.

To do this, on the command line:

  • Redirect the output from the batch file to the [.rc] file that defines the resource.

  • Compile the resource with rc.exe.

  • Paste the resulting file .ressimply by passing it to the linker.

In Visual Studio, this can be trickier.

0
source

Source: https://habr.com/ru/post/1534752/


All Articles