August 05, 2014 Re: Haskell calling D code through the FFI | ||||
|---|---|---|---|---|
| ||||
Posted in reply to David Soria Parra | So that does indeed solve some of the problems. However, using this method, when linking I get two errors, undefined reference rt_init() and rt_term() I had just put these methods in the header file. If I put wrappers around these functions and export I get the rt_init, rt_term is private.
On Tuesday, 5 August 2014 at 21:28:08 UTC, David Soria Parra wrote:
> On Monday, 4 August 2014 at 20:48:09 UTC, Jon wrote:
>
>> For reasons I don't completely understand, you also need a fake main function, dummy.d:
>>
>> void main(){}
>>
>
> Note that this is not necessary if you compile with -lib e.g.:
>
> dmd -lib -oflibtest.a test.d
>
> and then
>
> ghc Main.hs --make -omain libtest.a
>
> I don't have gdc or ldc installed but as far as I know ldc has a -lib flag as well.
| |||
August 06, 2014 Re: Haskell calling D code through the FFI | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jon | On Tuesday, 5 August 2014 at 23:23:43 UTC, Jon wrote:
> So that does indeed solve some of the problems. However, using this method, when linking I get two errors, undefined reference rt_init() and rt_term() I had just put these methods in the header file. If I put wrappers around these functions and export I get the rt_init, rt_term is private.
>
It works for me, here are the main parts of my Makefile:
DC = ~/bin/dmd
main: Main.hs FunctionsInD.a
ghc -o main Main.hs FunctionsInD.a ~/lib/libphobos2.a -lpthread
FunctionsInD.a: FunctionsInD.d
$(DC) -c -lib FunctionsInD.d
I passed in the phobos object directly because I don't know how to specify the "~/lib" directory on the ghc command line.
| |||
August 06, 2014 Re: Haskell calling D code through the FFI | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jon | Jon via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> writes: > So that does indeed solve some of the problems. However, using this method, when linking I get two errors, undefined reference rt_init() and rt_term() I had just put these methods in the header file. If I put wrappers around these functions and export I get the rt_init, rt_term is private. > rt_init is part of druntime. You need to link druntime into your program in order to make it work. | |||
August 06, 2014 Re: Haskell calling D code through the FFI | ||||
|---|---|---|---|---|
| ||||
Posted in reply to David Soria Parra | Hi, thank you!! I have modified the program based on a previous suggestion. rt_init is called before using any D functionality and rt_term is called after using D functionality. I did this by:
1) Placing int rt_init(); and int rt_term(); into the header file that Haskell reads
2) Creating Haskell stubs
foreign import ccall unsafe "FunctionsInD.h rt_init"
d_init :: IO CInt
foreign import ccall unsafe "FunctionsInD.h rt_term"
d_term :: IO CInt
And then in the Main haskell program, in main, the function starts with
d_init
and ends with
d_term
I'm pretty sure this is working nicely, because I can allocate structs with the "new" keyword in D, and this led to segfaults before using rt_init and rt_term.
I think the problem I was having was trying to do this in a stupid way i.e. put wrappers around init and term on the D side.
However, I still do not know how to compile without the using a fake main. Compiling with -c -lib does still give me a _Dmain undefined reference. I did
dmd -c -lib FunctionsInD.d
ghc --make Main.hs FunctionsInD.a -lphobos2
And get
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/libphobos2.so: undefined reference to `_Dmain'
On Wednesday, 6 August 2014 at 11:03:33 UTC, David Soria Parra via Digitalmars-d-learn wrote:
> Jon via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> writes:
>
>> So that does indeed solve some of the problems. However, using this
>> method, when linking I get two errors, undefined reference rt_init()
>> and rt_term() I had just put these methods in the header file.
>> If I
>> put wrappers around these functions and export I get the rt_init,
>> rt_term is private.
>>
>
> rt_init is part of druntime. You need to link druntime into your program
> in order to make it work.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply