自己写的一个头文件,名字叫“abcd.h”内容如下
#include<stdlib.h>
int a=0b=0;
int FuncA()
{
a=1;
b=1;
return 0;
}
如果我在CA.cpp和CB.cpp里分别引用“abcd.h“,就会出现错误,如下
1>CB.obj : error LNK2005: "int __cdecl FuncA(void)" (?FuncA@@YAHXZ) 已经在 CA.obj 中定义
1>CB.obj : error LNK2005: "int a" (?a@@3HA) 已经在 CA.obj 中定义
1>CB.obj : error LNK2005: "int b" (?b@@3HA) 已经在 CA.obj 中定义
1>D:\VCCODE\GetCodeSize\Debug\GetCodeSize.exe : fatal error LNK1169: 找到一个或多个多重定义的符号
如果我想在“abcd.h”头文件里保留原有内容,同时想消除在不同cpp文件里引用“abcd.h”出现的这种重定义的错误,应该怎么改呢?
#include<stdlib.h>
int a=0b=0;
int FuncA()
{
a=1;
b=1;
return 0;
}
如果我在CA.cpp和CB.cpp里分别引用“abcd.h“,就会出现错误,如下
1>CB.obj : error LNK2005: "int __cdecl FuncA(void)" (?FuncA@@YAHXZ) 已经在 CA.obj 中定义
1>CB.obj : error LNK2005: "int a" (?a@@3HA) 已经在 CA.obj 中定义
1>CB.obj : error LNK2005: "int b" (?b@@3HA) 已经在 CA.obj 中定义
1>D:\VCCODE\GetCodeSize\Debug\GetCodeSize.exe : fatal error LNK1169: 找到一个或多个多重定义的符号
如果我想在“abcd.h”头文件里保留原有内容,同时想消除在不同cpp文件里引用“abcd.h”出现的这种重定义的错误,应该怎么改呢?
#ifndef ABCD_H: #define ABCD_H: int a=0b=0;
int FuncA()
{
a=1;
b=1;
return 0;
}
#endif
1>CA.cpp
1>d:\vccode\getcodesize\abcd.h(5) : warning C4067: 预处理器指令后有意外标记 - 应输入换行符
1>d:\vccode\getcodesize\abcd.h(7) : error C2008: “:”: 宏定义中的意外
1>CB.cpp
1>d:\vccode\getcodesize\abcd.h(5) : warning C4067: 预处理器指令后有意外标记 - 应输入换行符
1>d:\vccode\getcodesize\abcd.h(7) : error C2008: “:”: 宏定义中的意外
指
#ifndef ABCD_H:
#define ABCD_H:
这两个地方