如何让Xcode 8 C预处理程序忽略// define中的注释 [英] How to get Xcode 8 C preprocessor to ignore // comments in #defines

查看:59
本文介绍了如何让Xcode 8 C预处理程序忽略// define中的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C预处理器( cpp )似乎应该正确处理以下代码:

The C preprocessor (cpp) seems like it should handle this code correctly:

#define A 1 // hello there

int foo[A];

我希望将 A 替换为 1

发生的情况是将 A 替换为 1 //你好,,这导致 cpp -std = c99 test.c 的输出如下:

What happens is that A is replaced with 1 // hello there, which results in the following output from cpp -std=c99 test.c:

# 1 "test.c"

int foo[1 // hello there];

无效的C且无法编译。

如何获取 cpp 来执行正确的替换?

How can I get cpp to perform the proper replacement?

有关编译器的注意事项:在Mac上使用最新的($ 8.2.1,2016年12月)Xcode中的 cpp ,所以我怀疑这是由于编译器过时造成的。

Note on compiler: Using cpp from the latest (8.2.1, Dec 2016) Xcode on mac, so I doubt it's due to an outdated compiler.

推荐答案

令我惊讶的是,我可以在Mac上重现该问题(macOS Sierra 10.12.2; Apple LLVM版本8.0。 0(clang-800.0.42.1))使用 / usr / bin / cpp ,这是XCode cpp —但不使用GNU cpp (我仅使用 cpp 调用)。

Somewhat to my surprise, I can reproduce the problem on my Mac (macOS Sierra 10.12.2; Apple LLVM version 8.0.0 (clang-800.0.42.1)) using /usr/bin/cpp which is the XCode cpp — but not using GNU cpp (which I invoke using just cpp).

解决方法包括:

/usr/bin/gcc -E -std=c99 test.c

这使用 clang 包装器 gcc 运行C预处理程序并正确处理版本。您可以添加 -v 选项并查看其运行方式。我没有看到它本身运行 cpp (它运行 clang -cc1 -E 以及许多其他信息) 。

This uses the clang wrapper gcc to run the C preprocessor and correctly handles the version. You could add a -v option and see what it runs; I didn't see it running cpp per se (it runs clang -cc1 -E with lots of other information).

您还可以使用:

clang -E -std=c99 test.c

实际上是同一回事。

您也可以安装GCC并使用它代替XCode。对于如何完成该任务存在一些疑问(但这并不是出于胆小)。

You could also install GCC and use that instead of XCode. There are questions with answers about how to get that done (but it isn't for the faint of heart).

这篇关于如何让Xcode 8 C预处理程序忽略// define中的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆