2010-01-25 69 views
1

我在C預處理和C編譯之間執行一些源處理。目前我:如何編譯已經用C預處理的C代碼?

  1. gcc -E file.c > preprocessed_file.c
  2. 做更多的東西preprocessed_file.c
  3. 繼續與preprocessed_file.c編譯。

如果你試圖編譯preprocessed_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’ 
[...] 

而這只是在file.c使用#include <stdio.h>。幸運的是有一個選項告訴GCC它作用於C代碼已經過預處理通過指定被編譯爲c-cpp-output語言(見this-x)。但它不起作用。我剛剛得到這個:

$ 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] 

回答

3

看起來像GCC文檔中的拼寫錯誤 - 請嘗試'-x cpp-output'。

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

那麼,這與一個你好的世界,但與我的代碼沒有。讓我看看發生了什麼。 – 2010-01-25 15:00:33

+0

也許試試'gcc -x cpp-output -std = c99 xyz.cppoutput'? – leegent 2010-01-25 15:05:48

+0

在visual studio中是否有類似的選項? – Naveen 2015-11-22 00:00:47

1

保存文件前處理後的.i後綴。 GCC手冊頁:

 
     file.i 
      C source code which should not be preprocessed. 

     file.ii 
      C++ source code which should not be preprocessed. 

+0

$ GCC -std = C99 bar.i 在文件中包含從bar.h:4, 從bar.cex:4: /usr/include/stdio.h:327:錯誤:語法' - '令牌之前的錯誤 /usr/include/stdio。h:335:錯誤:'__stdoutp'之前的語法錯誤 /usr/include/stdio.h:383:錯誤:'__sputc'的靜態聲明出現在非靜態聲明之後 /usr/include/stdio.h:334:error :先前聲明'__sputc'在這裏 – 2010-01-25 14:53:00

+0

@Ollie:你如何預處理文件?你用什麼命令? – 2010-01-25 14:56:14

4

restrict的警告是由於它是在C99中的關鍵字。所以,你必須使用相同的標準預處理和編譯你的代碼。

_main這個錯誤是因爲你的文件沒有定義main()?做下面的工作應該是:

gcc -c -std=c99 bar.c 

它會創建bar.o。如果您bar.c在它定義一個main(),也許它不叫bar.c?例如,我創建了一個bar.c具有有效main(),並且做:

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 

在這種情況下,你需要的-x c選項:

gcc -x c -std=c99 bar.E 

(或者,像尼古拉提到的,你需要預先處理過的文件保存到bar.i。)