2016-10-21 17 views
0

當我read編譯器錯誤使用的#​​include「文件名」

#include filename使由「分隔符之間的指定序列所標識的源文件的全部內容替換該指令的。

簡單例子如下。與g++ main.cpp f.h f.cpp編譯會產生編譯錯誤。該程序運行正常後,我刪除f.cpp#include。我有兩個問題。

  1. 原始程序中的錯誤原因是什麼?

  2. #include filename是否真的只是用引用文件的內容替換指令?

請問第二個問題,因爲該程序也可以正常運行,當我替換int f();問題的行(這是f.h的確切內容)。

謝謝。


f.h

int f(); 

f.cpp

#include "f.h" // problem 
int f() { return 1; } 

的main.cpp

#include <iostream> 
#include "f.h" 
int main() { 
    std::cout << f() << std::endl; 
} 

與原來的程序運行g++ main.cpp f.h f.cpp的錯誤消息是

f.cpp: In function 'int f()': 
f.cpp:2:5: internal compiler error: in ggc_record_overhead, at ggc-common.c:1013 
int f() { return 1; } 
    ^
0x8ae8eb ggc_record_overhead(unsigned long, unsigned long, void*, char const*, int, char const*) 
     /apps2/tools/gcc/4.9.2/gcc-4.9.2-src/gcc/ggc-common.c:1013 
0xc4af71 ggc_internal_alloc_stat(unsigned long, char const*, int, char const*) 
     /apps2/tools/gcc/4.9.2/gcc-4.9.2-src/gcc/ggc-page.c:1317 
0xa48462 get_combined_adhoc_loc(line_maps*, unsigned int, void*) 
     /apps2/tools/gcc/4.9.2/gcc-4.9.2-src/libcpp/line-map.c:136 
0xa9a5ff gimple_set_block(gimple_statement_base*, tree_node*) [clone .isra.8] 
     /apps2/tools/gcc/4.9.2/gcc-4.9.2-src/gcc/gimple.h:1489 
0xa9a5ff lower_stmt 
     /apps2/tools/gcc/4.9.2/gcc-4.9.2-src/gcc/gimple-low.c:239 
0xa9a5ff lower_sequence 
     /apps2/tools/gcc/4.9.2/gcc-4.9.2-src/gcc/gimple-low.c:206 
0xa9b481 lower_gimple_bind 
     /apps2/tools/gcc/4.9.2/gcc-4.9.2-src/gcc/gimple-low.c:415 
0xa9b5c6 lower_function_body 
     /apps2/tools/gcc/4.9.2/gcc-4.9.2-src/gcc/gimple-low.c:118 
0xa9b5c6 (anonymous namespace)::pass_lower_cf::execute() [clone .lto_priv.4979] 
     /apps2/tools/gcc/4.9.2/gcc-4.9.2-src/gcc/gimple-low.c:184 
Please submit a full bug report, 
with preprocessed source if appropriate. 
Please include the complete backtrace with any bug report. 
See <http://gcc.gnu.org/bugs.html> for instructions. 

UPDATE:

我發現g++ main.cpp f.cpp f.h失敗,因爲在我的目錄中的文件f.h.gch。該程序編譯後,我清理.gch文件。

(我很抱歉,我沒有按照我只注意到它之前此信息。)

然而,g++ main.cpp f.cpp f.h產生f.h.gch。所以如果我連續編譯兩次,這將是一個錯誤。

按照Donghui的建議,從g++刪除f.h也編譯正確。而且,它不生成f.h.gch文件。

我的問題是有可能的What is a .h.gch file?

+0

是的,#include filename用引用文件的內容替換指令。你顯示的代碼應該可以工作。其他的東西(你沒有顯示)導致了「問題」。請告訴我們更多。特別是,你是如何編譯的?錯誤信息是什麼? –

+1

「原始程序中出現錯誤的原因是什麼?」錯誤消息告訴你什麼? – juanchopanza

+0

@DonghuiZhang謝謝。我現在已經包含錯誤消息。它說「內部編譯器錯誤」,我不太明白。 – sam

回答

1

在你的G ++命令的副本,刪除F.H.

+0

這可能會解決編譯器錯誤,但代碼和命令行並沒有什麼本質上的錯誤。是的,編譯頭文件通常是一個錯誤,但沒有任何非法的。 –

+1

@東輝感謝您的回覆。在我的更新中,編譯器錯誤是由我的目錄中的'.gch'文件造成的。我現在把問題標記爲重複。 – sam