请问如何在一个cpp文件中调用另外一个cpp文件?

xiaojunsyiae 2002-11-15 10:14:21
两个同在一个project中的cpp文件,定义如下:
#ifndef CPP1_CPP_//cpp1.cpp
#define CPP1_CPP_
#include<iostream>
using namespace std;
extern void test()
{
cout<<"this is the cpp1.print"<<endl;
}
#endif

#include<iostream>//cpp.cpp引用cpp1.cpp中的test函数。
using namespace std;
#include<stdlib.h>
#include<stdio.h>
#include "cpp1.cpp"
//#include"test.h"

void main(void)
{
test();
system("pause");
}
报出连接错误如下:
cpp1.obj : error LNK2005: "void __cdecl test(void)" (?test@@YAXXZ) already defined in cpp.obj
Debug/cpp.exe : fatal error LNK1169: one or more multiply defined symbols found
请教如何解决?
...全文
352 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
asvaboy1980 2002-11-15
  • 打赏
  • 举报
回复
天啦,这个问题real简单
luohualiushui 2002-11-15
  • 打赏
  • 举报
回复
这次对了
唉~~,真是没想到!!
sterrys 2002-11-15
  • 打赏
  • 举报
回复
cpp1.cpp中不需要

#include "cpp1.cpp"
luohualiushui 2002-11-15
  • 打赏
  • 举报
回复
还是有错啊
wanglei888 2002-11-15
  • 打赏
  • 举报
回复
#ifndef CPP1_CPP_//cpp1.cpp
#define CPP1_CPP_
#include<iostream>
using namespace std;
extern void test() //~~~~~~~~ 注意,此处不能使用extern,因为函数在这里实现
{
cout<<"this is the cpp1.print"<<endl;
}
#endif

#include<iostream>//cpp.cpp引用cpp1.cpp中的test函数。
using namespace std;
#include<stdlib.h>
#include<stdio.h>
#include "cpp1.cpp"
//#include"test.h"

extern void test(); // 这里用extern 表示test函数在别的地方实现

void main(void)
{
test();
system("pause");
}

69,371

社区成员

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

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