代码改变世界

windows 下面make的使用示例

  雄风狂飙  阅读(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;
}
View Code
复制代码

               |--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
View Code
复制代码

       |--include
               |--comm.h(内容如下:)

复制代码
#ifndef _COMM_H__
#define _COMM_H__
#include <iostream>
using namespace std;
string trim(string &strIn);
#endif
View Code
复制代码

       |--module
               |--test
                           |--Makefile

test.exe:test.cpp
    g++ -m64 -I../../include -L../../../lib test.cpp -lcomm -o test.exe
    move ./test.exe ../../../bin/
View Code

                           |--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;    
}
View Code
复制代码

|--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.

 

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示