Windows安裝GNU編譯器使用makefile

1、下載安裝MinGW

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


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


2、設置環境變量

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


3、測試GCC編譯

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

[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的位置,鍵入 htm

gcc -o test.exe test.c blog

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

4、測試makefile

新建文件夾,在文件夾內建立max_num.cmax.hmax.cmakefile四個文件。 編譯器

max_num.c內容以下: it

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include "max.h"  
  4.   
  5. int main(void)  
  6. {  
  7.     printf("The bigger one of 3 and 5 is %d\n", max(3, 5));  
  8.     system("pause");  
  9.     return 0;  
  10. }  

max.h內容以下: io

[cpp]  view plain copy
  1. int max(int a, int b);  

max.c內容以下:

[cpp]  view plain copy
  1. #include "max.h"  
  2.   
  3. int max(int a, int b)  
  4. {  
  5.     return a > b ? a : b;  
  6. }  


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.   
  4. max_num.o: max_num.c max.h  
  5.     gcc -c max_num.c  
  6.   
  7. max.o: max.c max.h  
  8.     gcc -c max.c  


注意全部含有gcc的行前面是一個製表符,而非若干空格。不然可能會保存,沒法編譯。

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

相關文章
相關標籤/搜索