compiling a shared object written in haskell and template haskell and linking it with main in c

Viewed 728

i am trying to compile several literate haskell (.lhs) files to a shared object (.so), and then to link it with a main written in c. The issue here, though, is that 2 of the files used to create the .so are template haskell. i followed the rules for compiling a .so with template haskell, which means that i did the following steps:
1. I compiled each .lhs files ,statically
2. Then i compiled them all second time dynamically.
3. I created the shared object from the object files i got from steps 1 & 2.
4. I compiled the main.c into main.o
5. I created an executable from steps 3 & 4.

there are 3 files from which the .so is created. Dep1.lhs, Dep2.lhs & Dep3.lhs, and a main written in c

when i compile makefile i get this message:

my_directory >> make all
rm -f *.o *.hi *.so *.dyn_hi *.dyn_o main
ghc -c Dep3.lhs -XTemplateHaskell -XForeignFunctionInterface -o Dep3.o
ghc -c Dep3.lhs -dynamic -XTemplateHaskell -fPIC -no-hs-main -XForeignFunctionInterface -o Dep3.dyn_o -osuf dyn_o -hisuf dyn_hi ghc -c Dep2.lhs -XTemplateHaskell -XForeignFunctionInterface -o Dep2.o
ghc -c Dep2.lhs -dynamic -XTemplateHaskell -fPIC -no-hs-main -XForeignFunctionInterface -o Dep2.dyn_o -osuf dyn_o -hisuf dyn_hi
ghc -c Dep1.lhs -XTemplateHaskell -XForeignFunctionInterface -o Dep1.o

Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package pretty-1.1.1.0 ... linking ... done.
Loading package array-0.4.0.1 ... linking ... done.
Loading package deepseq-1.3.0.1 ... linking ... done.
Loading package containers-0.5.0.0 ... linking ... done.
Loading package template-haskell ... linking ... done.
ghc -c Dep1.lhs -dynamic -XTemplateHaskell -fPIC -no-hs-main -XForeignFunctionInterface -o Dep1.dyn_o -osuf dyn_o -hisuf dyn_hi
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package pretty-1.1.1.0 ... linking ... done.
Loading package array-0.4.0.1 ... linking ... done.
Loading package deepseq-1.3.0.1 ... linking ... done.
Loading package containers-0.5.0.0 ... linking ... done.
Loading package template-haskell ... linking ... done.
ghc -O2 -dynamic -shared -fPIC Dep1.dyn_o Dep2.dyn_o Dep3.dyn_o -o libShared.so -lHSrts-ghc7.6.3
gcc -O2 -I/usr/local/lib/ghc-7.6.3/include -L/usr/local/lib/ghc-7.6.3 -L/usr/local/lib/ghc-7.6.3/template-haskell-2.8.0.0/ -c Main.c -o main.o
gcc -o main main.o -L/usr/local/lib/ghc-7.6.3 -L/usr/local/lib/ghc-7.6.3/template-haskell-2.8.0.0/ -L. -lShared -Wl,-rpath,/usr/local/lib/ghc-7.6.3 -L/home/tal/a_prerequisites/new_haskell/ghc-7.6.3/libraries/base/dist-install/build/libHSbase-4.6.0.1-ghc7.6.3. -lHStemplate-haskell-2.8.0.0
/usr/bin/ld: dynamic variable `ghczmprim_GHCziTypes_True_closure' is zero size

/usr/bin/ld: dynamic variable `ghczmprim_GHCziTypes_ZMZN_closure' is zero size

/usr/bin/ld: /usr/local/lib/ghc-7.6.3/template-haskell-2.8.0.0//libHStemplate-haskell-2.8.0.0.a(Syntax__1744.o)(.text+0x77): unresolvable R_X86_64_32 relocation against symbol `ghczmprim_GHCziTypes_True_closure'

/usr/bin/ld: /usr/local/lib/ghc-7.6.3/template-haskell-2.8.0.0//libHStemplate-haskell-2.8.0.0.a(Lib__228.o)(.text+0x14): unresolvable R_X86_64_32S relocation against symbol `ghczmprim_GHCziTypes_ZMZN_closure'

/usr/bin/ld: /usr/local/lib/ghc-7.6.3/template-haskell-2.8.0.0//libHStemplate-haskell-2.8.0.0.a(Lib__137.o)(.text+0x14): unresolvable R_X86_64_32S relocation against symbol `ghczmprim_GHCziTypes_ZMZN_closure'

/usr/bin/ld: /usr/local/lib/ghc-7.6.3/template-haskell-2.8.0.0//libHStemplate-haskell-2.8.0.0.a(Lib__227.o)(.text+0x14): unresolvable R_X86_64_32S relocation against symbol `ghczmprim_GHCziTypes_ZMZN_closure'

/usr/bin/ld: /usr/local/lib/ghc-7.6.3/template-haskell-2.8.0.0//libHStemplate-haskell-2.8.0.0.a(Lib__124.o)(.text+0x14): unresolvable R_X86_64_32S relocation against symbol `ghczmprim_GHCziTypes_ZMZN_closure

and an executable 'main' is created, but when i try to run it, the following occurs:

host113@/home/tal/Documents/mfbus >> main
main: error while loading shared libraries: libHSbase-4.6.0.1-ghc7.6.3.so: cannot open shared object file: No such file or directory

i tried to include in the last rule (main) the directory of ' libHSbase-4.6.0.1-ghc7.6.3.so' in the '-l' option so that it will load it.but it doesnt seem to work. May someone have an insight to the error?

the code for Dep1.lhs:

> {-# LANGUAGE TemplateHaskell #-} <br/>
> {-# LANGUAGE ForeignFunctionInterface #-} <br/>

> module Dep1 where

> import Foreign <br/>
> import Foreign.C.Types <br/>
> import Dep3 <br/>

> data MyData = MyData
>    { foo :: String
>    , bar :: Int
>    }

> emptyShow ''MyData

> foreign export ccall some_func :: IO () <br/>
> foreign export ccall factorial :: Int -> Int

> some_func :: IO () <br/>
> some_func = print $ MyData { foo = "bar", bar = 5 }

> factorial :: Int -> Int <br/>
> factorial 0 = 1  <br/>
> factorial n = n *(factorial $ n - 1)    

the code for Dep3.lhs (comes here because Dep1.lhs imports it):

> {-# LANGUAGE TemplateHaskell, FlexibleInstances #-}

> module Dep3 where

> import Language.Haskell.TH

> emptyShow :: Name -> Q [Dec] <br/>
> emptyShow name = [d|instance Show $(conT name) where show _ = "some meaningful sentence"|]

the code for Dep2.lhs:

> {-# LANGUAGE ForeignFunctionInterface #-}

> module Dep2 where <br/>

> import Foreign    <br/>
> import Foreign.C.Types

> foreign export ccall power :: CInt -> CInt

> power :: CInt -> CInt
> power n = n*n

the code for Main.c:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <HsFFI.h>

#ifdef __GLASGOW_HASKELL__
#include "Tal2_stub.h"
#endif

#ifdef __GLASGOW_HASKELL__
extern void __stginit_power ( void );
#endif

// int power(int i){ return i*i; }

int fact(int i){
  if (i == 0) return 1;
  else return i * fact(i-1);
}
nt main(int argc, char *argv[]){
  hs_init(&argc, &argv);

#ifdef __GLASGOW_HASKELL__
  hs_add_root(__stginit_power);
#endif

 printf("what is 5!?\n");
 char buf[2048];
 scanf("%s",buf);
 int x = atoi(buf);
 if(x == fact(5)){
   printf("You're right!\n");
 } else {
   printf("You're wrong!\n");
 }
 printf("what is the power of 2?\n");
 scanf("%s",buf);
 x = atoi(buf);
 if(x == power(2)){
   printf("You're right!\n");
 } else {
   printf("You're wrong!\n");
 }
 hs_exit();
 return 0;
}

my makefile code:

all : clean main

main : shared main.o
        gcc -o main main.o -L/usr/local/lib/ghc-7.6.3 -L/usr/local/lib/ghc-7.6.3/template-haskell-2.8.0.0/ -L. -lShared -Wl,-rpath,/usr/local/lib/ghc-7.6.3 -L/home/tal/a_prerequisites/new_haskell/ghc-7.6.3/libraries/base/dist-install/build/libHSbase-4.6.0.1-ghc7.6.3. -lHStemplate-haskell-2.8.0.0 

main.o : 
    gcc -O2 -I/usr/local/lib/ghc-7.6.3/include -L/usr/local/lib/ghc-7.6.3 -L/usr/local/lib/ghc-7.6.3/template-haskell-2.8.0.0/ -c Main.c -o main.o 

shared : dep3second dep2second dep1second
        ghc -O2 -dynamic -shared -fPIC Dep1.dyn_o Dep2.dyn_o Dep3.dyn_o -o    libShared.so -lHSrts-ghc7.6.3

dep1second : dep1first
    ghc -c Dep1.lhs -dynamic -XTemplateHaskell -fPIC -no-hs-main -XForeignFunctionInterface -o Dep1.dyn_o -osuf dyn_o -hisuf dyn_hi   

dep2second : dep2first
    ghc -c Dep2.lhs -dynamic -XTemplateHaskell -fPIC -no-hs-main -XForeignFunctionInterface -o Dep2.dyn_o -osuf dyn_o -hisuf dyn_hi

dep3second: dep3first
    ghc -c Dep3.lhs -dynamic -XTemplateHaskell -fPIC -no-hs-main    -XForeignFunctionInterface -o Dep3.dyn_o -osuf dyn_o -hisuf dyn_hi

dep1first :
    ghc -c Dep1.lhs -XTemplateHaskell -XForeignFunctionInterface -o Dep1.o  

dep2first :
    ghc -c Dep2.lhs -XTemplateHaskell -XForeignFunctionInterface -o Dep2.o

dep3first :
    ghc -c Dep3.lhs -XTemplateHaskell -XForeignFunctionInterface -o Dep3.o

.PHONY : clean
clean :
    -rm -f *.o *.hi *.so *.dyn_hi *.dyn_o main
1 Answers
Related