But when I run swipl-ld -o mylib file.c, I get the following fatal error:
Undefined symbols for architecture arm64:
"_main", referenced from:
<initial-undefines>
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
cc returned code 256
*** swipl-ld exit status 1
This is the output of swipl --dump-runtime-variables:
I was just dealing with something possibly related to this the other day. I was not using swipl-ld but rather specifying target_link_libraries in my CMake file. I had tried to install my Prolog application on an Apple laptop and the linker was giving me this same grief about “symbols(s) not found for architecture…”
There’s a lot of stuff that automatically looks through certain directories looking for the dynamic linking files, which is convenient but it can also make it a little confusing when figuring out why something works on one system but doesn’t work on another. I think that maybe on my local Debian GNU/Linux system I had already built SWI-Prolog from source with statically compiled extensions? So this was probably why my code combined just fine on my machine but not on the Apple laptop. You may be having the same issue; to resolve it you probably have to do:
Make sure you actually have the dynamic linking binaries (“dylib” or whatever they are called on MacOS)
Also I don’t know if .so is going to be your extension on MacOS, I think it should be .dylib? Also, PLSOPATH should probably resolve to an actual path? I don’t know a lot about MacOS though.
Updated the example to work on all platforms and fixed several details. Runs fine for me using the installed 9.2.8 release.
P.s. #include <SWI-Prolog.h> is enough. swipl-ld adds the required -I flag to make the compiler find this file. You can add the -v flag to make swipl-ld print the commands it executes.