代码之家  ›  专栏  ›  技术社区  ›  Eli Bendersky

导致CPP(C预处理器)删除Mac OS X上的C++注释

  •  2
  • Eli Bendersky  · 技术社区  · 15 年前

    cpp 命令在Mac OS X0.4.4上不删除C++/C99双斜线 // 它处理的文件中的注释,不管它提供了什么选项。这是gcc版本:

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

    cpp公司 用作 another tool ).

    3 回复  |  直到 10 年前
        1
  •  2
  •   zwol    15 年前

    我找到了一个公式 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系统上也能工作(使用GCC4.4,但我记得它早在GCC2.95之前就工作了),所以苹果似乎把它弄坏了。

    我真的必须再次强调为这些问题提供一个完整、精确的测试用例的重要性。我昨天没有想到苹果会引入一个bug,因为我知道wilx的答案 应该有

        2
  •  0
  •   wilx    15 年前

    尝试添加 -x c++ -x c -std=c99

        3
  •  0
  •   Eli Bendersky    15 年前

    一个看起来有效的部分解决方案是调用 gcc -E 而不是 cpp

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

    这真是脱光了 //

    不过,我还是很好奇为什么 cpp公司 本身。