Reputation: 324
In C++ there are predefined macros:
#if defined(_M_X64) || defined(__amd64__)
// Building for 64bit target
const unsigned long MaxGulpSize = 1048576 * 128;// megabyte = 1048576;
const unsigned long MaxRecsCopy = 1048576 * 16;
#else
const unsigned long MaxGulpSize = 1048576 * 8;// megabyte = 1048576;
const unsigned long MaxRecsCopy = 1048576;
#endif
Which allows me to set constants to control the amount of memory that will be used.
Of course I can define a preprocessor variable verbatim:
#define Is64bit 1
using System;
using System.Collections.Generic;
-later-
#if Is64bit
// Building for 64bit target
const long MaxGulpSize = 1048576 * 128;// megabyte = 1048576;
const long MaxRecsCopy = 1048576 * 16;
#else
const long MaxGulpSize = 1048576 * 8;// megabyte = 1048576;
const long MaxRecsCopy = 1048576;
#endif
I cannot find a way to detect the platform based on the values set in the configuration manager which would allow for command line building:
set de=C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe
set sol=E:\Some\Path\to\my.sln
"%de%" %sol% /build "Release|x86"
"%de%" %sol% /build "Release|x64"
Is there a way to detect this or will I have to build, change platform and build again?
Update: The 32/64 bit question became a moot point when the last of the 32bit Windows 7 workstations were retired so needing to compile for both platforms was no longer necessary. I think though it's worthwhile to retain this thread as the concepts can help with other preprocessor directives, targeting MS Office vs Open Office for example.
Upvotes: 15
Views: 13337
Reputation: 7189
In vb.net project is possible to use PLATFORM variable
#If PLATFORM == "x86" Then
Imports MyProxy = MyX86dll.MyClass
#Else
Imports MyProxy = MyX64dll.MyClass
#End If
Upvotes: 1
Reputation: 51
You can also just define a symbol (e.g. _x64) in the project properties for the x64 platform. Open the properties dialogue of your project, select the x64 platform, on the Build page, just put "_x64" into the "Conditional compilation symbols" box.
Make sure to do this for both debug and release configuration.
Upvotes: 3
Reputation: 38079
You can add any constants you want to the .csproj
file. These can be put into conditional property groups like the one below.
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<DefineConstants>TRACE;X64</DefineConstants>
...
</PropertyGroup>
For my Release x64 build, I have defined a X64 constant that I can use like this:
#if X64
#endif
Upvotes: 25
Reputation: 13946
Make sure your design considers whether this needs to be known at compile time or at runtime. If it is compile time, then yes, you will probably need to define a constant. This can be passed on the command line, or if using Visual Studio or MSBUILD, through a configuration. Change the configuration and build again.
If it is runtime, search through the answers to questions such as How to know a process is 32-bit or 64-bit programmatically... and friends.
However, it is also possible that this distinction may not matter, depending on the needs of your application. .NET manages its own memory, and nothing is stopping your built-for-x86 assembly from running on a 64-bit machine. If you are interopping with unmanaged code, are there any externals from your library that can tell you what sizes you should be using, instead of assuming?
Upvotes: 1