To build to android, we must set libc file. But seems libc file is useless for c++ headers on 0.14.0. 0.13.0 was ok.
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const lib = b.addLibrary(.{
.name = "testso",
.linkage = .dynamic,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
}),
});
lib.linkLibC();
const system_target = getAndroidTriple(lib.rootModuleTarget()) catch |err| @panic(@errorName(err));
const android_api_version: u32 = 34;
const libc = createLibC(b, system_target, android_api_version, "E:\\SDKs\\AndroidSDK\\ndk\\27.0.12077973\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot", "27.0.12077973");
lib.setLibCFile(libc);
lib.root_module.addCSourceFile(.{ .file = b.path("test.cpp") });
b.installArtifact(lib);
}
fn getAndroidTriple(target: std.Target) error{InvalidAndroidTarget}![]const u8 {
if (target.abi != .android) return error.InvalidAndroidTarget;
return switch (target.cpu.arch) {
.x86 => "i686-linux-android",
.x86_64 => "x86_64-linux-android",
.arm => "arm-linux-androideabi",
.aarch64 => "aarch64-linux-android",
.riscv64 => "riscv64-linux-android",
else => error.InvalidAndroidTarget,
};
}
fn createLibC(b: *std.Build, system_target: []const u8, android_api_version: u32, ndk_sysroot_path: []const u8, ndk_version: []const u8) std.Build.LazyPath {
const libc_file_format =
\\# Generated by zig-android-sdk. DO NOT EDIT.
\\
\\# The directory that contains `stdlib.h`.
\\# On POSIX-like systems, include directories be found with: `cc -E -Wp,-v -xc /dev/null`
\\include_dir={[include_dir]s}
\\
\\# The system-specific include directory. May be the same as `include_dir`.
\\# On Windows it's the directory that includes `vcruntime.h`.
\\# On POSIX it's the directory that includes `sys/errno.h`.
\\sys_include_dir={[sys_include_dir]s}
\\
\\# The directory that contains `crt1.o`.
\\# On POSIX, can be found with `cc -print-file-name=crt1.o`.
\\# Not needed when targeting MacOS.
\\crt_dir={[crt_dir]s}
\\
\\# The directory that contains `vcruntime.lib`.
\\# Only needed when targeting MSVC on Windows.
\\msvc_lib_dir=
\\
\\# The directory that contains `kernel32.lib`.
\\# Only needed when targeting MSVC on Windows.
\\kernel32_lib_dir=
\\
\\gcc_dir=
;
const include_dir = b.fmt("{s}/usr/include", .{ndk_sysroot_path});
const sys_include_dir = b.fmt("{s}/usr/include/{s}", .{ ndk_sysroot_path, system_target });
const crt_dir = b.fmt("{s}/usr/lib/{s}/{d}", .{ ndk_sysroot_path, system_target, android_api_version });
const libc_file_contents = b.fmt(libc_file_format, .{
.include_dir = include_dir,
.sys_include_dir = sys_include_dir,
.crt_dir = crt_dir,
});
const filename = b.fmt("android-libc_target-{s}_version-{d}_ndk-{s}.conf", .{ system_target, android_api_version, if (ndk_version.len > 0) ndk_version else "unknown" });
const write_file = b.addWriteFiles();
const android_libc_path = write_file.add(filename, libc_file_contents);
return android_libc_path;
}
#include <string.h>
#include <stdio.h>
#include <iostream>
int func1()
{
printf("hello world!");
std::cout << "hello world" << std::endl;
return 0;
}
F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\src/filesystem/directory_iterator.cpp:12:10: note: in file included from F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\src/filesystem
/directory_iterator.cpp:12:
#include <filesystem>
^
F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/filesystem:540:12: note: in file included from F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/filesystem:540:
# include <__filesystem/directory_entry.h>
^
F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/__filesystem/directory_entry.h:19:10: note: in file included from F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/__fil
esystem/directory_entry.h:19:
#include <__filesystem/filesystem_error.h>
^
F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/__filesystem/filesystem_error.h:14:10: note: in file included from F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/__fi
lesystem/filesystem_error.h:14:
#include <__filesystem/path.h>
^
F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/__filesystem/path.h:29:12: note: in file included from F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/__filesystem/pat
h.h:29:
# include <iomanip> // for quoted
^
F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/iomanip:46:10: note: in file included from F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/iomanip:46:
#include <istream>
^
F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/istream:164:10: note: in file included from F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/istream:164:
#include <__ostream/basic_ostream.h>
^
F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/__ostream/basic_ostream.h:24:10: note: in file included from F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/__ostream/
basic_ostream.h:24:
#include <ios>
^
F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/ios:220:12: note: in file included from F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/ios:220:
# include <__locale>
^
F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/__locale:14:10: note: in file included from F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/__locale:14:
#include <__locale_dir/locale_base_api.h>
^
F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/__locale_dir/locale_base_api.h:17:12: note: in file included from F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/__loc
ale_dir/locale_base_api.h:17:
# include <__locale_dir/locale_base_api/android.h>
^
F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\include/__locale_dir/locale_base_api/android.h:22:12: note: in file included from F:\code2\tools\zig\zig-windows-x86_64-0.14.0\lib\libcxx\inclu
de/__locale_dir/locale_base_api/android.h:22:
# include <__support/xlocale/__posix_l_fallback.h>
The compiler did not even use the libc file I set up.
Zig Version
0.14.0
Steps to Reproduce and Observed Behavior
To build to android, we must set libc file. But seems libc file is useless for c++ headers on 0.14.0. 0.13.0 was ok.
Codes looks like: https://ziggit.dev/t/cant-build-to-android/9158/4
build.zig
test.cpp
Because
--libc [file]does not work, so I generate libc file dynamic.Compiler report like these:
The compiler did not even use the libc file I set up.
Expected Behavior
Build success