某个C文件又添加一个新的头文件,rebuild这个工程,结果此C文件报错。

src/xxxx.c: In function ‘foo’:
src/xxxx.c:27:1: error: parameter ‘some_pp’ is initialized
src/xxxx.c:27:1: warning: initialization from incompatible pointer type [enabled by default]

line 27是一个文件内全局变量(some_pp)的声明,检查无问题。function foo 是本文件需要用到的,头文件中引用的一个 而在line 27前面的内容,全是宏定义或者头文件声明,错都没法错。 更重要的是,这个文件已经编译过很多遍了,前面基本可以确定没啥问题。唯一的改变是添加了一个头文件(headername.h) 检查头文件发现如下代码

#ifndef __HEADERMANE_H__
#define __HEADERMANE_H__
// bla bla ...
int foo(int offset,const char *str)
#endif // __HEADERMANE_H__

foo是一个函数的声明,后面忘了加上分号了。 headername.h line 4末尾加上';'即可解决问题。

gcc以为xxxx.c中后文内容都是函数foo内的东西,然后发现不匹配,才报错挂了。

这件事提醒我们,编译器是有在实在编不下去了才报错,而它提示的位置,往往是在真正错误的位置的后面,我们需要从提示的位置开始,往前面寻找错误的真正来源。可能往前要追溯好多行,乃至超过1个文件。

Categories: Code

Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

3 Comments

花舞花落泪 · May 6, 2013 at 09:22

Google Chrome 26.0.1410.64 Google Chrome 26.0.1410.64 Windows 7 Windows 7

不错….有时一句代码错误会引起多个错误…

Leniy · April 23, 2013 at 07:51

Google Chrome 26.0.1410.64 Google Chrome 26.0.1410.64 Windows 7 Windows 7

是啊

    yu · April 23, 2013 at 12:45

    Google Chrome 26.0.1410.63 Google Chrome 26.0.1410.63 GNU/Linux x64 GNU/Linux x64

    悲剧–GFW貌似抽风。。网站貌似被K

Leave a Reply to yu Cancel reply

Your email address will not be published. Required fields are marked *