Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ “是什么意思?”;“不活跃”;C中的预处理器块?_C++_C_Visual Studio_C Preprocessor - Fatal编程技术网

C++ “是什么意思?”;“不活跃”;C中的预处理器块?

C++ “是什么意思?”;“不活跃”;C中的预处理器块?,c++,c,visual-studio,c-preprocessor,C++,C,Visual Studio,C Preprocessor,我在C中找到了一些代码行,它们在我的源代码预处理器块中变暗了。我的编译器MS Visual Studio将其命名为“非活动预处理器块”。这意味着什么,我的编译不会考虑这些代码行吗? 如何使其成为活动块?非活动预处理器块是由于预处理器指令而被停用的代码块。最简单的例子是: #if 0 //everytyhing here is inactive and will be ignored during compilation #endif 一个更常见的例子是 #ifdef SOME_VAR // c

我在C中找到了一些代码行,它们在我的源代码预处理器块中变暗了。我的编译器MS Visual Studio将其命名为“非活动预处理器块”。这意味着什么,我的编译不会考虑这些代码行吗?
如何使其成为活动块?

非活动预处理器块是由于预处理器指令而被停用的代码块。最简单的例子是:

#if 0
//everytyhing here is inactive and will be ignored during compilation
#endif
一个更常见的例子是

#ifdef SOME_VAR
// code
#else
// other code
#endif

在这种情况下,第一个或第二个代码块将处于非活动状态,这取决于是否定义了
SOME\u VAR

请检查为阐述您的问题而创建的假设示例

#include <iostream>
#include <windef.h>

#define _WIN32

int add(int n1, int n2){return n1 + n2;}

LONGLONG add(LONGLONG n1, LONGLONG n2){return n1 + n2;}

int _tmain(int argc, _TCHAR* argv[])
{
#ifdef _WIN32
    int val = add(10, 12);
#else
    LONGLONG = add(100L, 120L);//Inactive code
#endif // _WIN32
    return 0;
}
#包括
#包括
#定义WIN32
intadd(intn1,intn2){返回n1+n2;}
隆隆加法(隆隆n1,隆隆n2){返回n1+n2;}
int _tmain(int argc,_TCHAR*argv[]
{
#ifdef_WIN32
int val=加(10,12);
#否则
LONGLONG=add(100L,120L);//非活动代码
#endif/\u WIN32
返回0;
}
您可以看到_WIN32已定义,#else预处理器指令中的代码已禁用,将不会编译。您可以取消定义WIN32以查看反向操作。请参见附加的MS Visual Studio的屏幕截图。红色的行是禁用代码


希望这会有所帮助。

预处理器是程序翻译的最早阶段之一。它可以在编译阶段开始之前修改程序的源代码。通过这种方式,您可以根据各种约束将源配置为以不同的方式构建

预处理器条件块的用途包括:

  • 完全注释掉代码:

    #if 0
    // The code here is never compiled. It's "commented" away
    #endif
    
  • 基于各种约束提供不同的实现,如platfrom

    #if defined(WIN32)
      //Implement widget with Win32Api
    
    #elif defined(MOTIF)
      // Implement widget with Motif framework
    
    #else
      #error "Unknown platform"
    #endif
    
  • 有一个类似宏的行为方式不同

  • 确保适当定义了有用的抽象:

    #if PLATFORM_A
      typedef long int32_t;
    #elif PLATFORM_B
      typedef int  int32_t;