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


Windows安裝GNU WinGW編譯器使用makefile

 

 

Windows安裝GNU編譯器使用makefile

一、下載安裝MinGW

 

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


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

二、設置環境變量

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

三、測試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.   
  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內容如下:

 

[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,可生成指定的應運程序。

測試完成。


免責聲明!

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



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