如何使用#if决定要为C#编译哪个平台 [英] How to use #if to decide which platform is being compiled for in C#

查看:438
本文介绍了如何使用#if决定要为C#编译哪个平台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,有一些预定义的宏:

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;

-后来-

#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?

推荐答案

您可以将所需的任何常量添加到 .csproj 文件。可以将它们放入下面的条件属性组中。

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>

对于我的Release x64版本,我定义了一个X64常量,可以这样使用:

For my Release x64 build, I have defined a X64 constant that I can use like this:

#if X64

#endif

这篇关于如何使用#if决定要为C#编译哪个平台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆