Calling Haskell from C, getting "multiple definition of main" linker error -


i trying learn how interface haskell , c. start out, wrote inc.hs, simplest thing can conceive:

{-# language foreignfunctioninterface #-}  module inc  import foreign import foreign.c.types  inc :: int -> int inc = (+1)  foreign export ccall cinc :: cint -> cint cinc :: cint -> cint cinc = fromintegral . inc . fromintegral 

and compiled produce inc_stub.h:

ghc -c inc.hs 

worked fine. wrote c file, trying simple humanly possible:

#include <stdio.h> #include "inc_stub.h"  int main(int argc, char *argv[]) {     int = 1;     hs_init(&argc, &argv);      = cinc(a);      hs_exit();     if (a == 2) {         puts("worked!");     }      return 0; } 

tried compile it, got linker error:

ghc -no-hs-main inc.c inc -o simplest linking simplest.exe ... inc.o:inc.c:(.text+0x0): multiple definition of `main' inc.o:inc.c:(.text+0x0): first defined here inc.o:inc.c:(.text+0x31): undefined reference `cinc' c:/program files/haskell platform/7.10.2-a/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.6.3/../../../../x86_64-w64-mingw32/bin/ld.exe: inc.o: bad reloc address 0x0 in section `.pdata' collect2: ld returned 1 exit status 

everything compiled using ghc 7.10.2 on windows 10 64-bit.

i did following:

  1. renamed inc.c inc_main.c because having c object file inc.o might overlap haskell object
  2. run ghc -c -no-hs-main inc.hs -o inc.o
  3. produce c object file gcc -o -wall -i/usr/lib/ghc/include -c inc_main.c
  4. link executable ghc -no-hs-main inc.o inc_main.o -o simplest

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -