怎么解决"multiple definition of"错误

liubo1977 2005-12-16 12:23:08
我有三个文件:
文件件1,名为test.c:

#include<stdio.h>
#include"mytest.h"
int main()
{
ct_new_0[1][2]=9;
ct_new_1[2][2]=10;
}

文件2,名为:test1.c

#include"mytest.h"
void f()
{
greyTable[1][1]=12;
}


文件3,是头文件,名为:mytest.h:
#ifndef _MYTEST_H_
#define _MYTEST_H_
short ct_new_0[1024][3]=
{
{255, 255, 255} //B??
};

unsigned char ct_new_1[1024][3]=
{
//????1
{0,0,255}
};
unsigned char ct_new_2[1024][3]=
{
{255,255,255}
};

unsigned char greyTable[256][3]=
{
{252, 252, 252}
};
#endif
在gcc下面编译如下:
gcc -o test test1.c test.c
报错为:
/tmp/ccHm7uiD.o(.data+0x0): multiple definition of `ct_new_0'
/tmp/ccwMLEMM.o(.data+0x0): first defined here
/tmp/ccHm7uiD.o(.data+0x1800): multiple definition of `ct_new_1'
/tmp/ccwMLEMM.o(.data+0x1800): first defined here
/tmp/ccHm7uiD.o(.data+0x2400): multiple definition of `ct_new_2'
/tmp/ccwMLEMM.o(.data+0x2400): first defined here
/tmp/ccHm7uiD.o(.data+0x3000): multiple definition of `greyTable'
/tmp/ccwMLEMM.o(.data+0x3000): first defined here
请问这是怎么回事????
...全文
7285 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
handsomerun 2005-12-16
  • 打赏
  • 举报
回复
这个当然重复定义了啊

你在mytest.h已经定义了

然后再到test.c:

#include<stdio.h>
#include"mytest.h"
int main()
{
ct_new_0[1][2]=9;
ct_new_1[2][2]=10;
}
中又定义了,所以重复定义了啊
变量名重复了,都是ct_new_0什么的
jixingzhong 2005-12-16
  • 打赏
  • 举报
回复
把变量外部声明一下好了 ~

在 C 文件中定义,头文件中 EXTERN 声明 ~
goodluckyxl 2005-12-16
  • 打赏
  • 举报
回复
不要在头文件中定义变量
去.c中定义
然后其他要用的文件把变量extern 进来
liubo1977 2005-12-16
  • 打赏
  • 举报
回复
handsomerun(毛毛) :在test.c中并没有用到ct_new_2,但是仍然在报multiple definition of 错误?望指教。
zhNKUjw 2005-12-16
  • 打赏
  • 举报
回复
嗯,的确是这样,不过有什么比较好的解决方法呢?

69,382

社区成员

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

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