Hello, and please be patient I struggled a bit with using swipl-ld, here are my observations.
Linux
On my Linux installation, with SWI-Prolog compiled from source and not “installed” (runs from the build directory), using swipl-ld works fine as advertised in the docs:
swipl-ld -o mylib file.{c,o,cc,C} ...
However, this will create mylib.so
, not mylib
. This is OK but I couldn’t then make the example few lines lower in the same docs work. The example says:
Now write a file
mylib.pl
::- module(mylib, [ say_hello/1 ]). :- use_foreign_library(foreign(mylib)).
But this won’t work for me. I had to use the fully qualified name of the foreign library, with the .so
at the end:
:- use_foreign_library(foreign('mylib.so')).
MacOS
On my (Intel, ~ 5 year old hardware) MacOS I failed to convince swipl-ld to work for me. I have compiled from source and installed in my $HOME:
cmake \
-DUSE_GMP=OFF \
-DUSE_TCMALLOC=OFF \
-DSWIPL_PACKAGES_JAVA=OFF \
-DSWIPL_PACKAGES_X=OFF \
-DCMAKE_INSTALL_PREFIX=$HOME \
-G Ninja ..
ninja
ninja install
(I am not sure about all the -D flags but this works now for development purposes)
No matter what I tried, using swipl-ld would end up saying that
ld: library '-lswipl' not found
for example:
$ swipl-ld -shared -L"$HOME"/lib/swipl/lib/x86_64-darwin -o swiplite -l sqlite3 swiplite.o
ld: library '-lswipl' not found
clang: error: linker command failed with exit code 1 (use -v to see invocation)
cc returned code 256
*** swipl-ld exit status 1
This however worked:
gcc -shared -L"$HOME"/lib/swipl/lib/x86_64-darwin -o swiplite -l sqlite3 -l swipl swiplite.o
What could have I possibly mis-configured?