简体   繁体   English

c宏扩展错误

[英]Error With c Macro expansion

Hi please find the below simple macro code 嗨,请找到以下简单的宏代码

#include <iostream>

#define INCL    #include

INCL <stdlib.h>

int main()
{
    std::cout << "Hello, world\n" << std::endl ;

    return 0;
}

When I compile the code It throws error: stray â#â in program 当我编译代码时,它将引发错误:程序中出现流浪“#”

Please help. 请帮忙。

Thanks 谢谢

Preprocessor macros cannot create other preprocessor macros. 预处理器宏无法创建其他预处理器宏。 You're just out of luck in this regard. 您在这方面很不走运。

However, you can use a macro as the include argument: 但是,您可以使用宏作为include参数:

#define FOO(x) "/usr/lib/" #x
#include FOO(mylib.h)

C++ build consists of the following stages: C ++构建包括以下阶段:

  1. Preprocessing (macros expansion) 预处理(宏扩展)
  2. Compiling 编译中
  3. Linking 连结中

The point is that you try to use preprocessor (stage 1) to preprocess itself, which won't work. 关键是您尝试使用预处理器(第1阶段)对其本身进行预处理,这将无法正常工作。 The macros can only be used for generating the code for compiler. 宏只能用于为编译器生成代码。 You cannot wrap a preprocessor directive into a preprocessor macro. 您不能将预处理器指令包装到预处理器宏中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM