Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Target platform/processor at compile time

Is there a #define in C# that allows me to know, at compile time, if I'm compiling for x86 (Win32) or x64 (Win64)?

like image 803
user62572 Avatar asked Feb 07 '09 17:02

user62572


2 Answers

By default there is no way to do this. The reason is that C# code is not designed to target a particular platform as it runs on the CLR.

It is possible to hand roll this though. You can use the project configuration settings in Visual Studio to define your own constants. Or if you want it a little more streamline you can edit the .csproj yourself and hand roll some more configurations which have various defines.

For instance you can make your project file look like the following. I removed some of the information to make the x86/amd64 information clear.

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
    <!-- ... -->
    <DefineConstants>TRACE;DEBUG;X86</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|amd64' ">
    <!-- ... -->
    <DefineConstants>TRACE;DEBUG;AMD64</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>

Adding this to a .csproj file gives me 2 new platform configurations in my project.

like image 114
JaredPar Avatar answered Sep 23 '22 12:09

JaredPar


As far as I know Visual Studio defines only DEBUG and TRACE constants. Instead of declaring such constant manually in the project configurations you could use NANT to build your project. It can determine the build platform at compile time and define a custom directive accordingly.

like image 36
Darin Dimitrov Avatar answered Sep 21 '22 12:09

Darin Dimitrov