Hi
I need help specifically building Haskell functions that I can call from C++ (or C).
I looked at the Foreign Function Interface explanation at GHC user's guide, but I didn't understand itt well, and I'm not succeeding at doing it.
I'm using GCH 5.04.1 in Win32 environment, and Microsoft Visual C++
That is as far as I got:
I compiled a simple MyModule.hs file with ghc -c -fffi MyModule.hs
It was just a test module that conatined a factorial function called fac.
Here is the source:
module MyModule where
foreign export ccall "fac" fac :: Int -> Int
fac :: Int -> Int
fac n
    | n <= 0 = 1
    | otherwise = n * fac(n-1)
It gave me 4 files: MyModule_stub.h, MyModule_stub.c, MyModule_stub.o, MyModule.o (and a MyModule.hi, but I think not relevant for linking with C)
 
I was thinking that a .o and a .h file would be sufficient for the linking; anyway, I copied those 4 files to my c++ program directory, included the .h file at my c++ main program, add the file MyModule_stub.o to the linking process and tried to compile. That is my c++ program:
#include <iostream>
#include "MyModule_stub.h"

int main(){
 std::cout << fac(10) << std::endl;
 return 0;
}
The linker complains that some function calls are unresolved:
Compiling...
principal.cpp
Linking...
MyModule_stub.o : error LNK2001: unresolved external symbol _MyModule_zdffac_closure
MyModule_stub.o : error LNK2001: unresolved external symbol _GHCziTopHandler_runNonIO_closure
MyModule_stub.o : error LNK2001: unresolved external symbol _rts_getInt
MyModule_stub.o : error LNK2001: unresolved external symbol _rts_checkSchedStatus
MyModule_stub.o : error LNK2001: unresolved external symbol _rts_mkInt
MyModule_stub.o : error LNK2001: unresolved external symbol _rts_apply
MyModule_stub.o : error LNK2001: unresolved external symbol _rts_evalIO
Debug/templates.exe : fatal error LNK1120: 7 unresolved externals
Error executing link.exe.
I tried to use the file MyModule.o, instead of MyModule_stub.o, and got mor unresolved symbols. What was worse, the function symbol itself was unresolved:
Linking...
MyModule.o : error LNK2001: unresolved external symbol _getStablePtr
MyModule.o : error LNK2001: unresolved external symbol _GHCziNum_Szh_con_info
MyModule.o : error LNK2001: unresolved external symbol _stg_upd_frame_info
MyModule.o : error LNK2001: unresolved external symbol _GHCziBase_ZMZN_closure
MyModule.o : error LNK2001: unresolved external symbol _stg_ap_2_upd_info
MyModule.o : error LNK2001: unresolved external symbol _GHCziBase_ZC_con_info
MyModule.o : error LNK2001: unresolved external symbol _stg_sel_0_upd_info
MyModule.o : error LNK2001: unresolved external symbol _GHCziBase_Izh_static_info
MyModule.o : error LNK2001: unresolved external symbol _GHCziReal_zdfIntegralInt_closure
MyModule.o : error LNK2001: unresolved external symbol _stg_CAF_BLACKHOLE_info
MyModule.o : error LNK2001: unresolved external symbol _newCAF
MyModule.o : error LNK2001: unresolved external symbol _stg_IND_STATIC_info
MyModule.o : error LNK2001: unresolved external symbol _stg_INTLIKE_closure
principal.obj : error LNK2001: unresolved external symbol _fac
Debug/templates.exe : fatal error LNK1120: 14 unresolved externals
Error executing link.exe.
Even trying to include both .o modules to the linking process didn't help
If I link this module to a Haskel program, it works out fine...
So, it seems that when I create an object file from a Haskell module, I must link with other object files, that ghc probably links automatically. Is it true? An being the case, how can one know which object files to link?
Please, understand that I'm quite a newbie using haskell when answering.
Thanks for any help,
Mauricio

Reply via email to