我把函数原型声明在a.h中,实现在a.c中,但是在b.c中引用a.h中的函数时为什么说没有定义啊?
在b.c中我#include "a.h" ,但编译器提示有undefined reference
为什么啊?
问题点数:20、回复次数:21Top
1 楼OpenHero(开勇)回复于 2005-08-01 09:08:13 得分 0
具体代码~~~~:)?
具体错误~~~
这样看,应该没有问题Top
2 楼jixingzhong(瞌睡虫·星辰)回复于 2005-08-01 09:17:56 得分 0
#include "a.h"
别忘了
#include "a.c"Top
3 楼Microsnow(冰火)回复于 2005-08-01 09:24:20 得分 0
楼上,用得着这样吗?Top
4 楼zhoufanking(风铃)回复于 2005-08-01 09:25:13 得分 0
在b.c中也要include a.c吗? 但是比如使用stdio.h中声明的函数时也并没有include stdio.c啊!这是怎么做的?Top
5 楼hjf1010(黑色狂人)回复于 2005-08-01 09:30:50 得分 0
你的a.h在什么地方??把它和b.c放在一起试试Top
6 楼megaboy(飞天御剑流之杀神一刀斩)回复于 2005-08-01 10:36:24 得分 0
a.c和b.c要一起编译,光include个头文件有什么用啊,实现都在a.c里。Top
7 楼liao2001(知之为知之,不知为不知。。。)回复于 2005-08-01 11:43:40 得分 0
cc -o myexe a.c b.c
or
gcc -o muexe a.c b.cTop
8 楼yeehya(老汉了)回复于 2005-08-01 15:57:27 得分 0
在b.c中有#include "a.h"
可以这样编译:
gcc -c a.c
gcc -c b.c
gcc -o myexe a.o b.o
通过出错信息可以看到点原因.Top
9 楼hekaidong(比目鱼@429)回复于 2005-08-01 17:33:45 得分 0
你的a.h和b.c肯定不是在同一目录下的Top
10 楼ningzhiyu(凝滞雨)回复于 2005-08-02 09:00:04 得分 0
liao2001(知之为知之,不知为不知。。。) ( ) 信誉:100
yeehya(老汉了) ( ) 信誉:100
的都是正确的。
楼主是什么编译环境?如果是用gcc/g++的话,照上面的做就是了。
如果是用其他IDE的话,要把所有文件添加到同一个project中,例如dev-cpp,VC等
undefined reference
错误一般是在连接的时候没有找到实现的代码。
如果是没有include声明的话,一般是语法错误的,undeclared .......什么的Top
11 楼Darkay_Lee()回复于 2005-08-02 09:05:05 得分 0
要和a.c一起参与连接才行。否则只有a.h里面的声明(declare),没有a.c里面的实现(定义define)的话连接就会出错误。
Top
12 楼xuanwenchao(xuanwenchao)回复于 2005-08-02 09:43:03 得分 0
你把它们放在一个工程里同一目录下就没问题了Top
13 楼zhoufanking(风铃)回复于 2005-08-04 07:42:42 得分 0
我用的是devcpp,后来建了一个工程就ok了。谢谢各位。如果我想做到像c标准库那样用户只可以看到头文件,使用时只include一下就行的话,要怎么做呢?Top
14 楼MiloChin(游荡者)回复于 2005-08-04 11:50:51 得分 0
I think you didn't declare the prototype of the function which you defined in a.h file, so even though you included a.h file in b.c file, the compiler still thinks that you haven't declared the prototype of the function which you invoke in b.c file.
Solution: first, you have to define the function in a.c file, then declare the prototype of that function in a.h file, last, include a.h file at the beginning in b.c file. After done the above, I think you can invoke the function as will without undefined reference.Top
15 楼zhoufanking(风铃)回复于 2005-08-07 23:10:30 得分 0
to MiloChin:
正如你所说,我的函数定义在a.c,然后在a.h中我声明了每个函数的原型;在b.c的开头第一句就include "a.h";但是这样做的话问题依旧存在。只有建立一个工程,把这三个文件都加进去才ok。Top
16 楼haozi112(foring)回复于 2005-08-08 05:19:38 得分 0
没有把2个文件link起来,我也遇到过这样的情况
Top
17 楼zhoufanking(风铃)回复于 2005-08-14 17:00:16 得分 0
怎么link啊??Top
18 楼caocheng8230(学C++而不知疲倦)回复于 2005-08-14 21:34:14 得分 0
你的问题我也遇到过,可能你没有引进#include"stdafx.h"的原故Top
19 楼bsnhk((void *)0)回复于 2005-08-15 03:46:01 得分 0
还需要一个工程文件(.prj文件)
工程文件内容如下:
b.c(a.h)
当然你还需要在编译器中进行一些设置,比如指定工程文件名字及路径,还需要指定冤源文件的目录.Top
20 楼YFY(天易)回复于 2005-08-15 12:42:29 得分 0
在本身的.h中加入 extern 函数申明Top
21 楼rurality(rurality)回复于 2006-06-20 09:05:58 得分 0
在你的连接选项上,加入a.c编译后的库文件就可以了
VC是在项目->设定->链接里面
gcc加上 -I选项Top




