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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM