linux-c++-makefile的使用方法

本文详细介绍了使用C++编程语言创建Hello World程序的过程,包括定义类、实现构造函数和析构函数,并通过makefile进行编译。最终在Linux环境下成功运行程序并展示输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 c++编程中一般把文件分为三种,xx.h、xx.cpp、 main.cpp;linux环境下我们可以使用make工具进行编译,生成可执行文件之后便可以使用了。这里写一个helloworld,分享      一下.

hello.h文件内容如下:

#ifndef hell_h
#define hell_h
class HelloWorld()
{
public :
HelloWorld();
~HelloWorld();
};
#endif

helloworld.cpp文件内容如下:

#include<iostream>
#include "hello.h"
using namespace std;
HelloWorld::HelloWorld()
{
cout<<"HelloWorld constr"
}
HelloWorld::~HelloWorld()
{
cout<<"析构"<<endl;
}

main.cpp文件内容如下:

#include<iostream>
#include "hello.h"
int main()
{
HelloWorld h;
return 0;
}

下面是 makefile文件的内容,这也是最重要的。

dest:helloworld.o main.o
g++ -c helloworld.o main.o -o dest
helloworld.o helloworld.cpp hello.h
g++ -c helloworld.cpp -o helloworld.o
main.o:main.cpp hello.h
g++ -c main.cpp main.o

然后在linux终端输入命令make makefile这可以得到dest文件;

然后执行./dest显示打印两行数据,分别来自HelloWorld类的构造函数和析构函数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值