主题:问个多文件编译的简单问题
//test.h
#ifndef _TEST_H_
#define _TEST_H_
int a;
int b;
int c;
#endif
//test.cpp
#include "test.h"
int face1(int c,int d);
int face2(int c,int d);
int face3(int c,int d);
void main()
{
a=1;
b=2;
c=face1(a,b);
}
//face1.cpp
#include "test.h"
#include "face2.cpp"
#include "face3.cpp"
int face1(int c,int d)
{
return face2(c,d)+face3(c,d);
}
//face2.cpp
#include "test.h"
int face2(int c,int d)
{
return c+d;
}
//face3.cpp
#include "test.h"
int face3(int c,int d)
{
return c*d;
}
=======================================
出现如下编译错误:
错误 1 error LNK2005: "int a" (?a@@3HA) 已经在 test.obj 中定义 face1.obj test1
错误 2 error LNK2005: "int c" (?c@@3HA) 已经在 test.obj 中定义 face1.obj test1
错误 3 error LNK2005: "int b" (?b@@3HA) 已经在 test.obj 中定义 face1.obj test1
错误 4 error LNK2005: "int __cdecl face2(int,int)" (?face2@@YAHHH@Z) 已经在 face1.obj 中定义 face2.obj test1
错误 5 error LNK2005: "int a" (?a@@3HA) 已经在 test.obj 中定义 face2.obj test1
错误 6 error LNK2005: "int c" (?c@@3HA) 已经在 test.obj 中定义 face2.obj test1
错误 7 error LNK2005: "int b" (?b@@3HA) 已经在 test.obj 中定义 face2.obj test1
错误 8 error LNK2005: "int __cdecl face3(int,int)" (?face3@@YAHHH@Z) 已经在 face1.obj 中定义 face3.obj test1
错误 9 error LNK2005: "int a" (?a@@3HA) 已经在 test.obj 中定义 face3.obj test1
错误 10 error LNK2005: "int c" (?c@@3HA) 已经在 test.obj 中定义 face3.obj test1
错误 11 error LNK2005: "int b" (?b@@3HA) 已经在 test.obj 中定义 face3.obj test1
错误 12 fatal error LNK1169: 找到一个或多个多重定义的符号 E:\123\test1\Debug\test1.exe test1
#ifndef _TEST_H_
#define _TEST_H_
int a;
int b;
int c;
#endif
//test.cpp
#include "test.h"
int face1(int c,int d);
int face2(int c,int d);
int face3(int c,int d);
void main()
{
a=1;
b=2;
c=face1(a,b);
}
//face1.cpp
#include "test.h"
#include "face2.cpp"
#include "face3.cpp"
int face1(int c,int d)
{
return face2(c,d)+face3(c,d);
}
//face2.cpp
#include "test.h"
int face2(int c,int d)
{
return c+d;
}
//face3.cpp
#include "test.h"
int face3(int c,int d)
{
return c*d;
}
=======================================
出现如下编译错误:
错误 1 error LNK2005: "int a" (?a@@3HA) 已经在 test.obj 中定义 face1.obj test1
错误 2 error LNK2005: "int c" (?c@@3HA) 已经在 test.obj 中定义 face1.obj test1
错误 3 error LNK2005: "int b" (?b@@3HA) 已经在 test.obj 中定义 face1.obj test1
错误 4 error LNK2005: "int __cdecl face2(int,int)" (?face2@@YAHHH@Z) 已经在 face1.obj 中定义 face2.obj test1
错误 5 error LNK2005: "int a" (?a@@3HA) 已经在 test.obj 中定义 face2.obj test1
错误 6 error LNK2005: "int c" (?c@@3HA) 已经在 test.obj 中定义 face2.obj test1
错误 7 error LNK2005: "int b" (?b@@3HA) 已经在 test.obj 中定义 face2.obj test1
错误 8 error LNK2005: "int __cdecl face3(int,int)" (?face3@@YAHHH@Z) 已经在 face1.obj 中定义 face3.obj test1
错误 9 error LNK2005: "int a" (?a@@3HA) 已经在 test.obj 中定义 face3.obj test1
错误 10 error LNK2005: "int c" (?c@@3HA) 已经在 test.obj 中定义 face3.obj test1
错误 11 error LNK2005: "int b" (?b@@3HA) 已经在 test.obj 中定义 face3.obj test1
错误 12 fatal error LNK1169: 找到一个或多个多重定义的符号 E:\123\test1\Debug\test1.exe test1