Chinaunix
标题:
问个编译错误的问题,谢谢
[打印本页]
作者:
xiaozhu2007
时间:
2008-04-29 00:06
标题:
问个编译错误的问题,谢谢
cc -c -o main.o main.c
cc -c -o client_routine.o client_routine.c
cc -c -o tty_server_request.o tty_server_request.c
cc -c -o tty_server_routine.o tty_server_routine.c
gcc -o xiaozhu main.o client_routine.o tty_server_request.o tty_server_routine.o -lpthread
client_routine.o:(.bss+0x0): multiple definition of `tty_server'
main.o:(.bss+0x0): first defined here
client_routine.o:(.bss+0x54): multiple definition of `client_mutex'
main.o:(.bss+0x54): first defined here
client_routine.o:(.bss+0x80): multiple definition of `clients_done'
main.o:(.bss+0x80): first defined here
tty_server_request.o:(.bss+0x0): multiple definition of `tty_server'
main.o:(.bss+0x0): first defined here
tty_server_request.o:(.bss+0x54): multiple definition of `client_mutex'
main.o:(.bss+0x54): first defined here
tty_server_request.o:(.bss+0x80): multiple definition of `clients_done'
main.o:(.bss+0x80): first defined here
tty_server_routine.o:(.bss+0x0): multiple definition of `tty_server'
main.o:(.bss+0x0): first defined here
tty_server_routine.o:(.bss+0x54): multiple definition of `client_mutex'
main.o:(.bss+0x54): first defined here
tty_server_routine.o:(.bss+0x80): multiple definition of `clients_done'
main.o:(.bss+0x80): first defined here
collect2: ld returned 1 exit status
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:
#ifndef __head_h
#define __head_h
#include <stdlib.h>
#include <stdio.h>
int a = 3;
void fun(void);
#endif
复制代码
1.c:
#include "head.h"
int main(void)
{
printf("in 1.c: a = %d\n", a);
fun();
exit(0);
}
复制代码
2.c:
#include "head.h"
void fun(void)
{
a++;
printf("in 2.c: a = %d\n", a);
}
复制代码
编译:
[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