使用 #include "filename" 时编译器错误
compiler error when using #include "filename"
我读了
#include filename
将导致该指令被由"分隔符"之间的指定序列标识的源文件的整个内容所替换。
下面是一个简短的例子。用g++ main.cpp f.h f.cpp
编译会产生编译错误。程序运行正确后,我删除#include
在f.cpp
。我有两个问题。
-
原程序出错的原因是什么?
-
#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.
更新:
我发现g++ main.cpp f.cpp f.h
失败,因为在我的目录中有一个文件f.h.gch
。在我清理.gch
文件后,程序正确编译。
(很抱歉我之前没有提供这个信息,因为我刚刚注意到它。)
但是,g++ main.cpp f.cpp f.h
生成f.h.gch
。如果连续编译两次,就会出现错误。
根据东辉的建议,从g++
中删除f.h
也可以正确编译。并且,它不会生成f.h.gch
文件。
我的问题可能是什么是一个重复。h。我们为了文件?
在您的g++命令中,删除f.h.
相关文章:
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 重载方法的方式会在使用临时调用时生成编译器错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 编译器错误:destuctor 的更宽松的抛出说明符
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- C++ G++ 编译器 - 错误:隐式声明的定义
- 这个失败的测试是将零添加到空指针未定义的行为、编译器错误还是其他什么?
- 模板模板参数和模板别名:编译器错误?
- C++ 编译器错误:P1LinkedList.cpp:145:错误:重载的"to_string(int&)"调用不明确
- 在gcc中意外调用了Const重载.编译器错误或兼容性修复程序
- C1001内部编译器错误是由于矢量初始化(如数组)引起的
- 编译器错误(英特尔并行工作室 2019 与 Visual Studio 社区 2019)
- 如何在C++中克服此 C4430 编译器错误?
- 尝试使用 WinBGI 显示文本时出现编译器错误
- 使用typedef'ed返回类型声明友元函数时出现编译器错误
- 如何在类模板的成员函数中正确调用函数对象?正在生成 Visual Studio 编译器错误 C2440
- C++自定义分配器大小参数作为模板参数会引发编译器错误