Xcode 和预处理器 ##

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4383929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:58:54  来源:igfitidea点击:

Xcode & preprocessor ##

xcodepreprocessor

提问by Chris Masterton

In Xcode can I use ## in a macro?

在 Xcode 中,我可以在宏中使用 ## 吗?

In MSVC I can write:

在 MSVC 中,我可以写:

#define FOO(_var) int foo##_var## = 1

    FOO(bar);
    foobar++;

On the Mac (edit:compiling with GCC) the same code gives me the error "Pasting "foobar" and "=" does not give a valid preprocessing token. Is ## not supported in xcode?

在 Mac 上(编辑:使用 GCC 编译)相同的代码给了我错误“粘贴“foobar”和“=”没有给出有效的预处理标记。xcode 不支持 ## 吗?

回答by Nicholas Riley

Concatenation is supported in GCC and Clang. Xcode isn't a compiler; if you're posting errors like this, check what version of GCC, LLVM-GCC or Clang ("LLVM compiler") you're using because their behavior can differ.

GCC 和 Clang 支持连接。Xcode 不是编译器;如果您发布这样的错误,请检查您使用的是哪个版本的 GCC、LLVM-GCC 或 Clang(“LLVM 编译器”),因为它们的行为可能有所不同。

You're trying to make =part of an identifier (i.e., create a variable called foobar=) which I don't think is what you want.

您正在尝试制作=标识符的一部分(即,创建一个名为 的变量foobar=),我认为这不是您想要的。

Try #define FOO(_var) int foo##_var = 1instead.

试试吧#define FOO(_var) int foo##_var = 1

Incidentally, Clang gives a somewhat better error message:

顺便说一下,Clang 给出了一个更好的错误信息:

foo.c:4:5: error: pasting formed 'foobar=', an invalid preprocessing token
    FOO(bar);
    ^
foo.c:1:32: note: instantiated from:
#define FOO(_var) int foo##_var## = 1
                               ^