zig icon indicating copy to clipboard operation
zig copied to clipboard

error: unable to create compilation: LibCRuntimeNotFound when cross-compiling

Open leleliu008 opened this issue 1 year ago • 7 comments

Zig Version

0.13.0-dev.46+3648d7df1

Steps to Reproduce and Observed Behavior

step1. export CC environment variable to cross compile

export CC="$HOME/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang"

step2. create a simple C source file test.c

#include <stdio.h>

int main() {
    puts("Hello zig!");
    return 0;
}

step3. build test.c for build machine

./zig cc test.c

my build machine target triple is x86_64-unknown-linux-gnu

this will report error: unable to create compilation: LibCRuntimeNotFound

Note: this is a common case that we need two compilers, one for native build, another one for target build, to distinguish these two, we usually use BUILD_CC or CC_FOR_BUILD to point to compiler for native build, use CC for target build, but zig doesn't distinguish these two.

Expected Behavior

zig could find it's own C runtime.

leleliu008 avatar Apr 28 '24 19:04 leleliu008

Maybe you guys could study the way of rust to handle for cross-compling https://docs.rs/cc/latest/cc/#external-configuration-via-environment-variables

leleliu008 avatar Apr 28 '24 19:04 leleliu008

Why are you using zig cc if you want to use a separate c compiler? zig cc does not call a separate c compiler, it is a c compiler.

silversquirl avatar Apr 28 '24 19:04 silversquirl

I want to use zig because it is only around 45MB, I want to use zig replace GCC or LLVM.

I build some packages for Android, some packages need two compilers, one for native build, another one for target build

leleliu008 avatar Apr 28 '24 19:04 leleliu008

That's to say, I want to use zig cc for native build and use Android NDK for target build in one build.

leleliu008 avatar Apr 28 '24 19:04 leleliu008

build native. For that you may use zig cc but do not set any env like CC or CXX. Workout any problems with a hello.c program. Difficult to guess why you get LibCRuntimeNotFound.

build for android. You'll need to do a couple of things to get this working. First, always use a target that specifies the android abi. For example -target aarch64-linux-android . This should be given to the zig cc command line. Second, you'll need to supply the head/lib and perhaps crt search dirs and right now I cannot remember the specifics on that. libc.txt may be required? If so, for zig cc usage you need to set ZIG_LIBC=libc.txt and edit that file's includes, crt and perhaps gcc_dir .

mikdusan avatar Apr 29 '24 00:04 mikdusan

@mikdusan Thanks for your comments. I don't use zig cc to build for Android, I just use it to build for native. I must use CC environment variable, because I am a package builder, the example above I give is just to reproduce it, not the real world case, the real world case not that simple. I noticed that zig prebuild tarball is very small, so I'm trying to use zig to replace GCC/LLVM and I encounter this problem.

leleliu008 avatar Apr 29 '24 08:04 leleliu008

If you belive that this is a feature not a bug, please feel free to close it.

leleliu008 avatar Apr 29 '24 08:04 leleliu008