Linux下如何编译这个程序?

pressman 2004-10-06 01:55:44
///////////////////////////////////////
//
// filename: Tdate.h
//
//////////////////////////////////////
class Tdate
{
public:
void Set(int,int,int);
int IsLeapYear();
void Print();
private:
int month;
int day;
int year;
};
///////////////////////////////////////
//
// filename: Tdate.cpp
//
//////////////////////////////////////
#include <iostream.h>
#include "Tdate.h"

void Tdate::Set(int m,int d,int y)
{
month=m;
day=d;
year=y;

}

int Tdate::IsLeapYear()
{
return((year%4==0&&year%100!=0)||(year%400==0));
}

void Tdate::Print()
{
cout<<year<<"-"<<month<<"-"<<day<<endl;
}
///////////////////////////////////////
//
// filename: Test.cpp
//
//////////////////////////////////////
#include <iostream.h>
#include "Tdate.h"

void SomeFunc(Tdate *ps)
{
ps->Print();
if(ps->IsLeapYear())
cout<<"A leap year.\n";
else cout<<"not aleap year.\n";
}

int main(void)
{
Tdate s;
s.Set(2,15,1998);

return 0;
}
...全文
175 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
pressman 2004-10-07
  • 打赏
  • 举报
回复
请各位帮帮忙,下午下班时结帖!
pressman 2004-10-07
  • 打赏
  • 举报
回复
再请问:
上面的代码如下编译出现了这样的错误信息!
重复定义?

root@kod:~/# g++ test.cpp -o test Tdate.cpp

/tmp/ccUFZ6F1.o: In function `main':
/tmp/ccUFZ6F1.o(.text+0x144): multiple definition of `main'
/tmp/ccv4A6do.o(.text+0x58): first defined here
/usr/bin/ld: Warning: size of symbol `main' changed from 33 to 14 in /tmp/ccUFZ6F1.o
collect2: ld returned 1 exit status
blankman 2004-10-07
  • 打赏
  • 举报
回复
g++ test.cpp Tdate.cpp -o test

如果要分别检查的话
g++ -c Tdate.cpp
g++ -c test.cpp
这样只编译不连接
gettext 2004-10-07
  • 打赏
  • 举报
回复
# g++ Test.cpp Tdate.cpp

# g++ ch11_2.cpp -o ch11_2 Tdate.cpp

pressman 2004-10-07
  • 打赏
  • 举报
回复
/tmp/ccQzyd6j.o: In function `main':
/tmp/ccQzyd6j.o(.text+0x144): multiple definition of `main'
/tmp/cc6hnGdT.o(.text+0x58): first defined here
/usr/bin/ld: Warning: size of symbol `main' changed from 33 to 14 in /tmp/ccQzyd6j.o
collect2: ld returned 1 exit status

TO jellen(类痴狂人) ( ) 信誉:100 :
还是不行,不过还是结帖吧,谁要我说了呢!
starstargao 2004-10-07
  • 打赏
  • 举报
回复
g++ -o test test.cpp Tdate.cpp
jellen 2004-10-07
  • 打赏
  • 举报
回复
把头文件里面的#include <iostream.h>改成:
#include <iostream>
using namespace std;

然后先编译TDate.cpp: g++ -c TDate.cpp生成TDate.o
再编译test.cpp: g++ -c test.cpp生成test.o
最后连接:g++ -o test test.o TDate.o生成test可执行文件.
jacksg 2004-10-07
  • 打赏
  • 举报
回复
g++ -c Tdate.cpp -o libTdate.o
ar rcs libTdate.a libTdate.o
cp libTdate.a /lib
g++ test.cpp -o test -lTdate
这应该是你想要的结果吧。
xmzh 2004-10-07
  • 打赏
  • 举报
回复
up
xmzh 2004-10-07
  • 打赏
  • 举报
回复
up!
winux0 2004-10-06
  • 打赏
  • 举报
回复
g++ -o test test.cpp Tdate.cpp
54sp 2004-10-06
  • 打赏
  • 举报
回复
Tdate都没编译这么link的上呢!
pressman 2004-10-06
  • 打赏
  • 举报
回复
#(OS DEBIAN 2.4.20)
上面三个文件放在同一目录下,我用g++ test.cpp -o test编译,出错(当然会错了!)
ERROR CODE:
root@kod:/# g++ ch11_2.cpp -o ch11_2
/tmp/ccbLPF1c.o: In function `SomeFunc(Tdate *)':
/tmp/ccbLPF1c.o(.text+0xe): undefined reference to `Tdate::Print(void)'
/tmp/ccbLPF1c.o(.text+0x1d): undefined reference to `Tdate::IsLeapYear(void)'
/tmp/ccbLPF1c.o: In function `main':
/tmp/ccbLPF1c.o(.text+0x6c): undefined reference to `Tdate::Set(int, int, int)'
collect2: ld returned 1 exit status

不知道要输入什么样的命令才行,是不是要用MAKEFILE?
在线!

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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