haskell - Gakgu/Gakgu.github.io GitHub Wiki

μ •μ˜

순수 ν•¨μˆ˜ν˜• μ–Έμ–΄.

c++μ—μ„œ ν•˜μŠ€μΌˆ μ½”λ“œ 호좜 예제

λ‹€μŒ 3개의 예제 νŒŒμΌμ„ μž‘μ„±ν•œ ν›„ λΉŒλ“œν•˜λ©΄ λœλ‹€.

  • hello.hs

      module Hello where
    
      foreign export ccall add :: Int -> Int -> Int
      foreign export ccall multiply :: Int -> Int -> Int
    
      add :: Int -> Int -> Int
      add x y = x + y
    
      multiply :: Int -> Int -> Int
      multiply x y = x * y
    
  • main.cpp

      #include <iostream>
      #include "hello_stub.h"
      using namespace std;
    
      int main(int argc, char** argv)
      {
          hs_init(&argc, &argv);
    
          cout << "3 + 7 = " << add(3, 7) << endl;
          cout << "3 * 7 = " << multiply(3, 7) << endl;
    
          hs_exit();
    
          return 0;
      }
    
  • Makefile

      build : hello.o main.o
          ghc -no-hs-main -o main main.o hello.o -lstdc++
    
      main.o:
          g++ -c main.cpp -I/usr/lib/ghc-8.6.4/include
    
      hello.o:
          ghc -c hello.hs
    
      clean:
          rm  hello.hi \
              hello.o \
              hello_stub.h \
              main.o \
              main
    
⚠️ **GitHub.com Fallback** ⚠️