For myFunc, the returned slice will be allocated with the allocator. This means it will be trivial for Zig users to work with this function (i.e. they can free the slice, reallocate a bigger slice, etc.)
However, I’m not sure how to expose this same function signature for a C user. My first thought was to wrap this function in some sort of exported code that could pass in an allocator that would imitate malloc. That way, C users could use their respective functions (free, realloc, etc):
In order to compile this code, I add a hand-written header file:
extern char *myCFunc ();
When I compile a demo executable with the resulting library, I get this warning:
gcc demo-anyline.c -o demo-anyline libanyline.a
/usr/bin/ld: warning: root.o: missing .note.GNU-stack section implies executable stack
/usr/bin/ld: NOTE: This behaviour is deprecated and will be removed in a future version of the linker
When I run the executable finishes running, it terminates with this error message:
fish: Job 1, './demo-anyline' terminated by signal SIGSEGV (Address boundary error)
As a sanity check, I found a working library and linked it’s respective archive (.a) and header files into my executable and it terminates just fine.
I think you also need to specify that you want glibc and not musl to Zig. You can do that by defining the target (eg x86_64-linux-gnu or native-linux-gnu).
Oh, if that’s the case then it would certainly explain why it’s working on my system. I was under the impression that the default was the system libc though, unless otherwise specified.
That’s correct - the default is system libc if no target is specified.
However, the moment you specify a target, the host system is not relevant anymore - you are cross compiling. In such case the default libc for Linux is static musl.
Interesting… I copied your example and sure enough, it doesn’t segfault anymore. When I go back to my actual static library, even with -Dtarget=x86_64-linux-gnu, I still get the segfault.
I must be missing a subtlety. I’ll start a new thread instead of continue to pollute this one. Thanks for the knowledge, everyone!