Custom conditional compilation symbols
Asked Answered
B

1

5

I'm currently struggling using custom configurations. My solution has one .NET Standard Library and two other Projects (one for Windows, one for Android) which uses the library.

What I try to do is giving the library the compiler constant WINDOWS and MOBILE.

Thats how I tried it several times:

  1. Create two new configurations WindowsDE and MobileDE, copy settings from Debug configuration and create new project configuration. At some trys I also deleted the default Debug configuration but that didn't helped

  2. Properties of the library -> Build, choose WindowsDE and put WINDOWS into Conditional compilation symbols field then choose MobileDE and put ANDROID in it.

I'm testing it with calling a method in the library :

#if WINDOWS
        System.Diagnostics.Debug.WriteLine("Windows");
#endif

#if ANDROID
        System.Diagnostics.Debug.WriteLine("Android");
#endif

But that doesn't work at all. Also just using

System.Diagnostics.Debug.WriteLine("anything");

without having any #if does not print and at some trys I could not even debug the library anymore.

Would appreciate any help on this

Bodi answered 19/10, 2018 at 22:23 Comment(1)
"But that doesn't work at all" What does this mean? Any exception/error? Unexpected results? If the code isn´t executed it´s most likely because neither WINDOWS nor ANDROID are defined.Amaya
S
12

You can define conditional compiling constants in the project properties

enter image description here

#if WINDOWS
    System.Diagnostics.Debug.WriteLine("Windows"); // NOT printed!
#endif

#if ANDROID
    System.Diagnostics.Debug.WriteLine("Android"); // Printed!
#endif

You can enter several symbols separated by semicolons. Don't set them true or false. The ones that are listed are true. The missing ones are automatically false.

If neither the one nor the other prints, then possibly the solution does not compile and you are running an old code. Try to rebuild the solution.

Solmization answered 19/10, 2018 at 22:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.