顯示具有 MinGW 標籤的文章。 顯示所有文章
顯示具有 MinGW 標籤的文章。 顯示所有文章

2012年4月25日 星期三

安裝 C++ Compiler 編譯環境


G++ Compiler windows 安裝環境:

1. 至 MinGW 網站, 選擇 Download -> Installer -> mingw-get-inst -> mingw-get-inst-20120421
2. 下載 mingw-get-inst-20120421.exe
3. 執行 mingw-get-inst-20120421.exe, 選擇 Download latest repository catalogues
4. Select Components 選項中, 加選 C++ Compiler
5. 安裝完後, 新增 hello.cpp 檔案, 輸入以下程式:


// hello.cpp

#include <iostream> // Basic input and output library

int main() {
    std::cout << "Hello World\n";
    return 0; // Return to the operating system
}



6. 執行 C:\MinGW\bin\g++.exe hello.cpp -o hello
   出現: cc1plus.exe - 找不到元件, 這個應用程式無法啟動,因為找不到 libgmp-10.dll,
   重新安裝應用程式可能可以解決這個問題。

7. 如果出現步驟 6 的狀況, 請再到 MinGW 網站, 下載 mingw-get-inst-20111118.exe 檔案.
   下載完後, 重新安裝, 應該就會發現 C:\MinGW\bin 下面有 libgmp-10.dll 的檔案.

8. 設定環境變數 path, 我的電腦 -> 按滑鼠右鍵 -> 內容 -> 進階 -> 環境變數 -> 系統變數 -> Path -> 編輯
   -> 在最後分號處, 加入 C:\MinGW\bin;

9. 如果要編譯 Windows 的程式, 可以執行如下指令:
   C:\MinGW\bin\g++.exe sample01.cpp -o sample01 -mwindows -Wall

2010年9月21日 星期二

使用 MinGW 編譯 C++ 紀錄

1. windres Lession5.rc Lession5.o
2. g++ -o Lession5.exe Lession5.c Lession5.o
3. g++ -o sample01 sample01.cpp -mwindows (-mwindows 是在windows下執行exe檔案時,不會在跳出一個Console的視窗)

// g++ dos(使用 Notepad++ NppExec 套件的紀錄)
C:\MinGW\bin\g++.exe "$(FULL_CURRENT_PATH)" -o $(NAME_PART)

// g++(使用 Notepad++ NppExec 套件的紀錄)
C:\MinGW\bin\g++.exe "$(FULL_CURRENT_PATH)" -o $(NAME_PART) -mwindows -Wall( -Wall 會顯示程式中是否有無效的參數或函式)

// g++ & rc & comctl32(使用 Notepad++ NppExec 套件的紀錄, 使用Window控制項)
C:\MinGW\bin\windres.exe $(NAME_PART).rc -o $(NAME_PART).o
C:\MinGW\bin\g++.exe "$(FULL_CURRENT_PATH)" -o $(NAME_PART) $(NAME_PART).o "C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib\comctl32.lib" -mwindows -Wall

// g++ & rc(使用 Notepad++ NppExec 套件的紀錄)
C:\MinGW\bin\windres.exe $(NAME_PART).rc -o $(NAME_PART).o
C:\MinGW\bin\g++.exe "$(FULL_CURRENT_PATH)" -o $(NAME_PART) $(NAME_PART).o -mwindows -Wall

// g++ & lib(使用 Notepad++ NppExec 套件的紀錄, 使用Windows Timer套件)
C:\MinGW\bin\g++.exe "$(FULL_CURRENT_PATH)" -o $(NAME_PART) "C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib\WinMM.Lib" -mwindows

// C++ 連結 MySQL 的方法(不使用 Visual C++ 和 .net 的方式)
01. 需要上網抓取 mysql-connector-c-noinstall-6.0.2-win32
02. 參考 http://www.programmer-club.com/showSameTitleN/mysql/911.html
03. 參考 http://www.openwebspider.org/documentation/how-to-link-libmysqllib-with-dev-c-or-gcc-under-windows/
04. 產生 libmysql.a 的方式
> reimp.exe <FULL_PATH_OF(MySQL\lib\opt)>libmysql.lib
(^ This command creates: libmysql.def AND libmysql.a)
> dlltool.exe –input-def LIBMYSQL.def –dllname libmysql.dll –output-lib libmysql.dll
(^ this creates libmysql.dll)
05. 將產生的 libmysql.a 拷貝到 MinGW 的 lib 下
06. 在程式中, 需要 include 兩個檔案
#include <winsock.h> // SOCKET TYPE(for mysql.h)
#include <mysql.h>
07. 可以使用以下程式測試連結是否成功??
using namespace std;
int main(void) {
MYSQL my_connection; //This is a SQL connection.
mysql_init(&my_connection); //initinal mysql connection

if (mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD, DATABASE, 0, NULL, CLIENT_FOUND_ROWS)) { //connect success
cout << "Connection Success!!\r\n";
mysql_close(&my_connection); //close database connection
} else {
cout << "Connection Fail!!\r\n";
}
getch();
return EXIT_SUCCESS;
}

08. 編譯檔案的時候, 需要在後面加上 -lmysql -lws2_32, 如下:(使用 Notepad++ NppExec 套件的紀錄)
C:\MinGW\bin\g++.exe "$(FULL_CURRENT_PATH)" -o $(NAME_PART) -I "C:\mysql_c\include" -lmysql -lws2_32
例如: C:\MinGW\bin\g++.exe "C:\portable\C++\WinApp\mysqlconn.cpp" -o mysqlconn -I "C:\mysql_c\include" -lmysql -lws2_32 -mwindows

使用 MinGW + Notepad++ 來寫 C++

1. 下載 Notepad++
2. 下載 Notepad++ plugin -- NppExec
3. 下載 MinGW-5.1.6.exe,安裝起來

4. 使用 Notepad++ 新增檔案
// hello.cpp
#include // cout
#include "conio.h" // getch
#include // TCHAR

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    std::cout << "Hello World!\n";
    getch();
    return 0;
}


5. 使用 NppExec套件, 輸入
C:\MinGW\bin\g++.exe "$(FULL_CURRENT_PATH)" -o $(NAME_PART)

6. 執行後, 即會 Complier 成 exe 檔案 !!

2010年1月8日 星期五

MinGW安裝方式

MinGW安裝方式:
1. 先至 MingGW 下載 MinGW-5.1.4.exe
2. 安裝時除了core 必安裝外, 最好在安裝c++套件
3. 設定 windows 的 path , 加上 C:\MinGW\bin
4. 在 C:\MinGW\ 下, 產生 win_hello.c 檔案
內容如下:
#include
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow)
{
MessageBox (NULL, "Hello", "Hello Demo", MB_OK);
return (0);
}

5. 執行 cmd, 在console 下執行 :
C:\MinGW>g++ -o win_hello win_hello.c -mwindows

6. 完成後, 如果沒有任何錯誤, 就會產生一個簡單的exe程式了~~ 祝順利~~

C/C++ MinGW 中,如何 Compilier *.rc 和 *.c

ps: *.rc is resource file
1. 先用 *.rc 產生 *.o 檔案.
2. 在 Complilier C/C++時,將 *.o 加入編譯中.

Ex:

windres resources.rc resources.o
gcc -o main.exe main.c resources.o

參考網站:這裡