linking ghc object files with mingw-g++
Tue, 27 May 2003 23:10:55 +0200


I'm trying to link ghc object files with mingw's g++/ld...
The following shows that linking ghc code with C code can be done very

$ cat > Test.hs
module Test where
foreign export ccall test :: IO ()
test = putStr "Hello from Haskell World!\n"

$ cat > main.c
#include "RtsApi.h"
#include "Test_stub.h"
#ifdef __cplusplus
extern "C"
void __stginit_Test(void);
int main(int argc, char *argv[]) {
startupHaskell(argc, argv, __stginit_Test);

$ ghc -ffi -c Test.hs -o Test.o
$ gcc -c Test_stub.c -Ic:/ghc/ghc-5.04.3/include
$ gcc -c main.c -Ic:/ghc/ghc-5.04.3/include
$ ghc -no-hs-main main.o Test.o Test_stub.o
$ ./a.out
Hello from Haskell World!

The procedure above works well with gcc; g++ object files seem to depend on
a rt-lib or something, which ghc does not link into the executable by

$ g++ -c main.cpp -Ic:/ghc/ghc-5.04.3/include
$ ghc -no-hs-main main.o Test.o Test_stub.o
main.o(.text+0x22):main.cpp: undefined reference to `__stginit_Test()'
main.o(.eh_frame+0x11):main.cpp: undefined reference to

The other way round, linking the files with gcc/ld, using

$ gcc main.c Test_stub.c -Ic:/ghc/ghc-5.04.3/include \
c:/ghc/ghc-5.04.3/HSbase1.o \
c:/ghc/ghc-5.04.3/HSbase2.o \
c:/ghc/ghc-5.04.3/HSbase3.o \
c:/ghc/ghc-5.04.3/HSbase_cbits.o \
c:/ghc/ghc-5.04.3/HStext.o \
c:/ghc/ghc-5.04.3/HStext_cbits.o \
c:/ghc/ghc-5.04.3/HSlang.o \
c:/ghc/ghc-5.04.3/HSlang_cbits.o \
c:/ghc/ghc-5.04.3/HShaskell98.o \
c:/ghc/ghc-5.04.3/HSrts.o \
-lgmp Test.o -lwsock32 -lgmp -lwinmm

results in

c:/ghc/ghc-5.04.3/HSrts.o(.text+0x9ff8): multiple definition of `main'
c:\DOKUME~1\jgoetz\LOKALE~1\Temp/ccWKaaaa.o(.text+0x0):main.c: first defined
c:/ghc/ghc-5.04.3/HSrts.o(.text+0xa00e): undefined reference to
c:/ghc/ghc-5.04.3/HSrts.o(.text+0xa01f): undefined reference to

I'm stuck...
Is there a way to merge ghc code with c++ code? What am I doing wrong?
Do you think it's possible to link ghc object code with Metrowerks and MSVC?

btw, I'm using ghc-5.04.3, windows binary distribution and current mingw g++