c++ – Compiling a program with different variable values


I already asked a similar question, but there was a need to pass the string to the program at the compilation stage.

There is a code:

#ifdef VALUE
    std::cout << VALUE << std::endl;
#endif // VALUE
#ifndef VALUE
    char* VALUE = "not defined";
    std::cout << VALUE << std::endl;
#endif // !VALUE

When compiled with the parameter

cl /DVALUE=100 core.cpp

Everything works and the program outputs a number. As I understand it, the / D key is an option only for numbers, but is there a similar key for strings?


This is not for numbers, but for literals. Just in the text VALUE before compilation will be replaced with whatever you substitute.

Want with quotes – substitute in the Windows command line

 cl /DVALUE=\"100\"

In general, read about the preprocessor, it's a big and interesting topic … For example, here .

Scroll to Top