Using "dotnet build" with an .sln, how to pass preprocessor symbol on command line to a particular .csproj?

Viewed 32

I've found information on how to do this when building a single .csproj directly, but when I do the same thing forbuilding a complete solution, the project within the solution that needs to see the preprocessor I'm defining isn't seeing it.

This is the example I'm trying to follow: http://mikehadlow.blogspot.com/2020/08/c-preprocessor-directive-symbols-from.html

The build is being done with TeamCity, which is executing a "dotnet build" on our solution file, and I've added /p:MyOption="MY_OPTION" in the "Command line parameters" field for the appropriate TeamCity solution build step.

I've also added <DefineConstants>$(MyOption)</DefineConstants> inside a <PropertyGroup> to the .csproj file where I need the to see the MY_OPTION flag defined.

In my C# code, I have a section like this:

#if MY_OPTION
    // ...internal-use implementation...
#else
    // ...public implementation...
#endif

When I look in the TeamCity build logs I can see that my command line option is indeed being properly passed along to the build, and there are no error being generated.

The desired effect of the preprocessor symbol being defined doesn't happen, however. The public implementation is all I get.

Can someone suggest a way to make this work correctly?

0 Answers
Related