程序由两个文件构成,如何用VC编译、连接?

zcsd 2003-08-23 07:50:24
//file1:p29.cpp
#include<iostream>
#include"d:\c++\p30.cpp"
using namespace std;
//---------------------------------------------------------
int Am,An;
static int ASx,ASy;
//---------------------------------------------------------
void Func1();
void Func2();
void Func3();
//---------------------------------------------------------
int main()
{
cout<<"执行过Func1()\n";
Func1();
cout<<"Am的值是:"<<Am<<endl;
cout<<"An的值是:"<<An<<endl;
cout<<endl;
cout<<"执行过Func2()\n";
Func2();
cout<<"Am的值是:"<<Am<<endl;
cout<<"An的值是:"<<An<<endl;
cout<<"\n执行过Func3()\n";
Func3();
return 0;
}

extern int Bp;
//---------------------------------------------------------
void Func3()
{
cout<<"Bp的值是:"<<Bp<<endl;
cout<<endl;
}

//---------------------------------------------------------
//---------------------------------------------------------
//file2:p30.cpp
int Bp;
extern int Am,An;
//---------------------------------------------------------
void Func1()
{
Bp=2;
Am=8;
}
//---------------------------------------------------------
void Func2()
{
Am+=10;
An=27;
}

在VC下link时有4个错误LNK2005。
"void __cdecl Func1(void)" (?Func1@@YAXXZ) already defined in p29.obj;
"void __cdecl Func2(void)" (?Func2@@YAXXZ) already defined in p29.obj;
"int Bp" (?Bp@@3HA) already defined in p29.obj;
one or more multiply defined symbols found;

我自己分析:
p29.cpp中的void func1(); void func2();只是原型声明,具体定义在p30.cpp中。
同样,p29.cpp中的extern int Bp只是声明Bp在另一文件p30.cpp中有定义。
但是,VC编译器不这样理解,没办法。
要不就是我的联调方法不对,如#include"d:\c++\p30.cpp",不对。

请高手指点迷津!诚谢!


...全文
105 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zcsd 2003-08-24
  • 打赏
  • 举报
回复
一语中的!感谢!
晨星 2003-08-23
  • 打赏
  • 举报
回复
呵呵,你需要的只是原型,而不是定义,你把cpp文件include了,那还不造成函数的重复定义吗?
要知道include其实就相当于把那个文家你的内容拷贝过来,这就是说,在你的好几个文件中都有那个函数的定义,所以link出错了。
这就是为什么不提倡包含cpp文件的原因。正确的做法是h文件中仅包含声明或宏定义、类型定义。cpp文件中包含实体的定义(如函数、全局变量的定义)。然后需要时只包含h文件。
zcsd 2003-08-23
  • 打赏
  • 举报
回复
谢谢!
但这样不行,仍有link error

file1中的main()中用到了func1();和func2();
故我觉得在file1中声明func1与func2的原型是必要的。
winco 2003-08-23
  • 打赏
  • 举报
回复
//file1:p29.cpp
#include<iostream>
#include"p30.cpp"
using namespace std;

//---------------------------------------------------------
//int Am,An;
static int ASx,ASy;
//---------------------------------------------------------
//void Func1();
//void Func2();
void Func3();
//---------------------------------------------------------
int main()
{
cout<<"执行过Func1()\n";
Func1();
cout<<"Am的值是:"<<Am<<endl;
cout<<"An的值是:"<<An<<endl;
cout<<endl;
cout<<"执行过Func2()\n";
Func2();
cout<<"Am的值是:"<<Am<<endl;
cout<<"An的值是:"<<An<<endl;
cout<<"\n执行过Func3()\n";
Func3();
return 0;
}

extern int Bp;
//---------------------------------------------------------
void Func3()
{
cout<<"Bp的值是:"<<Bp<<endl;
cout<<endl;
}


//---------------------------------------------------------
//---------------------------------------------------------
//file2:p30.cpp
int Bp;
int Am,An;
//---------------------------------------------------------
void Func1()
{
Bp=2;
Am=8;
}
//---------------------------------------------------------
void Func2()
{
Am+=10;
An=27;
}
zcsd 2003-08-23
  • 打赏
  • 举报
回复
steedhorse(晨星),谢谢你!
我试了,成功!
有个疑问,
在一个工程项目中,我已编辑了多个文件(相互之间不一定有关联),需要调试时,我就将相应的那个文件加入到source files中,这样用起来挺方便。(注:我是在参照textbook学习,为避免每调试一个程序就建立一个工程,故如此)。刚才,我仅将相关联的两个文件加入进来,F7,出现link错误。
但照你所说,新建一个项目,其中仅有这两个文件,并都已加入到source files中,编译、连接成功!
能解释一下吗?完了,就结帐。
zcsd 2003-08-23
  • 打赏
  • 举报
回复
难怪我找不到了呢,原来是你------------

新手嘛,误打误撞,在所难免。
晨星 2003-08-23
  • 打赏
  • 举报
回复
这个问题怎么放到“标准库”子版了?帮你转一下吧。
晨星 2003-08-23
  • 打赏
  • 举报
回复
新建一个工程,然后把相关的源文件都加入这个工程,然后直接Build。

69,382

社区成员

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

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