Already asked a similar question, but there was a need to transfer 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 a parameter

 cl /DVALUE=100 core.cpp 

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

  • one
    And if you try cl /DVALUE="some text" core.cpp ? - Majestio
  • @Majestio does not compile because it tries to "some text" - an undeclared identifier ... compiles in single quotes, but something is not transmitted ... - R. Key
  • -DVALUE='"a b"' g++ 1.cpp -DVALUE='"a b"' -o 1 1.cpp works for me on Linux g++ 1.cpp -DVALUE='"a b"' -o 1 - pavel
  • @pavel under Windows does not work, the way Harry works - R. Key

2 answers 2

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

Want with quotes - substitute in the Windows command line

  cl /DVALUE=\"100\" 

In general, read about the preprocessor, this is a big and interesting topic ... For example, here .

  • Thank you. And for the link a special thank you) - R. Key

Compilation line for clang (FreeBSD):

 clang++ -std=c++11 -DVALUE='"Some text"' Hello.cpp -o Hello 

Test code itself:

 #ifndef VALUE #define VALUE "Test" #endif int main() { std::cout << "Value: " << VALUE << std::endl; return 0; } 

Everything works fine.

  • I will clarify. This also works in gcc. The key word here is Linux. - pavel
  • Rather, probably - * nix) - Majestio