[MLton] Re: MLton and shared libraries
Jens Axel Søgaard
jensaxel@soegaard.net
Tue, 26 Apr 2005 21:41:20 +0200
Hi all,
I am happy to report, that the first MLton generated shared library
now works with MzScheme.
ja# cat test.sml
val e = _export "foo": int -> int;
val _ = e (fn (i) => 42);
print "TESTING 1 2 3";
ja# cat test.ss
(require (lib "foreign.ss"))
(unsafe!)
(define lib (ffi-lib "/usr/soegaard/ml/source/mlton-20041109/soegaard/test"))
(define foo (get-ffi-obj "foo" lib (_fun _ulong -> _ulong)))
ja# ../build/bin/mlton -cc-opt "-shared -symbolic" -link-opt "-shared -Bsymbolic" -codegen c -default-ann 'allowExport
true' -keep g -verbose 1 -link-opt --verbose -link-opt -Wl,-Bsymbolic test.sml
...
ja# mzscheme -f test.ss Welcome to MzScheme version 299.102, Copyright (c) 2004-2005 PLT Scheme, Inc.
init_function> START
init_function> before real_Init
init_function> after real_Init
init_function> before trampoline
init_function> inside trampoline
TESTING 1 2 3
init_function> After main
> foo
#<primitive:ffi:foo>
> (foo 1)
42
There are two things to note:
1. It is important that both gcc and ld gets the -symbolic and -Bsymbolic
options respectively
2. The MLton option -link-opt doesn't pass the option directly to ld
but to gcc, which means that linker options is to be prefixed with
an explicit -Wl, .
I must admit that it took me a long time to figure 2 out (bear with me,
it is at least five years since I wrote my last C program).
However, I now feel confident that it is indeed possible to get MLton to generate
shared libraries that can be used with mzscheme.
The inputs from Weeks and Fluet helped tremendously.
I'll keep you posted on my further progress.
--
Jens Axel Søgaard