导致 cpp(C 预处理器)删除 C++对 Mac OS X 的评论

发布于 2024-10-01 14:13:05 字数 398 浏览 4 评论 0原文

我有一份用户报告(不幸的是,由于缺乏合适的机器,无法验证它),Mac OS X 10.6.4 上的 C 预处理器 (cpp) 命令不会删除 C++/C99 双斜杠// 来自它处理的文件的注释,无论给出什么选项。这是 gcc 版本:

 i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664)

是否有可能以某种方式导致它删除此类注释,正如人们对 C++ 预处理器所期望的那样(这是需要的,因为 cpp 被用作 另一个工具)。

I have a user report (unfortunately can't verify it due to lack of appropriate machine) that the C preprocessor (cpp) command on Mac OS X 10.6.4 doesn't remove C++/C99 double slash // comments from files it processes, no matter what option it's given. This is the gcc version:

 i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664)

Is it possible to somehow cause it to remove such comments, as one would expect from a C++ preprocessor (this is needed because cpp is used as part of another tool).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还不是爱你 2024-10-08 14:13:05

我找到了一个适用于 cpp 命令的公式:尝试 cpp -xc++ (请注意 -x之间缺少空格>c++)。

$ printf '/* block comment */\n// line comment\nnot a comment\n' | cpp -xc++
# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "<stdin>"


not a comment

对比:

$ printf '/* block comment */\n// line comment\nnot a comment\n' | cpp -x c++
i686-apple-darwin10-gcc-4.2.1: c: No such file or directory
i686-apple-darwin10-gcc-4.2.1: c++: No such file or directory
i686-apple-darwin10-gcc-4.2.1: warning: '-x -x' after last input file has no effect
i686-apple-darwin10-gcc-4.2.1: no input files

现在 '-x c++' 应该可以工作了,并且确实可以在我的 Linux 机器上工作(使用 gcc 4.4,但我记得它早在 gcc 2.95 就可以工作),所以看来苹果打破了它。

我确实必须再次强调为此类问题提供完整、精确的测试用例的重要性。昨天我并没有想到要寻找 Apple 引入了一个错误,因为我知道 wilx 的答案应该有效,并且在没有对 OP 用户尝试的内容进行精确描述的情况下,它是更有可能的是,他们的实际命令行上有其他东西否定了它。如果我上面显示的命令行和错误消息是在原始问题中提供的,那将更有效地吸引每个人的注意力。

I've found a formula that works with the cpp command: try cpp -xc++ (note the lack of spaces between -x and c++).

$ printf '/* block comment */\n// line comment\nnot a comment\n' | cpp -xc++
# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "<stdin>"


not a comment

contrast:

$ printf '/* block comment */\n// line comment\nnot a comment\n' | cpp -x c++
i686-apple-darwin10-gcc-4.2.1: c: No such file or directory
i686-apple-darwin10-gcc-4.2.1: c++: No such file or directory
i686-apple-darwin10-gcc-4.2.1: warning: '-x -x' after last input file has no effect
i686-apple-darwin10-gcc-4.2.1: no input files

Now '-x c++' is SUPPOSED to work, and DOES work on my Linux box (with gcc 4.4, but I recall it working as long ago as gcc 2.95) so it seems that Apple broke it.

I really must reemphasize the importance of providing a complete, precise test case for questions like these. It did not occur to me yesterday to look for Apple having introduced a bug, because I know that wilx's answer should have worked, and in the absence of a precise description of what the OP's user tried, it was far more likely that they had something else on their actual command line that was negating it. If the command line and error messages I show above were provided in the original question, that would have targeted everyone's attention much more effectively.

醉态萌生 2024-10-08 14:13:05

尝试将 -x c++-xc -std=c99 添加到命令行。

Try adding either -x c++ or -x c -std=c99 to the command line.

薄荷港 2024-10-08 14:13:05

一种似乎可行的部分解决方案是调用 gcc -E 而不是 cpp。

 -E                       Preprocess only; do not compile, assemble or link

这确实删除了 Mac OS X 上的 // 注释。

但是,我仍然很好奇为什么 cpp 本身存在问题。

One partial solution that appears to work is invoke gcc -E instead of cpp.

 -E                       Preprocess only; do not compile, assemble or link

This indeed strips // comments on Mac OS X.

However, I'm still curious why there are problems with cpp itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文