Calling SML compiled with MLton from C #456
-
I want to have a C program calling SML (used as library). The example C code contains functions that call SML functions, What I want to do is: have C functions including the main(), calling the SML-functions. Is it possible at all, to call the output of MLton like a static or shared library from a C program (including main())? If it's possible: where can I find documentation on that? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 23 replies
-
Yes, it is possible to create an SML library that is called by a C program. The documentation is at http://mlton.org/LibrarySupport. The essential process is to create your SML library as an SML program that uses |
Beta Was this translation helpful? Give feedback.
-
I used
and got:
Then I used:
and got:
I have mlton 20210117 here. Is there somehere a working example that shows, that it's not only a hypothetical assumption that the compilation as lib should work? |
Beta Was this translation helpful? Give feedback.
-
Here is a simple, working example: mlton-lib-example.zip
Both |
Beta Was this translation helpful? Give feedback.
-
@MatthewFluet Thanks for the detailed answer! The new prepare() {
cd $pkgname
setconf Makefile.config RELEASE true
make MLTON_VERSION="$pkgver" version
}
build() {
make -C $pkgname
}
package() {
make -C $pkgname PREFIX=/usr DESTDIR="$pkgdir" install
for f in $pkgname/doc/license/*-LICENSE; do
install -Dm644 "$f" "$pkgdir/usr/share/licenses/$pkgname/$f"
done
} This works fine, but after building the package, the package checking utility
Is adding |
Beta Was this translation helpful? Give feedback.
-
Thanks to all who helped to fix the problem (mlton and Arch). |
Beta Was this translation helpful? Give feedback.
-
[Consolidating discussion to a single answer.] Yes, it is possible to create an SML library that is called by a C program. The documentation is at http://mlton.org/LibrarySupport. The essential process is to create your SML library as an SML program that uses Here is a simple, working example: mlton-lib-example.zip
If there are linking errors (e.g., |
Beta Was this translation helpful? Give feedback.
-
I am playing around with the provided sample code from this discussion(i.e. calling sml library from C (main). One thing I curious/confused about is what happens to the Mlton garbage collection. Is the Mlton GC not invoked in this scenario? |
Beta Was this translation helpful? Give feedback.
[Consolidating discussion to a single answer.]
Yes, it is possible to create an SML library that is called by a C program. The documentation is at http://mlton.org/LibrarySupport.
The essential process is to create your SML library as an SML program that uses
_export "func" public
as described in http://www.mlton.org/CallingFromCToSML; note thepublic
symbol scope to make the symbol visible by other DSOs. Compile that program with-export-header foo.h -format library -libname foo
(to create a dynamic shared library (i.e., a.so
)) or with-export-header foo.h -format archive -libname foo
(to create an archive (i.e., a.a
)). Then your C program should#include "foo.h"
(the header created wh…