03.12 windows 下使用make命令,編譯代碼

Windows安裝GNU WinGW編譯器使用makefile

Windows安裝GNU編譯器使用makefile

一、下載安裝MinGW

MinGW下載網頁:http://sourceforge.net/projects/mingw/files/latest/download?source=files

windows 下使用make命令,編譯代碼

下載後,運行程序:mingw-get-inst-20120426.exe,選擇download latest repository catalogues. 選擇編譯器是勾選C Compiler 與C++ Compiler,點擊next進行下載及安裝。

windows 下使用make命令,編譯代碼

二、設置環境變量

右擊計算機->屬性->高級系統設置->環境變量,在系統變量中找到PATH,將MinGW安裝目錄裡的bin文件夾的地址添加到PATH裡面,(注意:PATH裡兩個目錄之間以英文的;隔開)。打開MinGW的安裝目錄,打開bin文件夾,將mingw32-make.exe重命名為make.exe。

windows 下使用make命令,編譯代碼

三、測試GCC編譯

創建一下test.c,用記事本打開該文件,將以下內容複製到文件中。

[cpp] view plain copy

  1. #include<stdio.h>

  2. #include<stdlib.h>

  3. int main(void){

  4. printf("Hello, world!\\n");

  5. system("pause");

  6. return 0;

  7. }

打開命令提示符,更改目錄到test.c的位置,鍵入

gcc -o test.exe test.c

可生成test.exe可執行文件。

四、測試makefile

新建文件夾,在文件夾內創建max_num.c、max.h、max.c、makefile四個文件。

max_num.c內容如下:

[cpp] view plain copy

  1. #include <stdio.h>

  2. #include <stdlib.h>

  3. #include "max.h"

  4. int main(void)

  5. {

  6. printf("The bigger one of 3 and 5 is %d\\n", max(3, 5));

  7. system("pause");

  8. return 0;

  9. }

max.h內容如下:

[cpp] view plain copy

  1. int max(int a, int b);

max.c內容如下:

[cpp] view plain copy

  1. #include "max.h"

  2. int max(int a, int b)

  3. {

  4. return a > b ? a : b;

  5. }

makefile內容如下:

[html] view plain copy

  1. max_num.exe: max_num.o max.o

  2. gcc -o max_num.exe max_num.o max.o

  3. max_num.o: max_num.c max.h

  4. gcc -c max_num.c

  5. max.o: max.c max.h

  6. gcc -c max.c

注意所有含有gcc的行前面是一個製表符,而非若干空格。否則可能會保存,無法編譯。

打開命令提示符,更改目錄到新建的文件夾,鍵入make,可生成指定的應運程序。

測試完成。


分享到:


相關文章: