ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.85k stars 2.55k forks source link

'undefined symbol: getcontext' when compiling for Android #17281

Open Jomy10 opened 1 year ago

Jomy10 commented 1 year ago

Zig Version

0.12.0-dev.575+fb6fff256

Steps to Reproduce and Observed Behavior

  1. Compile an zig library into a static library for Android (e.g. see the build.zig for my project: https://github.com/Jomy10/stadsspel/blob/master/app/common/build.zig)
    • The zig source file uses std.debug.print
  2. Link the library into a c/cpp project, e.g:
    $ANDROID_NDK_ROOT/.../x86_64-linux-anroid33-clang++ -shared libmy_zig_lib.a main.cpp.o # more flags...
  3. This will cause the linker to fail due to an undefined symbol getcontext:
    ld: error: undefined symbol: getcontext
    >>> referenced by debug.zig:721 (/Users/jonaseveraert/Documents/zig/lib/std/debug.zig:721)
    >>>               ../out/android-x86_64/debug/zig/cache/o/bba95f0acb1a2336ac375e4cf91c92f6/librender_objects.a.o:(debug.writeCurrentStackTrace__anon_8024) in archive ../out/android-x86_64/debug/lib/librender_objects.a
    clang-14: error: linker command failed with exit code 1 (use -v to see invocation)

The issue points to this line: https://github.com/ziglang/zig/blob/e7bf143b364f004a76e86cad5fd3256fa87761e4/lib/std/debug.zig#L726

Expected Behavior

Expected the linker to succeed without undefined symbols.

LinuxUserGD commented 1 year ago

getcontext is currently disabled when targeting android https://github.com/ziglang/zig/pull/17176

kcbanner commented 1 year ago

@Jomy10 can you try this patch to debug.zig?

diff --git a/lib/std/debug.zig b/lib/std/debug.zig
index 38a463171..6c4c6db82 100644
--- a/lib/std/debug.zig
+++ b/lib/std/debug.zig
@@ -169,6 +169,7 @@ pub fn relocateContext(context: *ThreadContext) void {

 pub const have_getcontext = @hasDecl(os.system, "getcontext") and
     builtin.os.tag != .openbsd and
+    !builtin.target.isAndroid() and
     (builtin.os.tag != .linux or switch (builtin.cpu.arch) {
     .x86,
     .x86_64