头文件的问题

Tulipsys 2004-12-19 10:38:01
我在头文件comm.h中定义了 extern int plus_one( int *num, int min, int max);

然后在comm.c中给出了具体的实现,可是在调用的时候有问题:
如果引用comm.c(#include "comm.c")那么一切正常
如果引用comm.h,就有如下错误:

[root@localhost workspace]# gcc pro01.c -o pro012
/tmp/ccCvmjum.o(.text+0xb1): In function `main':
: undefined reference to `plus_one'
/tmp/ccCvmjum.o(.text+0xcb): In function `main':
: undefined reference to `plus_one'
/tmp/ccCvmjum.o(.text+0xe2): In function `main':
: undefined reference to `plus_one'
collect2: ld returned 1 exit status
[root@localhost workspace]#

我已经在comm.c中包含了comm.h(#include "comm.h")
是哪儿的问题呢?
...全文
144 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
avalonBBS 2004-12-19
  • 打赏
  • 举报
回复
不明白,程序应该没错,怀疑你的gcc.
试写个
a.h
extern void foo(void);
a.c
void foo(void)
{
}
main.c
#include "a.h"
int main(void)
{
foo();
return 0;
}

看可能行。
Tulipsys 2004-12-19
  • 打赏
  • 举报
回复
GCC下还是这样:

[root@localhost workspace]# gcc pro01.c
/tmp/ccUgQXq0.o(.text+0xb1): In function `main':
: undefined reference to `plus_one'
/tmp/ccUgQXq0.o(.text+0xcb): In function `main':
: undefined reference to `plus_one'
/tmp/ccUgQXq0.o(.text+0xe2): In function `main':
: undefined reference to `plus_one'
collect2: ld returned 1 exit status
[root@localhost workspace]#
avalonBBS 2004-12-19
  • 打赏
  • 举报
回复
我在dev c++下编译的没事啊。

::-> 111 112 113 114 121 122 123 124 131 132 133 134 141 142 14
3 144 211 212 213 214 221 222 223 224 231 232 233 234 241 242 24
3 244 311 312 313 314 321 322 323 324 331 332 333 334 341 342 34
3 344 411 412 413 414 421 422 423 424 431 432 433 434 441 442 44
3 444 <-::
Tulipsys 2004-12-19
  • 打赏
  • 举报
回复
这个事情很怪,用VC第一次编译的时候出现同样的错误信息,再次编译就没事了!
然后再用GCC编译,还是那个样子
bigbee 2004-12-19
  • 打赏
  • 举报
回复
在.h中用extern来申明变量或函数是为了防止这个头文件被多个实现文件重复的包含导致的重复定义。在简单的程序中,在程序员还可以控制的情况下,可以不用在头文件里用extern来申明变量和函数。
bigbee 2004-12-19
  • 打赏
  • 举报
回复
在.h中
int plus_one( int *num, int min, int max);
就可以了
Tulipsys 2004-12-19
  • 打赏
  • 举报
回复
这是源代码:

comm.h
...............................................................
extern int plus_one( int *num, int min, int max);

comm.c

#include "comm.h"
...............................................................
int plus_one( int *num, int min, int max)
{
if( *num == max)
{
*num = min;
return( -1);
}
else
{
(*num)++;
return( 1);
}
}

pro01.c
...............................................................
#include "stdio.h"
#include "comm.h"

//extern int plus_one( int *num, int min, int max);

int main(void)
{
int n[4] = { 1, 2, 3, 4};
int pos[3] = { 0, 0, 0};
int i;
int num;

printf("\n::-> ");
num = n[pos[0]] * 100 + n[pos[1]] * 10 + n[pos[2]];
printf("%d ", num);
for( ;;)
{
if( plus_one( &pos[2], 0, 3) < 0)
if( plus_one( &pos[1], 0, 3) < 0)
plus_one( &pos[0], 0, 3);

num = n[pos[0]] * 100 + n[pos[1]] * 10 + n[pos[2]];
printf("%d ", num);
if( num == 444) break;
}
printf(" <-:: \n");

return(1);
}
pingfanhuan 2004-12-19
  • 打赏
  • 举报
回复
我发表一下观点:在.h中加extern好像没有道理。
avalonBBS 2004-12-19
  • 打赏
  • 举报
回复
comm.h中
extern int plus_one( int *num, int min, int max);
............................
/************************************************************/
comm.c中
int plus_one( int *num, int min, int max)
{
...............
}
...........................
/********************************************************/
#include "comm.h"
................
int main(void)
{
...........................
return 0;
}

////////////////////////楼主的是这样的结构吗?如果是最好能拿点源码出来看看
somedummy 2004-12-19
  • 打赏
  • 举报
回复
这个是当然的了,comm.h里面又没有plus_one这个函数的定义,当然不可能通过编译了

你必须指定lib文件,或者将这个comm.c文件同时编译才行
要不然就要分别编译(狭义的编译,也就是光产生目标文件,不链接),然后用链接程序进行链接
Tulipsys 2004-12-19
  • 打赏
  • 举报
回复
还是这样

/tmp/ccAlPsMf.o(.text+0x11): In function `main':
: undefined reference to `foo'

69,376

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧