C++简单类中如何书写Makefile(一)
来源:互联网 发布:美非农数据最新信息 编辑:程序博客网 时间:2024/05/17 22:23
最近学习设计模式的时候,开始用起来Makefile,关于Makefile过多的介绍就不多说了。今天讲述的就是一个基本的类,如何使用Makefile。
我们一共有以下几个文件:add.cpp add.h base.cpp base.h context.cpp context.h main.cpp Makefile
直接上代码:
1.类COperation : base.h / .cpp
// base.h#ifndef base_h#define base_hclass COperation{public: int m_nFirst; int m_nSecond; virtual double GetResult();};#endif// base.cpp #include "base.h"double COperation::GetResult(){ double dResult=0; return dResult;}
2.类 AddOperation :
// add.h#include "base.h"class AddOperation : public COperation{public: AddOperation(int a,int b); virtual double GetResult();};// add.cpp #include "add.h"AddOperation::AddOperation(int a,int b){ m_nFirst=a; m_nSecond=b;}double AddOperation::GetResult(){ return m_nFirst+m_nSecond;}
3.类 Context:
// context.h#include "base.h"class Context{private: COperation* op;public: Context(COperation* temp); double GetResult();};// context.cpp #include "context.h"Context::Context(COperation* temp){ op=temp;}double Context::GetResult(){ return op->GetResult();}
主函数:
// main.cpp #include "add.h"#include "context.h"#include <iostream>using namespace std;int main(){ int a,b; char c; cin >> a >> b; Context *context=new Context(new AddOperation(a,b)); cout<<context->GetResult()<<endl; return 0;}
书写makefile 文件:
# Makefile main:main.o base.o add.o context.o g++ main.o base.o add.o context.o -o mainmain.o:main.cpp add.h context.h base.h g++ -c main.cppbase.o:base.cpp base.h g++ -c base.cppadd.o : add.cpp base.h g++ -c add.cppcontext.o:context.cpp base.h g++ -c context.cppclean: -rm -rf *.o main
操作结果:
ubuntu@ubuntu:~/my_file1/Write-Makefile$ lsadd.cpp add.h base.cpp base.h context.cpp context.h main.cpp Makefileubuntu@ubuntu:~/my_file1/Write-Makefile$ubuntu@ubuntu:~/my_file1/Write-Makefile$ makeg++ -c add.cppg++ -c context.cppg++ main.o base.o add.o context.o -o mainubuntu@ubuntu:~/my_file1/Write-Makefile$ lsadd.cpp add.o base.h context.cpp context.o main.cpp Makefileadd.h base.cpp base.o context.h main main.oubuntu@ubuntu:~/my_file1/Write-Makefile$ ./main 3 47ubuntu@ubuntu:~/my_file1/Write-Makefile$
这就是一个基本的Makefile的写法,如果把3个类的cpp文件去掉或者去掉h文件去掉,而是在对应的文件实现,那么Makefile如何书写,感觉挺有意思的。后面的文章会继续讲解。项目源码下载
1 0
- C++简单类中如何书写Makefile(一)
- C++简单类中如何书写Makefile(二)
- makefile的书写规范(一)
- makefile 书写规则一
- 举例书写Makefile文件(C语言)
- 举例书写Makefile文件(C语言)
- 项目Makefile书写示例(一)
- 编译链接简介及makefile书写<一>
- Makefile 详解 (书写规则)
- linux中make命令的简单使用以及Makefile文件的书写
- 如何写Makefile(一)
- makefile 书写
- makefile书写
- makefile 书写
- Makefile 书写
- 【makefile】一起写makefile(三)--Makefile书写规则
- 【makefile】一起写makefile(四)--makefile书写命令
- 如何在C语言中实现简单的泛型编程(一)
- STF 使用
- zkpython安装
- 匈牙利算法java版本
- SpringMVC Controller介绍及常用注解
- JAVASE学习(17)java文件(io)编程
- C++简单类中如何书写Makefile(一)
- Git的诞生
- Android自定义支付宝输入软键盘
- TCP 协议的三次握手和四次握手
- EMAP开发时,app启动时数据库语句初始时报错怎么办?
- Mac上配置MySQL5.7.17出现问题Access denied解决
- HTML5新特性有哪些,你都知道吗
- K60学习笔记6——IAR软件仿真注意事项以及#ifdef用法
- Zookeeper在Kafka中的应用