标签: preprocessor-directive

用于启用和禁用代码功能的 C 宏

我之前使用过一个代码库,它有一个用于启用和禁用代码部分的宏系统。它看起来像下面这样:

#define IN_USE      X
#define NOT_IN_USE  _

#if defined( WIN32 )
    #define FEATURE_A       IN_USE
    #define FEATURE_B       IN_USE
    #define FEATURE_C       NOT_IN_USE
#elif defined( OSX )
    #define FEATURE_A       NOT_IN_USE
    #define FEATURE_B       NOT_IN_USE
    #define FEATURE_C       IN_USE
#else
    #define FEATURE_A       NOT_IN_USE
    #define FEATURE_B       NOT_IN_USE
    #define FEATURE_C       NOT_IN_USE
#endif
Run Code Online (Sandbox Code Playgroud)

然后功能的代码将如下所示:

void DoFeatures()
{
#if USING( FEATURE_A )
    // Feature A code...
#endif

#if USING( FEATURE_B )
    // Feature B code...
#endif

#if USING( FEATURE_C )
    // Feature C code...
#endif

#if USING( FEATURE_D ) // …
Run Code Online (Sandbox Code Playgroud)

c c++ macros c-preprocessor preprocessor-directive

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

#define 在#define 中;在预处理器中会发生什么?

如果我有:

#define X 5
#define Y X
Run Code Online (Sandbox Code Playgroud)

这种事情的预处理器会发生什么?它是否遍历整个文件并将每个 X 更改为 5,然后返回到下一个定义,然后将每个 Y 更改为 5(因为在前一次迭代中 Y 得到了 5)?

c-preprocessor preprocessor-directive

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

发布时不会编译调试模式的代码

在某些地方,我使用#if DEBUG编译器指令来选择要编译的代码。直到不久前,我还能够以调试或发布模式发布 ASP.NET MVC 应用程序。事实上我仍然可以做到这一点。但现在,由于某种原因,无论我选择哪种模式,我在输出中都只能得到 Release 代码。如果我使用 .NET Reflector 查看生成的 dll,我可以看到后面的代码#if DEBUG不存在。我已经Define DEBUG constant在项目属性中检查了调试模式。我可以让它工作的唯一方法是显式定义 DEBUG 常量。除了发布时的设置之外,还有其他地方可以配置发布设置吗?

asp.net-mvc publish debug-mode preprocessor-directive

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

什么是最好的"类型重命名"方法?

如果我想将std::string类型重命名为更简单和更自然的样式,string我应该使用这两种方法中的哪一种(基于性能和通常的标准)

我应该将其重命名为预处理指令

#define string std::string
Run Code Online (Sandbox Code Playgroud)

或者使用类型定义

typedef std::string string;
Run Code Online (Sandbox Code Playgroud)

什么是最高效的方法?什么也更熟悉和社区认可?

c++ typedef user-defined-types preprocessor-directive

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

如何在一个地方进行全局预处理器定义而不产生大量警告?

我想要一种具有“全局预处理器定义”的方法,以便我可以在编译之前更改单个值以添加或删除程序的功能。目前我有一个“全局脚本”(称为 God.cs),其常量如下:

public const bool PRINT_RUN_VALUES = true;
public const bool DEBUG_MOVEMENT = false;
public const bool DOUBLE_SPEED = false;
Run Code Online (Sandbox Code Playgroud)

所有相关脚本都使用这些值,例如:

在脚本1中:

if (God.PRINT_RUN_VALUES) {
   float averageDist = /* calculate values */
   print("Script1: averageDist = " + averageDist);
}
Run Code Online (Sandbox Code Playgroud)

在脚本2中:

if (God.PRINT_RUN_VALUES) {
   print("Script2: radius = " + radius);
   print("Script2: time = " + time);
}
Run Code Online (Sandbox Code Playgroud)

这种方法的问题是我得到了很多

CS0162:检测到无法访问的代码

警告。我可以使用以下方法关闭这些警告:

#pragma warning disable 0162
#pragma warning disable 0429
Run Code Online (Sandbox Code Playgroud)

但我不想这样做,因为这些警告在不涉及 God.cs 中的这些值的情况下可能很有用。另一种方法可能是使用经典的预处理器定义,例如:

#define PRINT_RUN_VALUES
...
#if (PRINT_RUN_VALUES)
   float averageDist = /* calculate …
Run Code Online (Sandbox Code Playgroud)

c# preprocessor global definition preprocessor-directive

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

C++ 中的预处理器指令:以下代码的输出是什么?

学生在这里。我有以下代码,但我对其输出感到困惑。当我运行这段代码时,它告诉我 C 将是 2,但我认为它会是 0。为什么是 2?泰!

#include <iostream>
using namespace std;

#define A    0
#define B    A+1 
#define C    1-B

int main() {
cout<<C<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ preprocessor-directive

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

Makefile 触发 C/C++ 预处理器指令的重建

假设我有一个 C++ 源文件,如下所示:

int main() {
...
#ifdef flag
    // do something
#endif
...
#ifndef flag
    // do something different
#endif
...
}
Run Code Online (Sandbox Code Playgroud)

我知道对于 gcc,我可以用作-D'flag'预处理器的指令来定义flag,从而确定上面代码的哪一部分实际被编译,并且这可以在 Makefile 中使用。

但是我如何编写一个 Makefile,以便根据我是否想要flag定义来重新构建源文件(不对任何文件进行其他更改,包括 Makefile 本身)?

例如,我知道我可以(如果有更好的方法,解决方案不需要遵循这个!),当我运行 make eg with 时定义一个变量make flag=1 all,但是如果文件包含main(与上面的源代码示例一致)此后一直没有更改。

即从一个干净的目录开始并运行make all将运行构建,但是如果我立即运行,make flag=1 all我希望包含main(例如main.cpp)的文件能够正确重建,而不必touch main.cpp首先。

c++ gcc makefile gnu-make preprocessor-directive

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

当遇到未知指令时,C/C++ 预处理器如何工作?

c/c++ 预处理器是否处理所有以 # 开头的行?遇到未知宏时会出错还是会忽略它们?

举个例子,

#include <stdio.h>

#hello
int main(){
    printf("Hello World!");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下会发生什么?它会产生错误还是会起作用(忽略#hello行)?

c c++ preprocessor c-preprocessor preprocessor-directive

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

预处理器指令在条件不起作用时

尝试使用预处理程序指令中定义的变量时:

  #define TIME_CONST 20;
Run Code Online (Sandbox Code Playgroud)

在一段时间的条件:

  while(i<TIME_CONST){...}
Run Code Online (Sandbox Code Playgroud)

抱怨抱怨的我有错误...

我用的时候:

  while(i<20)
Run Code Online (Sandbox Code Playgroud)

一切正常.

我究竟做错了什么?

先感谢您!

c c++ preprocessor-directive

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

是否存在预处理'AND'指令

是否有Preproccessor AND指令?如果不是,你会怎么建议我解决以下任务?

#define INSTALL_V8
#define INSTALL_V9
#define INSTALL_V10  // Using preprocessor directives and not static variables to
// avoid packaging unnecessary code into the application/installer

#ifdef INSTALL_V8 AND INSTALL_V9 AND INSTALL_V10
  #define CHECK_BOX_STRT_Y 60 // move the start y up so we have room to fit 3 checkboxes in the window
#else
  #define CHECK_BOX_STRT_Y 80 // place 1st checkbox in middle of window
#endif
Run Code Online (Sandbox Code Playgroud)

c-preprocessor preprocessor-directive

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