12

Trying to define a preprocessor directives in the Visual studio 2012.

#define FLAG
....
#endif

But not sure, where to include this FLAG in visual studio - C#. I remember specifying something like this in C++ projects.

Any thoughts ?

  • 1
    They're called "preprocessor directives" anyway, even though the compiler doesn't have a preprocessor. https://msdn.microsoft.com/en-us/library/ed8yd1ha.aspx – Robert Harvey May 20 '15 at 13:14

2 Answers2

27

You have two options as to where to define it:

  1. Code file level - In the beginning of the file write #define FLAG. You cannot place anything else (other than comments and blank lines) before define directives. As Ron Beyer points out, a directive defined in a file exists only for that file.

  2. Project level - Right click in the project in Solution Explorer, select Properties, then the Build tab, then look at Conditional compilation symbols. Then one can define several comma-separated symbols there such as: FLAG,FOO,BAR. Note that this symbols list is project configuration dependent (there is a configuration selector in the same tab).


Note as mentioned in the comments, define does not work in C# the same way that it does in C. In C# you just declare that the symbol exists, but you can't assign a value to it. Hence the only use for these symbols is for #if FLAG directives and for the Conditional attribute.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Konamiman
  • 49,681
  • 17
  • 108
  • 138
  • 1
    You should note that defining it at the code file level only defines the flag for that code file, it doesn't do it project wide. Its a departure from how C++ works. – Ron Beyer May 20 '15 at 13:18
  • 2
    Also note, you cannot specify *values* for these definitions. You can only specify *the definition itself.* – Der Kommissar May 20 '15 at 13:36
  • 1
    if you define it at the project level, where does VS store that info? I can't find it in the .csproj or .sln files, and I don't seem to be able to remove it – mcmillab Jun 06 '18 at 06:08
  • 1
    @mcmillab it's in the .csproj under \Project\PropertyGroup\DefineConstants – HuBeZa Jul 03 '18 at 11:53
2

For some reason, when properties clicked, nothing happened for me. So I did the following. And it worked.

Open your csproj project file using text editor and add your preprocessor compiler directives in between these. <DefineConstants> HERE!!! </DefineConstants>

Lenvanthis
  • 71
  • 1
  • 6