免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1798 | 回复: 7
打印 上一主题 下一主题

[C] 问个编译错误的问题,谢谢 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-04-29 00:06 |只看该作者 |倒序浏览

  1. cc    -c -o main.o main.c
  2. cc    -c -o client_routine.o client_routine.c
  3. cc    -c -o tty_server_request.o tty_server_request.c
  4. cc    -c -o tty_server_routine.o tty_server_routine.c
  5. gcc -o xiaozhu main.o client_routine.o tty_server_request.o tty_server_routine.o -lpthread
  6. client_routine.o:(.bss+0x0): multiple definition of `tty_server'
  7. main.o:(.bss+0x0): first defined here
  8. client_routine.o:(.bss+0x54): multiple definition of `client_mutex'
  9. main.o:(.bss+0x54): first defined here
  10. client_routine.o:(.bss+0x80): multiple definition of `clients_done'
  11. main.o:(.bss+0x80): first defined here
  12. tty_server_request.o:(.bss+0x0): multiple definition of `tty_server'
  13. main.o:(.bss+0x0): first defined here
  14. tty_server_request.o:(.bss+0x54): multiple definition of `client_mutex'
  15. main.o:(.bss+0x54): first defined here
  16. tty_server_request.o:(.bss+0x80): multiple definition of `clients_done'
  17. main.o:(.bss+0x80): first defined here
  18. tty_server_routine.o:(.bss+0x0): multiple definition of `tty_server'
  19. main.o:(.bss+0x0): first defined here
  20. tty_server_routine.o:(.bss+0x54): multiple definition of `client_mutex'
  21. main.o:(.bss+0x54): first defined here
  22. tty_server_routine.o:(.bss+0x80): multiple definition of `clients_done'
  23. main.o:(.bss+0x80): first defined here
  24. collect2: ld returned 1 exit status
  25. make: *** [xiaozhu] Error 1
复制代码

我只是在head.h里定义:
int                     client_threads;
pthread_mutex_t         client_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t          clients_done = PTHREAD_COND_INITIALIZER;
其它的.c文件#include "head.h",编译出现这种问题一般是哪的问题???
谢谢!

论坛徽章:
0
2 [报告]
发表于 2008-04-29 00:12 |只看该作者
这肯定是重复定义啊,在头文件中extern

论坛徽章:
0
3 [报告]
发表于 2008-04-29 00:29 |只看该作者
head.h:

  1. #ifndef __head_h
  2. #define __head_h

  3. #include <stdlib.h>
  4. #include <stdio.h>

  5. int              a = 3;
  6. void fun(void);

  7. #endif
复制代码


1.c:

  1. #include "head.h"

  2. int main(void)
  3. {
  4.         printf("in 1.c: a = %d\n", a);
  5.         fun();

  6.         exit(0);
  7. }
复制代码


2.c:

  1. #include "head.h"

  2. void fun(void)
  3. {
  4.         a++;
  5.         printf("in 2.c: a = %d\n", a);
  6. }
复制代码


编译:
[zuhf@localhost test]$ make
cc    -c -o 1.o 1.c
cc    -c -o 2.o 2.c
gcc -o xiaozhu 1.o 2.o
2.o.data+0x0): multiple definition of `a'
1.o.data+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [xiaozhu] Error 1
不明白的地方:
在head.h的那一int a的时候,如果定义的时候初始化的话,编译就会出现上述multiple definition of 'a'的错误,
如果不初始化的话就会make成功。
麻烦给讲解下,谢谢!

论坛徽章:
0
4 [报告]
发表于 2008-04-29 00:31 |只看该作者

回复 #2 cugb_cat 的帖子

程序中出现的问题和上述测试代码相似,怎么解决?

论坛徽章:
0
5 [报告]
发表于 2008-04-29 00:38 |只看该作者
head.h改为:
extern int              a;

1.c添加:
int a = 3;
其他不变

论坛徽章:
0
6 [报告]
发表于 2008-04-29 00:48 |只看该作者
其实#include"head.h"的意思就是将整个head.h文件都包含到了.c文件中去,因此,此时的head.h文件实际上在两个.c文件中都存在了。那么你再将两个.o文件链接的话就会报重复定义的错误。

论坛徽章:
0
7 [报告]
发表于 2008-04-29 10:43 |只看该作者
重复定义,头文件不要定义变量

论坛徽章:
0
8 [报告]
发表于 2008-04-30 22:50 |只看该作者

回复 #6 scutan 的帖子

为什么把head.h里的int a = 3;改成int a就没事了呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP