windows 下面make的使用示例
2017-09-27 21:32 雄风狂飙 阅读(3700) 评论(0) 编辑 收藏 举报---恢复内容开始---
前面已经安装了windows下面的编译器g++和mingw32-make,下面就make做个示例说明
1.文档结构
|--src
|--comm
|--comm.cpp(内容如下:)

#include "../include/comm.h" string trim(string &strIn) { string tmp = " "; strIn.erase(strIn.find_last_not_of(tmp)+1,strIn.size()-strIn.find_last_not_of(tmp)); strIn.erase(0,strIn.find_first_not_of(tmp)); return strIn; }
|--Makefile(内容如下:)

libcomm.a:comm.o ar -r libcomm.a comm.o move libcomm.a ..\..\lib comm.o:comm.cpp g++ -m64 -c comm.cpp -o comm.o
|--include
|--comm.h(内容如下:)

#ifndef _COMM_H__ #define _COMM_H__ #include <iostream> using namespace std; string trim(string &strIn); #endif
|--module
|--test
|--Makefile

test.exe:test.cpp g++ -m64 -I../../include -L../../../lib test.cpp -lcomm -o test.exe move ./test.exe ../../../bin/
|--test.cpp(内容如下:)

#include <iostream> #include "..\..\include\comm.h" using namespace std; int main() { //cout<<"Hello World!"<<endl; string hello = " Hello World! "; cout<<trim(hello)<<endl; return 0; }
|--lib
|--bin
2.执行顺序
2.1先到comm(src\comm)下面编译出来.o 和.a 文件,然后把.a移动到lib目录下面
cd src
cd common
mingw32-make
结果如下所示:
your current path>mingw32-make
g++ -m64 -c comm.cpp -o comm.o
ar -r libcomm.a comm.o
ar: creating libcomm.a
move libcomm.a ..\..\lib
移动了 1 个文件。
yout current path>
2.2到test目录(src\module\test)下面编译出来可执行文件,然后把可执行文件移动到bin目录下面:
your current path>mingw32-make
g++ -m64 -I../../include -L../../../lib test.cpp -lcomm -o test.exe
move ./test.exe ../../../bin/
移动了 1 个文件。
your current path>
2.3到bin下面去执行test.exe
your current path>test.exe
Hello World!
your current path>
2.4至此,hello world执行完毕。
3.我这里执行用的是mingw64,make用的是mingw32-make。
使用mingw64下面的g++的时候,执行move命令没有问题,需要加-m64参数(如果OS是64位,推荐使用这个,32位未作测试)
使用mingw32下面的g++的时候,生成的可执行文件也没有问题,但是执行dos命令move就会报错,可以不加参数,默认是-m32.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架