Add rules for statically linking the standard library in
authorLuke Lau <luke_lau@icloud.com>
Sat, 9 Nov 2019 18:15:38 +0000 (18:15 +0000)
committerLuke Lau <luke_lau@icloud.com>
Sat, 9 Nov 2019 18:28:48 +0000 (18:28 +0000)
If statically linking in the Makefile, we need to make sure that our
putchard symbol/function doesn't get stripped out, as GHC passes the
-dead_strip flag to the linker to remove any unused functions. Since
nothing directly calls putchard in our program, it will get stripped out
unless we specify that we want to export it. We can use an export list
to indicate that we still want the symbol. (On Linux, you might need to
remove the underscore from _putchard. )

.gitignore
Makefile
stdlib.syms [new file with mode: 0644]

index 699f4dd7e2c82f0d6b127c0e1cd8566cb1c969d4..38b1613684f1f9145ffb660e1eb0a7769a5d3d37 100644 (file)
@@ -1 +1,3 @@
 stdlib.dylib
+stdlib.o
+Main
index 4e533dcd9d103d5afe98c54f6eb4e2206c23ffcc..1a37353a226b8671148a1e95db3fd049c37a9dc4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,2 +1,14 @@
 stdlib.dylib: stdlib.c
        clang -shared $< -o $@
+
+# for statically linking the stdlib:
+# make sure to change in Main.hs
+#   loadLibraryPermanently (Just "stdlib.dylib")
+# to
+#   loadLibraryPermanently Nothing
+stdlib.o: stdlib.c
+       clang -c $< -o $@
+
+Main: Main.hs stdlib.o
+       ghc $^ -o $@ -optl -Wl,-exported_symbols_list,stdlib.syms \
+               -no-keep-hi-files -no-keep-o-files
diff --git a/stdlib.syms b/stdlib.syms
new file mode 100644 (file)
index 0000000..94f22f1
--- /dev/null
@@ -0,0 +1 @@
+_putchard