Skip to content

Instantly share code, notes, and snippets.

@ljli
Created November 22, 2017 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljli/c5aca47ca4eb89bded7e5cd87144bd96 to your computer and use it in GitHub Desktop.
Save ljli/c5aca47ca4eb89bded7e5cd87144bd96 to your computer and use it in GitHub Desktop.
GHC add foreign object
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Data.Int
import System.Process
import Language.Haskell.TH.Syntax
foreign import ccall "add" add :: Int32 -> Int32 -> Int32
$(let rsSource =
"#[no_mangle] pub extern \"C\" fn add(a: i32, b: i32) -> i32 { a + b }"
compileRust :: String -> FilePath -> IO ()
compileRust src objPath = do
readProcess
"rustc"
["--crate-type", "cdylib", "--emit", "obj", "-o", objPath, "-"]
src
pure ()
in do objPath <- addTempFile ".o"
runIO $ compileRust rsSource objPath
addForeignFilePath RawObject objPath
pure [])
main :: IO ()
main = putStrLn $ "add 16 32 = " <> show (add 16 32)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment