windows 下面make的使用示例

时间:2023-03-09 03:35:02
windows 下面make的使用示例

---恢复内容开始---

前面已经安装了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)+,strIn.size()-strIn.find_last_not_of(tmp));
strIn.erase(,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 ;
}

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