标签: conditional-compilation

Mathematica:有条件的"编译"

我正在尝试创建一个条件表达式来初始化一些函数,变量等.在C中看起来像这样的东西:

#if option==1
  int foo(int x){/*some code here*/}
  int q=10;
#else
  char foo(int x){/*some other code*/}
  double q=3.141592;
#endif
use_q(q);
f(some_var);
Run Code Online (Sandbox Code Playgroud)

在Mathematica中,我尝试过使用If,像这样:

If[option==1,
foo[x_]=some_expression1;
q=10;
,
foo[x_]=some_expression2;
q=3.141592;
]
use_q[q];
f[some_var];
Run Code Online (Sandbox Code Playgroud)

但结果是函数的参数被涂成红色,并且在If中没有任何内容被初始化或计算.那么,我该怎样做才能获得有条件的"编译"?

wolfram-mathematica conditional-compilation

0
推荐指数
1
解决办法
323
查看次数

#if ... #else的##快捷方式... #endif喜欢#define something as string

在C#dotNET for Mono中,有更简单的方法吗?

#if __MonoCS__
    public static SqliteConnection NewConnection
#else
    public static SQLiteConnection NewConnection
#endif
Run Code Online (Sandbox Code Playgroud)

在CI中可以#if然后#define something,#else并将其定义为其他内容.

我知道C#预处理器不允许我想要的东西,但有没有更简单的方法来处理SQLite for Windows和Linux之间的差异?

谢谢,
吉姆


感谢那些回答的人.
依赖于SQLite的文件现在包含这样的语句:

#if __MonoCS__
    using Mono.Data.Sqlite;
    using SQLiteCommand =     Mono.Data.Sqlite.SqliteCommand;
    using SQLiteConnection =  Mono.Data.Sqlite.SqliteConnection;
    using SQLiteException =   Mono.Data.Sqlite.SqliteException;
    using SQLiteParameter =   Mono.Data.Sqlite.SqliteParameter;
    using SQLiteTransaction = Mono.Data.Sqlite.SqliteTransaction;
#else
    using System.Data.SQLite;
#endif
Run Code Online (Sandbox Code Playgroud)

在CI中,将所有内容放在.h文件中,并在需要的地方#include它.有没有办法在Mono C#项目中做到这一点?我没有尝试过任何工作.


吉姆,再次感谢

c# mono conditional-compilation

0
推荐指数
1
解决办法
380
查看次数

条件编译常量在 .net 核心多目标 sdk 样式项目中不起作用

以下是最初定义条件编译常量的方式(注意多目标):

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;netcoreapp2.0;net461</TargetFrameworks>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' OR '$(TargetFramework)' == 'netstandard2.0'">
    <DefineConstants>NETCORE;</DefineConstants>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(TargetFramework)' == 'net461'">
    <DefineConstants>NETFULL;</DefineConstants>
  </PropertyGroup>
  ...

</Project>
Run Code Online (Sandbox Code Playgroud)

当时NETCORE常量工作正常。

#if NETCORE
    // Works Fine! Not gray in VS; Compiler recognizes code!
    public string Abc { get; set; }   
#endif
Run Code Online (Sandbox Code Playgroud)

我正在处理我的代码,但当时我的程序集没有编译。


之后,我添加了额外的条件编译常量(不编辑以前的 -NETFULLNETCORE):

<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0'">
    <DefineConstants>NETCOREONLY;</DefineConstants>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

整体代码(程序集)仍未编译。

并删除了NETCOREONLY不需要的附加项,只留下以前的(NETCORENETFULL)。

整体代码(程序集)仍未编译。

问题是它NETCORE不再像以前那样工作了。

我正在切换到netcoreapp2.0平台,但代码

#if NETCORE …
Run Code Online (Sandbox Code Playgroud)

conditional-compilation csproj multitargeting visual-studio .net-core

0
推荐指数
1
解决办法
1023
查看次数

必须仅在调试版本中包含的源文件的条件编译

我一直在阅读有关条件编译的最佳实践,但是我还没有找到关于这种情况的示例。

我有一个C项目,其目标平台是特定设备(不同于PC)。我有一个仅包含集成测试功能之类的源文件。我希望此文件仅在DEBUG构建而不是在构建中进行编译和链接RELEASE

我的问题是以下哪种选择是更好的做法:

一个*.c类似如下的文件:

Tests.c
---------
#ifndef NDEBUG
// All testing functions
...
#endif
Run Code Online (Sandbox Code Playgroud)

并在该文件DEBUGRELEASE构建文件中都包含该文件。

要么

检查是否NDEBUG从项目的内部定义Makefile / CMakeLists.txt并且因此包括提到的源文件。

c embedded macros makefile conditional-compilation

0
推荐指数
1
解决办法
104
查看次数

Cargo:如何将整个目录或文件包含在功能标志中?

我正在开发一个 Rust 项目。我正在使用 Cargo 功能标志来对某些代码进行条件编译。在某些情况下,我必须将整个文件包含在功能标志中,因此这样做添加#[cfg(feature="my-flag")]每个函数和use语句没有多大意义。

因此,为了将整个文件包含在功能标志中,我想将文件中的所有内容包围在一个块中并为该块添加功能标志。

#[cfg(feature="my-flag")]
{
  use crate::access_control::{func1, func2};
  use crate::models;
  ...

  #[derive(Debug)]
  pub enum MyEnum{..}
  
  #[derive(Clone)]
  pub Struct MyStruct{..}

  pub fn my_func() {...}
  fn my_func_internal() {...}
  ...
}
Run Code Online (Sandbox Code Playgroud)

但我收到错误Syntax Error: expected an item after attributes

另外,在某些情况下,我希望将整个目录包含在功能标志中。我该怎么办?为每个文件添加功能标志是一种方法。是否存在更好的方法?

conditional-compilation rust rust-cargo

0
推荐指数
1
解决办法
986
查看次数

C++ 中 #ifdef Defined() 的奇怪行为

我正在 Visual Studio Community 中使用 C++20 编写跨平台代码,但我陷入了以下代码的输出:


#define WINDOWS (defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__))
#define UNIX (defined(__unix__) || defined(__unix))

constexpr bool is_unix1()
{
#ifdef UNIX
    return true;
#else
    return false;
#endif
}

constexpr bool is_unix2()
{
#ifdef defined(UNIX)
    return true;
#else
    return false;
#endif
}


int main()
{
    cout << std::boolalpha << is_unix1() << " " << is_unix2() << endl;
}
Run Code Online (Sandbox Code Playgroud)

当我在 Windows 中从 VS Community 内部运行此代码时,我得到以下输出:

true false
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么is_unix1()评估为falsewhileis_unix2()评估为吗true? …

c++ macros conditional-compilation defined

0
推荐指数
1
解决办法
125
查看次数

C## if/#ifdef语法不能编译,为什么?

为什么下面的代码没有编译(片段)?

  public enum ApplicationType : int
  {
   CONSOLE = 1,
   WINDOWS_FORMS = 2,
   ASP_NET = 3,
   WINDOWS_SERVICE = 4,
   MUTE = 5
  }

        //#if( false)
        //#if (DEBUG && !VC_V7)
 #if( m_iApplicationType != ApplicationType.ASP_NET  )
        public class HttpContext
  {
   public class Current
   {
    public class Response
    {
     public static void Write(ref string str)
     {
      Console.WriteLine(str);
     }
    }
   }
  }
#endif
Run Code Online (Sandbox Code Playgroud)

.net c# syntax conditional-compilation

-1
推荐指数
1
解决办法
3616
查看次数

用#ifdef或类似的东西替换开关/外壳

我试图switch/case通过其他工具替换结构做同样的事情,但具有更好的性能(更少的执行时间...),我想到的#ifdef方法,但我不知道如何在这种情况下使用它:

float k_function(float *x,float *y,struct svm_model model)
{
    int i;
    float sum=0.0;
    switch(model.model_kernel_type)  
    {
    case LINEAR :
        return result1;
    case POLY :
        return result2;
    case RBF :
        return result3;
    case SIGMOID :
        return result4;
    default :
        return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

PS:

typedef   enum   kernel_type   {LINEAR, POLY, RBF, SIGMOID};
Run Code Online (Sandbox Code Playgroud)

c conditional-compilation switch-statement

-1
推荐指数
1
解决办法
115
查看次数

比较 ifdef 指令中的字符串

我在编译过程中传递了一个 macto:

% gcc -DIDENT="abcd" app.c

在编译宏期间检查的正确方法是什么?例如下面的工作,但抛出警告:

#ifdef IDENT == "abcd"
printf("abcd\n");
#endif
Run Code Online (Sandbox Code Playgroud)

警告:#ifdef 指令末尾的额外标记。

c macros conditional-compilation

-2
推荐指数
1
解决办法
1746
查看次数