回 帖 发 新 帖 刷新版面

主题:问个多文件编译的简单问题

//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

回复列表 (共9个回复)

沙发

#include "test.h"
这一行的意思是“把test.h的所有内容拷贝到这里”。
于是,test.cpp、face1.cpp、face2.cpp、face3.cpp,这些文件里面都有如下代码:
int a;
int b;
int c;
于是全局变量重复定义,最终导致连接失败。

修改方法:
把test.h中的int a;修改为extern int a;。然后在任意一个cpp文件(例如:test.cpp)里面写int a;

板凳

如果定义全部加static也行的吧

3 楼


是这样吗?
这样??还是不行哎

//test.h
#ifndef _TEST_H_
#define _TEST_H_
void face1(int c,int d);
void face2(int c,int d);
void face3(int c,int d);

extern int a;
extern int b;
extern int e;
extern int f;
#endif
--------------
//test.cpp
#include <iostream>
#include "test.h"
using namespace std;

void main()
{
    int a=1;
    int b=2;
    int e=0;
    int f=0;
    
    face1(a,b);
    cout<<"e="<<e<<"\n";
    cout<<"f="<<f<<"\n";
}
-------------------
//face1.cpp
#include "test.h"
void face1(int c,int d)
{
    
     face2(c,d);
     face3(c,d);
}
-------------------------
//face2.cpp
#include "test.h"
void face2(int c,int d)
{
    int e;

     e= c+d;     
}
---------------------
//face3.cpp
#include "test.h"
void face3(int c,int d)
{
    int f;
    f=c*d;
}
还是不能改变全局变量

4 楼

1 楼怎么混到 20000 分的?

楼主的错误在于没声明就使用了 face1

根本不存在重复包含头文件的问题,因为 test.h 用 #ifndef ... #endif 保护起来了

3 楼的问题在于,全局变量应该在所有函数外面定义,函数内定义的变量是局部变量

应该这样:

//test.cpp
#include<iostream>
#include "test.h"
using namespace std;

int a;
int b;
int e;
int f;

int main()
{
    a=1;
    b=2;
    e=0;
    f=0;
    
    face1(a,b);
    cout<<"e="<<e<<"\n";
    cout<<"f="<<f<<"\n";
  
    return 0;

}


5 楼

请问,如果定义const变量,还需要extern吗?

6 楼


膜拜。。。





















------------------------------用户言论/签名并不代表本站观点
title="[url=http://www.seo03.com]上海网站优化[/url]"
title="[url=http://www.seo04.com]上海百度优化[/url]"

7 楼

[quote]1 楼怎么混到 20000 分的?

楼主的错误在于没声明就使用了 face1

根本不存在重复包含头文件的问题,因为 test.h 用 #ifndef ... #endif 保护起来了[/quote]
或许你没有看清楚。我并没有说“重复包含头文件”,我说的是“全局变量重复定义”。正因为这样的错误,连接时才有诸如“error LNK2005: "int a" (?a@@3HA) 已经在 test.obj 中定义”的错误。

To 楼主:
1、我在1楼说“然后在任意一个cpp文件(例如:test.cpp)里面写int a;”,可能说得不够清楚。但我本意并不是让楼主写到main函数里面,而是应该写在函数之外,就像tianyuan008在4楼那样的代码。

2、往往,#include "xx.cpp"这是一个不太正常的信号,一般我们不会把cpp文件拿去include。原因就像我在1楼说的,include一个文件,就相当于把这个文件的所有内容拷贝到此处。face1.cpp中include了face2.cpp,于是face1.cpp就有如下内容:
void face2(int c,int d)
{
    int e;
    e= c+d;
}
这样一来,face1.cpp和face2.cpp都各自定义了一个名叫face2的全局函数,并且参数也完全相同。这是不被允许的。错误“error LNK2005: "int __cdecl face2(int,int)" (?face2@@YAHHH@Z) 已经在 face1.obj 中定义    face2.obj”,就是说的这个。

8 楼

楼上正解

9 楼

七楼说的很好,建议你向七楼好好请教一下吧~~

我来回复

您尚未登录,请登录后再回复。点此登录或注册