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** ⚠️