ffi

Haskell FFI - can you obtain a C Pointer from a Haskell data structure?

爷,独闯天下 提交于 2019-12-09 07:08:13
问题 I have quite a few C structs structured like typedef struct { unsigned int a; unsigned int b; } StructA; And a lot of functions like void doSomethingWith(StructA*,StructB*,StructC*); Is there an easy way to call these functions with Haskell FFI? Like, is there something that behaves like the & operator in C? (I imagine there isn't, but if there was I'd like to know). Do I have to make the Haskell side data 's instance of storeable (I don't have any constructor functions for these structs).

Should I pass a mutable reference or transfer ownership of a variable in the context of FFI?

被刻印的时光 ゝ 提交于 2019-12-08 18:52:30
I have a program that utilizes a Windows API via a C FFI (via winapi-rs). One of the functions expects a pointer to a pointer to a string as an output parameter. The function will store its result into this string. I'm using a variable of type WideCString for this string. Can I "just" pass in a mutable ref to a ref to a string into this function (inside an unsafe block) or should I rather use a functionality like .into_raw() and .from_raw() that also moves the ownership of the variable to the C function? Both versions compile and work but I'm wondering whether I'm buying any disadvantages with

How to force g++ to inline functions?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 15:55:39
问题 I have recently encountered an issue with C++ inline functions when using Haskell FFI to C/C++. Namely, g++ does not really inline functions that are declared inline , and generate symbols for them. Ultimately, this generates linker error when ghci tries to load an object file which calls the inline function: Loading object (static) solveeq.o ... done Loading object (dynamic) /usr/lib/gcc/x86_64-linux-gnu/4.6/libstdc++.so ... done final link ... ghc: solveeq.o: unknown symbol `

How do I call a C function which returns many types of different function pointers?

吃可爱长大的小学妹 提交于 2019-12-08 05:46:37
问题 I'm trying to access the OpenGL function glCreateVertexArrays . Consider the following small C++ program: #include <GL/glx.h> #include <GLFW/glfw3.h> int main() { // Init GLFW glfwInit(); // Set OpenGL Version glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // Select core profile glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true); // Create Window auto window = glfwCreateWindow(400, 400,

Using a haskell function from C++: Undefined reference error

邮差的信 提交于 2019-12-08 00:21:24
问题 I want to call haskell functions out of C++ and did use the tutorial at http://www.haskell.org/ghc/docs/7.0.2/html/users_guide/ffi-ghc.html So I have a haskell file Foo.hs: module Foo where foreign export ccall foo :: Int -> IO Int foo :: Int -> IO Int foo n = return (length (f n)) f :: Int -> [Int] f 0 = [] f n = n:(f (n-1)) and called ghc Foo.hs which created a Foo_stub.h: #include "HsFFI.h" #ifdef __cplusplus extern "C" { #endif extern HsInt foo(HsInt a1); #ifdef __cplusplus } #endif a Foo

How to wrap function in Ruby FFI method that takes struct as argument?

北城余情 提交于 2019-12-07 21:03:25
问题 I am trying to call a function from a shared object using ruby-ffi. I compiled the following into a shared object: #include <stdio.h> typedef struct _WHAT { int d; void * something; } WHAT; int doit(WHAT w) { printf("%d\n", w.d); return w.d; } The problem is, how do I declare the function with attach_function in Ruby? How is the struct argument (WHAT w) defined in the list of arguments in Ruby? It is not a :pointer, and does not seem to fit any of the other available types described in the

c2hs not getting installed / registered correctly

谁说我不能喝 提交于 2019-12-07 16:36:08
问题 When I run cabal install c2hs , it seems to finish correctly, but doesn't register the package. The package seems as if it's not even installed: it doesn't show up in ghc-pkg list , and rerunning cabal install c2hs goes through the same install procedure, instead of saying "All the requested packages are already installed". I'd much appreciate any help getting this up and running. I'm using the prebuilt GHC 7.4.1 binary. cabal install output: http://pastebin.com/CRUii8mm imports are defunct:

Clozure Common Lisp 接口数据库创建操作指导(教程翻译)

僤鯓⒐⒋嵵緔 提交于 2019-12-07 12:53:09
Clozure Common Lisp 接口数据库创建操作指导(教程翻译) === 原文地址: 网络: http://trac.clozure.com/ccl/browser/trunk/source/examples/cocoa/interface-databases/ 本地: file:///ccl-1.8-darwinx86/examples/cocoa/interface-databases/HOWTO.html 原文标题: Interface Databases HOWTO 翻译者: FreeBlues 2013-07-21 特别说明: 有了这个指导应该可以按图索骥地亲自创建那些自己需要框架的接口数据库了,不过我还没亲自试验过,改天亲自试验之后再更新这篇翻译. 目录 0 关于接口数据库 About Interface Databases 0.1 在线文档 Online Documentation 0.2 接口数据库和外部函数接口 Interface Databases and the Foreign Function Interface 0.3 接口数据库和框架 Interface Databases and Frameworks 1 增加新的接口数据库Adding New Interface Databases 1.1 获取和安装ffigen Obtain and

How can I specify linker flags/arguments in a build script?

可紊 提交于 2019-12-07 10:21:51
问题 I'm using Rust, bindgen, and a build script to work on some FFI bindings to a library. This library is built using OpenMP, so when linking against it, I'd normally pass the -fopenmp flag to the compiler. How can I get this flag to be set by build.rs when the library is built by Cargo? Currently, building using Cargo fails, with the failing command being something like: cc -Wl,--as-needed -Wl,-z,noexecstack -m64 -l gomp -l stdc++ ...skipping dozens of paths/files... -Wl,-Bdynamic -l dl -l rt

How to transfer ownership of a value to C code from Rust?

爱⌒轻易说出口 提交于 2019-12-07 04:01:07
问题 I'm trying to write some Rust code with FFI that involves C taking ownership of a struct: fn some_function() { let c = SomeStruct::new(); unsafe { c_function(&mut c); } } I want c_function to take ownership of c . In C++, this could be achieved by the release method of unqiue_ptr . Is there something similar in Rust? 回答1: The std::unique_ptr type in C++ corresponds to Box in Rust, and .release() corresponds to Box::into_raw. let c = Box::new(SomeStruct::new()); unsafe { c_function(Box::into