이전에는 MinGW 의 gcc 버전이 낮아서 gmp, mpfr 을 사용하는데 핸디캡이 있었지만, 지금은 MinGW 의 gcc  버전이 4.6.x 대로 높아져서 꽤 쓸만한 개발도구가 되었다.

그래서 MinGW 의 설치 방법과 gmp, mpfr 라이브러리를 사용하는 방법을 남겨 둔다.

MinGW 는 Minimalist GNU for Windows 를 줄인 굴이라고 보면 이애하기가 쉬울 것이다.

 

* 윈도우 환경에  MinGW 및 MSYS 설치하기


무료로 쓰는 GNU C/C++ 컴파일러를 사용하기 위해서, 윈도우에 cygwin을 설치하는 일은 다소 무겁다는 생각이 든다. 이럴 때는 MinGW와 MSYS를 설치하면 간단한 C/C++ 개발 환경이 구축된다.

MinGW는 윈도우용 GCC Compiler Toolchain이고,
MSYS(Minal SYStem)로 윈도우에서 리눅스 쉘(Linux Shell)과 Binutils 환경을 제공한다.


  1.  최신 MinGW GUI 인스틀러 다운로드

         - MinGW와 MYS의 설치는 GUI 인스톨러를 사용하면 설치 과정이 간단하다.

 

     < GUI 인스톨러를 실행한 첫 화면 >

 

     < 설치 과정 중에 모든 설치 옵션을 체크한다. >

 

     < 설치 과정 중에 MSYS 옵션도 체크해야 한다. >

 

 

 2. MinGW 의 설치 확인

명령프롬프트> set PATH=c:\MinGW\bin;%PATH%

명령프롬프트> gcc -v

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.2/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.6.2/configure --enable-languages=c,c++,ada,fortran,obj
c,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgo
mp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-r
untime-libs --build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.6.2 (GCC)


 

 3. MSYS 환경 없이 MinGW 의 gcc, g++ 사용하기

         명령프롬프트> set PATH=c:\MinGW\bin;%PATH%

         명령프롬프트> g++ -o rpnSTL rpnSTL.cpp

         명령프롬프트> rpnSTL

 

 4. MSYS 환경  하레 MinGW 의 gcc, g++ 사용하기

         명령프롬프트> set PATH=c:\MinGW\bin;c:\MinGW\msys\1.0\bin;%PATH%

         명령프롬프트> msysmnt

         명령프롬프트> bash

         $  g++ -o rpnSTL rpnSTL.cpp
 
         $  ./rpnSTL



[ STL을 이용하는 C++ 예제 소스:  rpnSTL.cpp ]

   (참고: GCC 4.6.2 용으로 수정됨, atof() 대신 ::strtod() 사용하는 것으로 수정함.)

// stl/rpnSTL.cpp -- RPN (Reverse Polish Notation) Calculator
//
//  Modified by PH Kim, 2012-12-31
//
//  Which are different from the original source rpn.cpp ?
//
//      1. double instead of int
//      2. negative value possible
//      3. support the power operator ^
//      4. support for gcc 4.6.2
//      5. etc
//
//  Compile:
//      [Borland C++ 5.5.1]  bcc32 rpnSTL.cpp
//        [Visual C++ 2008]  cl /EHsc rpnSTL.cpp
//                [GNU C++]  g++ -o rpnSTL rpnSTL.cpp
//
//  Execute:  rpnSTL
//            ./rpnSTL
//
// Original from:
//    http://www.fredosaurus.com/notes-cpp/examples/rpn/rpn.html
// ---------------------------------------------------------
// stl/rpn.cpp -- RPN (Reverse Polish Notation) Calculator
// Fred Swartz 2001-12-05, 2002-09-27
// ---------------------------------------------------------

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <stdexcept>
#include <cstdlib>    // required for ::strtod()
using namespace std;
//--- prototypes
double pop(vector<double>& stk);
void printVector(vector<double>& stk);
void clearVector(vector<double>& stk);
void printUsage();

//============================================================= main
int main() {
    vector<double> opndStack; // vector used as operand stack
    string token;          // to read number or operator
    cout << "RPN Calculator" << endl;
    printUsage();
    while (cin >> token) {
        if (isdigit(token[0]) || (token.length() > 1 && (token[0] == '-' || token[0] == '+') && isdigit(token[1]))) { // if first is digit, it's number.
             // opndStack.push_back(atof(token.c_str())); // convert, push
             // Note that atof(str) has been changed to ::strtod(str, 0)
             opndStack.push_back(::strtod(token.c_str(), 0)); // convert, push
            printVector(opndStack);

        } else { // If it's not a number, assume it's an operator
            double left, right;  //  used by some operators as temps
            switch (token[0]) {  // assume operators are one char
              case '+': opndStack.push_back(pop(opndStack) + pop(opndStack));
                        break;
              case '-': right = pop(opndStack); // get right operand
                        left  = pop(opndStack); // get left operand
                        opndStack.push_back(left - right);
                        break;
              case '*': opndStack.push_back(pop(opndStack) * pop(opndStack));
                        break;
              case '/': right = pop(opndStack); // get right operand
                        left  = pop(opndStack); // get left operand
                        opndStack.push_back(left / right);
                        break;
              case '^': right = pop(opndStack); // get right operand
                        left  = pop(opndStack); // get left operand
                        opndStack.push_back(pow(left, right));
                        break;
              case '=':
              case 'p':
                        break;
              case 'c':
                        clearVector(opndStack);
                        break;
              case 'q':
              case 'x':
                        cout << "Quit" << endl;
                        exit(0);
                        break;
              case 'h':
                        printUsage();
                        break;
              default:  throw domain_error("Undefined operator");
            }
            printVector(opndStack);
            if (!opndStack.empty())
                cout << "    (" << token[0] << ") Top value: " << opndStack.back() << endl;
        }
    }
    return 0;
}//end main

//============================================================== pop
   // This utility function checks stack for underflow
   // and pops (removes and returns) last element.
double pop(vector<double>& stk) {
    if (stk.empty()) {
        throw underflow_error("Stack underflow.");
    }
    double result = stk.back();
    stk.pop_back();
    return result;
}//end pop

double peek(vector<double>& stk) {
    if (stk.empty()) {
        throw underflow_error("Stack underflow.");
    }
    return stk.back();
}//end pop

void printVector(vector<double>& stk) {
    int n = stk.size();
    cout << "[";
    for (int i = 0; i < n; i++) {
       cout << stk[i];
       if (i < n - 1)
           cout <<", ";
    }
    cout << "]" << endl;
}//end printVector

void clearVector(vector<double>& stk) {
    while (!stk.empty()) {
       stk.erase(stk.begin());
       // stk.pop_back();
    }
}//end clearVector
void printUsage() {
    cout << "  +,-,*,/,^: opr,  =,p: show top,  c: clear stack,  q,x: quit,  h: help" << endl;
}//end printVector

/*
// stl/rpn.cpp -- RPN (Reverse Polish Notation) Calculator
// Fred Swartz 2001-12-05, 2002-09-27
#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
using namespace std;
int pop(vector<int>& stk);  //--- prototype to pop stack
//============================================================= main
int main() {
    vector<int> opndStack; // vector used as operand stack
    string token;          // to read number or operator
    while (cin >> token) {
        if (isdigit(token[0])) { // if first is digit, it's number.
            opndStack.push_back(atoi(token.c_str())); // convert, push

        } else { // If it's not a number, assume it's an operator
            int left, right;  //  used by some operators as temps
            switch (token[0]) {  // assume operators are one char
              case '+': opndStack.push_back(pop(opndStack) + pop(opndStack));
                        break;
              case '-': right = pop(opndStack); // get right operand
                        left  = pop(opndStack); // get left operand
                        opndStack.push_back(left - right);
                        break;
              case '*': opndStack.push_back(pop(opndStack) * pop(opndStack));
                        break;
              case '/': right = pop(opndStack); // get right operand
                        left  = pop(opndStack); // get left operand
                        opndStack.push_back(left / right);
                        break;
              default:  throw domain_error("Undefined operator");
            }
            cout << "Result: " << opndStack.back() << endl;
        }
    }
    return 0;
}//end main
//============================================================== pop
   // This utility function checks stack for underflow
   // and pops (removes and returns) last element.
int pop(vector<int>& stk) {
    if (stk.empty()) {
        throw underflow_error("Stack underflow.");
    }
    int result = stk.back();
    stk.pop_back();
    return result;
}//end pop
*/



Posted by Scripter
,