ffi

How to pass a string from Haskell to C?

江枫思渺然 提交于 2019-12-04 03:36:39
All I want to do is pass a plain-text string from Haskell to C. However, it says that [Char] is an unacceptable return type. I can't find anywhere why they think it is, nor what acceptable return types are. I'm trying to make a very simple OS image that I can boot with Qemu. Does anyone know how to do this? Thanks. {-# LANGUAGE ForeignFunctionInterface #-} module Hello where import Foreign import Foreign.C.String import Foreign.C.Types hello :: String -> (CString -> IO a) -> IO a hello = "Hello, world!" foreign export ccall hello :: String -> (CString -> IO a) -> IO a You want a CString .

ghc 7.4.1 not producing stub.o files

跟風遠走 提交于 2019-12-04 03:17:11
问题 I'm running the default installation of Haskell platform on Ubuntu and when I run this example http://www.haskell.org/haskellwiki/Calling_Haskell_from_C "ghc -c -O Safe.hs" is not producing the Safe_stub.o file. I have checked this on a separate installation of Ubuntu on a friends box and on both 32 bit and 64 bit Ubuntu installs. Can somebody confirm if this is specific to ghc-7.4.1 or Ubuntu installs only? Thanks! 回答1: It's a ghc-7.4 ( ghc >= 7.2 actually) thing. That doesn't need and

Compiling ghc with -fPIC support

99封情书 提交于 2019-12-03 20:37:04
问题 I'm trying to install GHC with -fPIC support in Fedora. I've grabbed a source tarball since it seems no binary one has this. In Build.mk i've changed the quick build type to ifeq "$(BuildFlavour)" "quick" SRC_HC_OPTS = -H64m -O0 -fasm -fPIC GhcStage1HcOpts = -O -fasm -fPIC GhcStage2HcOpts = -O0 -fasm -fPIC GhcLibHcOpts = -O -fasm -fPIC SplitObjs = NO HADDOCK_DOCS = NO BUILD_DOCBOOK_HTML = NO BUILD_DOCBOOK_PS = NO BUILD_DOCBOOK_PDF = NO endif unfortunately, when compiling i still get the ld

Is it possible to invoke bash or shell scripts from a haskell program?

岁酱吖の 提交于 2019-12-03 16:25:28
问题 I'm writing some shell scripts with haskell, which I'm running in gitbash, but there are a few other existing scripts I'd like to be able to use from those scripts. For example, I'd like to run maven goals or do a git pull, but without having to integrate specifically with those tools. Is there a way to do this? 回答1: You can use System.Process. For example, executing seq 1 10 shell command: > import System.Process > readProcess "seq" ["1", "10"] "" "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" it ::

Building a dynamic library with haskell and using it from C++

给你一囗甜甜゛ 提交于 2019-12-03 15:33:45
问题 I want to build a dynamic library containing haskell functions. I work on linux and want to call this dynamic library from C++ code. I used the example at http://wiki.python.org/moin/PythonVsHaskell and have the following files: Test.hs: {-# LANGUAGE ForeignFunctionInterface #-} module Test where import Foreign.C.Types hsfun :: CInt -> IO CInt hsfun x = do putStrLn "Hello World" return (42 + x) foreign export ccall hsfun :: CInt -> IO CInt module_init.c: #define CAT(a,b) XCAT(a,b) #define

Catching panic! when Rust called from C FFI, without spawning threads

徘徊边缘 提交于 2019-12-03 13:22:56
I'm working on a Rust wrapper for the Duktape JavaScript interpreter . In a normal use case, the call stack will look like this: Rust: Arbitrary application code. Rust: My library wrapper. C: The Duktape interpreter. Rust: My Rust code. Rust: Arbitrary callbacks into application code. What happens if (5) calls panic! ? According to various Rust developers on IRC, attempting to panic! from inside non-Rust callframes like (3) may result in undefined behavior. But according the Rust documentation, the only way to catch a panic! is using std::task::try , which spawns an extra thread. There's also

How to handle long running external function calls such as blocking I/O in Rust?

房东的猫 提交于 2019-12-03 11:29:43
问题 Editor's note: This question is from a version of Rust prior to 1.0 and uses terms and functions that do not exist in Rust 1.0 code. The concepts expressed are still relevant. I need to read data provided by an external process via a POSIX file descriptor in my Rust program. The file descriptor connection is kept up a very long time (hours) and the other side passes data to me from time to time. I need to read and process the data stream continuously. To do so, I wrote a loop that calls libc:

Passing a list of strings from Python to Rust

自古美人都是妖i 提交于 2019-12-03 11:21:19
I've been learning Rust for about two weeks now and today, I got into its FFI. I used Python to play with Rust, using ctypes and libc. I passed integers, strings and even learned to pass a list of integers ( thanks to this wonderful answer ). Then, I tried to pass a list of strings (following the reasoning behind the that answer), but I failed, as I couldn't get a lead on it. In Python, I have something like this to pass the array of strings. def testRust(): lib = ctypes.cdll.LoadLibrary(rustLib) list_to_send = ['blah', 'blah', 'blah', 'blah'] c_array = (ctypes.c_char_p * len(list_to_send))()

Passing a set of NumPy arrays into C function for input and output

落爺英雄遲暮 提交于 2019-12-03 11:13:28
Let's assume we have a C function that takes a set of one or more input arrays, processes them, and writes its output into a set of output arrays. The signature looks as follows (with count representing the number of array elements to be processed): void compute (int count, float** input, float** output) I want to call this function from Python via ctypes and use it to apply a transformation to a set of NumPy arrays. For a one-input/one-output function defined as void compute (int count, float* input, float* output) the following works: import ctypes import numpy from numpy.ctypeslib import

unsafePerformIO and FFI library initialization

纵然是瞬间 提交于 2019-12-03 11:11:40
I'm creating an FFI module to a library in C which wants a 1-time, non-reentrant function to be called before anything else is. This call is idempotent, but stateful, so I could just call it in every Haskell call. But it's slow and due to non-reentrancy it could cause conflicts. So is this the right time to use unsafePerformIO? I could wrap a Bool in an unsafe IORef or MVar to make these initialization calls idempotent by ignoring subsequent calls (calls where the global, hidden IORef state is False). If not, what is the right way to do this? Don Stewart I prefer the approach of initializing