MCU單晶片韌體設計

2018年11月7日 星期三

[C語言]安裝MinGW - Minimalist GNU for Windows


對於不熟悉Linux,又想要用GNU gcc 編譯你的 C code或g++編譯你的C++ code, 你可以在windows上安裝minGW 使得你的windows也有最小的GNU程式開發工具.


MinGW - Minimalist GNU for Windows

 1.)下載  MinGW 並在windows下進行安裝

MinGW: A native Windows port of the GNU Compiler Collection (GCC), with freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All of MinGW's software will execute on the 64bit Windows platforms.
mingw32-base 選項,按右鍵 Mark for Installation



接著在選單[Installation]上選Apply Changes,就會進行線安裝






2.) 

2-1
  使用Code::Blocks IDE作為來編譯程式     <--建議: 新手使用視窗界面

 


2-2  在Windows下使用 gcc 指令來編譯程式    <--文字指令界面

安裝後, 在windows上將MinGW的執行檔路徑(預設在C:\MinGW) 加入到[Path環境變數]中, 使得gcc這個指令可以被執行。Path 是一個系統變數,可讓您的作業系統從指令行或終端機視窗中尋找所需的可執行檔。

在桌面的電腦圖示按一下滑鼠右鍵 從內容功能表中選擇內容 按一下進階系統設定連結, 可以看到如下的畫面, 就可以修改環境變數



      


在Path 這個變數上再串上一個新的路徑 ; C:\MinGW\bin  (此路徑為MinGW你安裝工具的路徑)





此時你可以開啓windows 的命令視窗(cmd.exe) , 執行gcc -v , 會顯示gcc版本, 若能正確顯示, 就表示Path已正確指定到gcc 路徑.



3.) C程式測試: 寫個hello world程式


接著寫第一個 Hello World程式

// File Location: C:\Users\Joseph\c-code\test.c

#include<stdio.h>
int main()
{
printf("hello world\n");
getchar();
return 0;

}


開啓windows 命令視窗(cmd.exe) 

cd  c-code
#進行gcc編譯
gcc test.c -o test 
#執行程式
test  

執行的畫面


編譯後資料匣將多了一個執行檔 (test.exe)


4.) 套件管理工具:  MinGW installation Manager 


想要寫C++ code, 但沒有g++ 可以編譯(註: g++為C++的編譯器), 可以利用 MinGW installation Manager 去進行套件安裝g++ .

( MinGW installation Manager 在安裝MinGW時就會一併安裝的)



將欲安裝的套件 Mark , 如 mingw32-gcc-g++



 然後回到選單的[Installation] 選擇 Mark All Upgrades, 就會開始進行mingw32-gcc-g++安裝


 成功安裝完mingw32-gcc-g++





5.) 寫第一個C++ 的hello world程式


// File Location: C:\Users\Joseph\c++\hello.cpp
#include <iostream>
using namespace std;

int main() {
   cout << "Hello, world!" << endl;
   return 0;
}

用Notepad++ 編輯一個C++程式 (hello.cpp)




開啓windows 命令視窗(cmd.ext) 

cd  c++
#進行gcc編譯
g++  hello.cpp  -o hello 
#執行程式
hello




程式執行畫面
其他相關文件:







1 則留言 :

  1. 請問如何用PI 接上 PCA9685 來控制 2顆以上的MG995? 程式該如何寫? I2C的模組有需要另外更變什麼嗎?

    回覆刪除