none
how to use global #define to let all file know? RRS feed

  • Question

  • Dear All,

    Is it possible to define the "#define" statement in up-most file and this file is imported by other files to do conditional compiling?

    Example,

    #define READ_FUNCTION_SUPPORTED is defined in FunctionSwitch.cs

    there is a code block inside the statement pair "#if READ_FUNCTION_SUPPORTED ....#end" in UI.cs file

    and there is a code block inside the statement pair "#if READ_FUNCTION_SUPPORTED ....#end" in Business.cs file

    Thanks and Best regards,

    E-John

    Monday, October 15, 2018 2:37 AM

All replies

  • >Is it possible to define the "#define" statement in up-most file and this file is imported by other files to do conditional compiling?

    You can pass definitions via the command line, or in the VS IDE for a C# project, use the project's Build, conditional compilation
    symbols setting. See here:
    https://docs.microsoft.com/en-us/visualstudio/ide/reference/build-page-project-designer-csharp?view=vs-2017

    Dave

    Monday, October 15, 2018 9:01 AM
  • Monday, October 15, 2018 11:05 AM
  • Hi E-John,

    Welcome to the MSDN forum.

    Refer to your description, this issue is about the C# development. Since our forum is to discuss the VS IDE, I will help you move this thread to the appropriate forum for a better support, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Tuesday, October 16, 2018 1:59 AM
  • Hi E-John,

    For your question, please refer to the thread in StackOverFlow.

    It show how to use #define in multiple files.

    https://stackoverflow.com/questions/13836501/preprocessor-directives-across-different-files-in-c-sharp

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 16, 2018 7:38 AM
    Moderator
  • Hi Wendy,

    Here is my test.

    I have two files, one is TestGlobalDefinition.cs in which Main() is defined.

    The other is GlobalDefinition.cs(.NET ClassLibrary) which put my global definition. I put a #define PRINT_HELLO in this file.

    And I import the GlobalDefinition library by "using GlobalDefintion"

    But the "#define PRINT_HELLO" seems don't be included(it works for c compiling environment, #include"global_definition.h")

    Is it possible let TestGlobalDefinition.cs to know the "#define PRINT_HELLO", like c compiling enviornment?

    Thanks and Best regards,

    E-John

    using GlobalDefinition;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace TestGlobalDefinition
    {
        class Program
        {
            static void Main(string[] args)
            {
                Class1 c = new Class1();
    #if PRINT_HELLO
                Console.WriteLine("Hello!");
    #endif
                Console.ReadLine();
            }
        }
    }

    #define PRINT_HELLO
    
    using System;
    
    namespace GlobalDefinition
    {
        public class Class1
        {
        }
    }

    Wednesday, October 17, 2018 2:57 AM
  • Symbols are per project so generally you set them that way.

    However starting with VS 2017 you can also use a directory.build.props file. This file is found either in the project or solution level. It is generally used to set solution-level settings. You can define a symbol in there at the solution level and the child projects will inherit it. Here's an example we use for multi-targeted projects.

    <Project>
       <!-- Shared solution settings -->
       <PropertyGroup Condition="'$(TargetFramework)'=='net47'   Or '$(TargetFramework)'=='net471' Or '$(TargetFramework)'=='net472'">      <DefineConstants>$(DefineConstants);NET_FRAMEWORK</DefineConstants>
       </PropertyGroup>
    </Project>
    We also use this for shared versioning in SDK projects.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, October 17, 2018 2:14 PM
    Moderator