2011-12-31 9 views
17

Desidero creare una libreria dinamica contenente le funzioni haskell. Lavoro su Linux e voglio chiamare questa libreria dinamica dal codice C++.Creazione di una libreria dinamica con haskell e utilizzo da C++

Ho usato l'esempio a http://wiki.python.org/moin/PythonVsHaskell ed hanno i seguenti file:

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 XCAT(a,b) a ## b 
#define STR(a) XSTR(a) 
#define XSTR(a) #a 

#include <HsFFI.h> 

extern void CAT (__stginit_, MODULE) (void); 

static void library_init (void) __attribute__ ((constructor)); 
static void 
library_init (void) 
{ 
    /* This seems to be a no-op, but it makes the GHCRTS envvar work. */ 
    static char *argv[] = { STR (MODULE) ".so", 0 }, **argv_ = argv; 
    static int argc = 1; 

    hs_init (&argc, &argv_); 
    hs_add_root (CAT (__stginit_, MODULE)); 
} 

static void library_exit (void) __attribute__ ((destructor)); 
static void 
library_exit (void) 
{ 
    hs_exit(); 
} 

Ora compilare questo file in una dinamica biblioteca:

$ ghc -dynamic -shared -fPIC -optc '-DMODULE=Test' Test.hs module_init.c -o libTest.so 
[1 of 1] Compiling Test    (Test.hs, Test.o) 
Linking libTest.so ... 
.515.053.691,36321 milioni

Questo crea tra le altre cose il file Test_stub.h:

#include "HsFFI.h" 
#ifdef __cplusplus 
extern "C" { 
#endif 
extern HsInt32 hsfun(HsInt32 a1); 
#ifdef __cplusplus 
} 
#endif 

e Test_stub.c:

#define IN_STG_CODE 0 
#include "Rts.h" 
#include "Stg.h" 
#ifdef __cplusplus 
extern "C" { 
#endif 

extern StgClosure Test_zdfhsfunzua165_closure; 
HsInt32 hsfun(HsInt32 a1) 
{ 
Capability *cap; 
HaskellObj ret; 
HsInt32 cret; 
cap = rts_lock(); 
cap=rts_evalIO(cap,rts_apply(cap,(HaskellObj)runIO_closure,rts_apply(cap,&Test_zdfhsfunzua165_closure,rts_mkInt32(cap,a1))) ,&ret); 
rts_checkSchedStatus("hsfun",cap); 
cret=rts_getInt32(ret); 
rts_unlock(cap); 
return cret; 
} 
static void stginit_export_Test_zdfhsfunzua165() __attribute__((constructor)); 
static void stginit_export_Test_zdfhsfunzua165() 
{getStablePtr((StgPtr) &Test_zdfhsfunzua165_closure);} 
#ifdef __cplusplus 
} 
#endif 

Poi crea un file cpp main.cpp:

#include "Test_stub.h" 

#include <iostream> 

using namespace std; 

int main() 
{ 
    cout << hsfun(5); 
} 

e voglio compilare e collegarlo. Ma quando chiamo g ++, si dice:

$ g++ -I/usr/lib/ghc-7.0.3/include -L. -lTest main.cpp 
/tmp/ccFP2AuB.o: In function `main': 
main.cpp:(.text+0xa): undefined reference to `hsfun' 
collect2: ld gab 1 als Ende-Status zurück 

Così ho aggiunto il file Test_stub.o alla riga di comando (anche se credo che la funzione hsfun dovrebbe già essere definito in libTest.so che viene aggiunto tramite il -ltest . parametro non credo, dovrei collegare il file Test_stub.o nell'eseguibile perché voglio utilizzare il collegamento dinamico), ma anche questo non funziona:

$ g++ -I/usr/lib/ghc-7.0.3/include -L. -lTest main.cpp Test_stub.o 
Test_stub.o: In function `hsfun': 
Test_stub.c:(.text+0x9): undefined reference to `rts_lock' 
Test_stub.c:(.text+0x16): undefined reference to `rts_mkInt32' 
Test_stub.c:(.text+0x1d): undefined reference to `Test_zdfhsfunzua165_closure' 
Test_stub.c:(.text+0x28): undefined reference to `rts_apply' 
Test_stub.c:(.text+0x2f): undefined reference to `base_GHCziTopHandler_runIO_closure' 
Test_stub.c:(.text+0x3a): undefined reference to `rts_apply' 
Test_stub.c:(.text+0x4a): undefined reference to `rts_evalIO' 
Test_stub.c:(.text+0x5c): undefined reference to `rts_checkSchedStatus' 
Test_stub.c:(.text+0x66): undefined reference to `rts_getInt32' 
Test_stub.c:(.text+0x70): undefined reference to `rts_unlock' 
Test_stub.o: In function `stginit_export_Test_zdfhsfunzua165': 
Test_stub.c:(.text.startup+0x3): undefined reference to `Test_zdfhsfunzua165_closure' 
Test_stub.c:(.text.startup+0x8): undefined reference to `getStablePtr' 
collect2: ld gab 1 als Ende-Status zurück 

devo collegare il Test_stub .O? Se sì, perché? E quali argomenti dovrei passare al linker?

+0

Non conosco i dettagli; ma è certamente necessario collegare anche il runtime di Haskell (in particolare il suo garbage collector). –

+1

"$ g ++ -I/usr/lib/ghc-7.0.3/include -L. -lTest main.cpp" forse funziona se si mettono i flag del linker alla fine della riga di comando? –

+0

@Daniel: È la seconda volta che vedo qualcuno suggerire di mettere i flag di linker alla fine e la prima volta sembra che risolva il problema. Perché? Pensavo che il posizionamento delle bandiere non importasse? – Xeo

risposta

9

Probabilmente più facile che lottare con g ++ è lasciare ghc fare il lavoro,

ghc main.cpp -o hithere -L. -lTest -lstdC++

ha fatto il lavoro per me dopo aver creato la lib condivisa come hai fatto tu. L'ho provato con 7.2.2 e 7.0.2, entrambi hanno funzionato qui.

+2

L'interfaccia haskell è un modulo di un progetto più grande e non voglio compilare l'intero progetto C++ con ghc. – Heinzi

+0

Questo ha senso. Si potrebbe provare a catturare i flag necessari per il linker facendo in modo che ghc compili l'esempio con una verbosità sufficientemente alta e reindirizzando lo stderr a un file.Ti darà comunque una lunga lista di oggetti da collegare. –

+1

Ho funzionato usando la tua soluzione. Quando passo il parametro -v a ghc, stampa i parametri del linker usato sulla riga di comando. – Heinzi

Problemi correlati