利用makefile文件来编译C++

这篇博客介绍了如何创建并使用Makefile文件来编译C++程序。通过在终端中编写Makefile,然后运行'./myfirst11',可以得到编译后的输出结果。参考了Linux环境下GCC和Makefile的实例。

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

</pre><pre name="code" class="cpp">  1 //file_NO1:hello.cpp
  2 #include<iostream>
  3 #include"hello.h"
  4 using namespace std;
  5 hello::hello()
  6 {
  7 }
  8 
  9 int hello::Display()
 10 {
 11     cout<<"Hello, World!\n"<<endl;
 12     return 0;
 13 }

  1 //file_NO2:hello.h
  2 #ifndef HELLO_H
  3 #define HELLO_H
  4 class hello{
  5     public:
  6         hello();
  7         int  Display();
  8 };
  9 #endif                   

  1 //file_NO3:myfirst.cpp
  2 #include <iostream>
  3 #include"hello.h"
  4 //using namespace std;
  5 
  6 int main()
  7 {
  8    hello theHello;
  9     theHello.Display();
 10    //cout<<"Hello"<<endl;
 11     return 0;
 12 }


在终端输入:vim makefile

makefile文件如下:

  1 myfirst11:myfirst.o hello.o
  2         g++ myfirst.o hello.o -o myfirst11
  3 hello.o:hello.cpp
  4         g++ -c hello.cpp -o hello.o
  5 myfirst.o:myfirst.cpp
  6         g++ -c myfirst.cpp -o myfirst.o
 

最后,再在终端输入:./myfirst11即可得到输出结果。


在ubuntu下面进行普通的编译和链接见前文博客里面的过程: 解决C++编译出现的重定义问题:multiple definition of ’XXX‘错误


参考资料:Linux下GCC和Makefile实例(从GCC的编译到Makefile的引入)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值