如何编译已经用GCC进行了C预处理的C代码?

如何编译已经用GCC进行了C预处理的C代码?,c,compilation,C,Compilation,我正在C预处理和C编译之间执行一些源代码处理。此刻我: gcc-E file.c>预处理的\u file.c 对预处理的文件.c执行更多操作 使用预处理的_file.c继续编译 如果您尝试编译预处理的_file.c,就像编译普通的c(步骤3)一样,您会得到以下许多结果: /usr/include/stdio.h:257: error: redefinition of parameter ‘restrict’ /usr/include/stdio.h:257: error: previous de

我正在C预处理和C编译之间执行一些源代码处理。此刻我:

  • gcc-E file.c>预处理的\u file.c
  • 预处理的文件.c
    执行更多操作
  • 使用
    预处理的_file.c
    继续编译
  • 如果您尝试编译
    预处理的_file.c
    ,就像编译普通的c(步骤3)一样,您会得到以下许多结果:

    /usr/include/stdio.h:257: error: redefinition of parameter ‘restrict’
    /usr/include/stdio.h:257: error: previous definition of ‘restrict’ was here
    /usr/include/stdio.h:258: error: conflicting types for ‘restrict’
    /usr/include/stdio.h:258: error: previous definition of ‘restrict’ was here
    /usr/include/stdio.h:260: error: conflicting types for ‘restrict’
    [...]
    
    这只是在
    文件.c
    中使用
    #include
    。幸运的是,通过指定编译为
    C-cpp-output
    (请参见第页的
    -x
    )的语言,可以告诉GCC它正在对已经预处理过的C代码执行操作。但它不起作用。我只知道:

    $ gcc -x c-cpp-output -std=c99 bar.c
    i686-apple-darwin9-gcc-4.0.1: language c-cpp-output not recognized
    i686-apple-darwin9-gcc-4.0.1: language c-cpp-output not recognized
    ld warning: in bar.c, file is not of required architecture
    Undefined symbols:
      "_main", referenced from:
          start in crt1.10.5.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    
    与更新版本的GCC的响应完全相同:

    $ gcc-mp-4.4 -x c-cpp-output -std=c99 bar.c
    [same error stuff comes here]
    

    预处理后,使用
    .i
    后缀保存文件。Gcc手册页:

    file.i C source code which should not be preprocessed. file.ii C++ source code which should not be preprocessed. 档案,我 不应预处理的C源代码。 档案ii C++源代码,不应进行预处理。
    关于
    restrict
    的警告是因为它是C99中的一个关键字。因此,您必须使用相同的标准预处理和编译代码

    关于
    \u main
    的错误是因为您的文件没有定义
    main()
    ?执行以下操作应该有效:

    gcc -c -std=c99 bar.c
    
    它将创建
    bar.o
    。如果您的
    bar.c
    中定义了
    main()
    ,那么它可能不是
    bar.c
    ?例如,我使用有效的
    main()
    创建了一个
    bar.c
    ,并执行了以下操作:

    gcc -E -std=c99 bar.c >bar.E
    gcc -std=c99 bar.E
    
    得到:

    Undefined symbols:
      "_main", referenced from:
          start in crt1.10.6.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    
    在这种情况下,您需要
    -xc
    选项:

    gcc -x c -std=c99 bar.E
    

    (或者,正如Nikolai提到的,您需要将预处理的文件保存到
    bar.i

    看起来像是GCC文档中的打字错误-请尝试“-x cpp output”

    gcc -E helloworld.c > cppout
    gcc -x cpp-output cppout -o hw
    ./hw
    Hello, world!
    

    $gcc-std=c99 bar.i,包含在bar.h:4中的文件中,from bar.cex:4:/usr/include/stdio.h:327:error:syntax error before'-'令牌/usr/include/stdio.h:335:error:syntax error before'.\u stdoutp'/usr/include/stdio.h:383:error:'.\u sputc'的静态声明跟在非静态声明/usr/include/stdio之后。h:334:error:previous:here@Ollie:您如何预处理这些文件?你使用的命令是什么?这在hello world中有效,但在我的代码中无效。让我看看发生了什么。也许可以试试“gcc-xcpp output-std=c99 xyz.cppoutput”?visual studio中有类似的选项吗?