Chinaunix

标题: 问个编译错误的问题,谢谢 [打印本页]

作者: xiaozhu2007    时间: 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",编译出现这种问题一般是哪的问题???
谢谢!
作者: cugb_cat    时间: 2008-04-29 00:12
这肯定是重复定义啊,在头文件中extern
作者: xiaozhu2007    时间: 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成功。
麻烦给讲解下,谢谢!
作者: xiaozhu2007    时间: 2008-04-29 00:31
标题: 回复 #2 cugb_cat 的帖子
程序中出现的问题和上述测试代码相似,怎么解决?
作者: Fixend    时间: 2008-04-29 00:38
head.h改为:
extern int              a;

1.c添加:
int a = 3;
其他不变
作者: scutan    时间: 2008-04-29 00:48
其实#include"head.h"的意思就是将整个head.h文件都包含到了.c文件中去,因此,此时的head.h文件实际上在两个.c文件中都存在了。那么你再将两个.o文件链接的话就会报重复定义的错误。
作者: kimi.cai    时间: 2008-04-29 10:43
重复定义,头文件不要定义变量
作者: xiaozhu2007    时间: 2008-04-30 22:50
标题: 回复 #6 scutan 的帖子
为什么把head.h里的int a = 3;改成int a就没事了呢?




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2