So I’m at work try to use zig to solve a very annoying problem which is that for one of the target that we have which is a raspberry pi zero 2w, running a custom version of debian which ships with a version of glibc that makes zig the perfect candidate for cross-compiling, I went through hell figuring out how to properly link everything, find where are all the headers, path, etc etc. But After all that trouble I’m stuck because I can’t find a way to tell zig to not link ‘-lc++’ and instead use the one in the system, because libcamera and opencv seems to be compiled with different symbols than the one in -lc++ anyway if anyone has some experience I’d be delighted.
const std = @import("std");
pub const gotowhite_directory: []const u8 = "src/fireforce";
pub const gotowhite_source: []const []const u8 = &.{
"gotowhite.c",
};
pub const gotowhite_flags: []const []const u8 = &.{
"-O3",
"-pipe",
"-ffast-math",
"-fstrict-aliasing",
"-ftree-vectorize",
"-ffunction-sections",
"-fdata-sections",
"-DNDEBUG",
"-D_GNU_SOURCE",
"-mcpu=cortex-a53",
"-mtune=cortex-a53",
"-mfpu=neon-vfpv4",
"-mfloat-abi=hard",
};
pub const camera_app_source: []const []const u8 = &.{
"src/fireforce/camera/CameraHandler.cpp",
"src/fireforce/camera/main.cpp",
"src/fireforce/INIReader.cpp",
};
pub const camera_app_flags: []const []const u8 = &.{
"-DBOOST_NO_CXX98_FUNCTION_BASE",
"-Wno-deprecated-builtins",
"-D_LIBCPP_DISABLE_MACROS",
"-D_GNU_SOURCE",
"-nostdinc++",
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const gotowhite = b.addExecutable(.{
.name = "gotowhite",
.root_module = b.createModule(.{
.root_source_file = null,
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
gotowhite.addCSourceFiles(.{
.root = b.path(gotowhite_directory),
.files = gotowhite_source,
.flags = gotowhite_flags,
.language = .c,
});
gotowhite.linkLibC();
b.installArtifact(gotowhite);
const camera_app = b.addExecutable(.{
.name = "camera_app",
.root_module = b.createModule(.{
.root_source_file = null,
.target = target,
.optimize = .ReleaseFast,
.link_libc = true,
.link_libcpp = false,
}),
});
camera_app.root_module.addSystemIncludePath(b.path("../../sysroot/usr/include/c++/10"));
camera_app.root_module.addSystemIncludePath(b.path("../../sysroot/usr/include/arm-linux-gnueabihf/c++/10"));
camera_app.root_module.addLibraryPath(b.path("../../sysroot/usr/lib/gcc/arm-linux-gnueabihf/10"));
camera_app.root_module.addLibraryPath(b.path("../../sysroot/usr/lib/arm-linux-gnueabihf"));
camera_app.root_module.linkSystemLibrary("stdc++", .{});
camera_app.root_module.linkSystemLibrary("m", .{});
camera_app.root_module.linkSystemLibrary("gcc_s", .{});
camera_app.root_module.linkSystemLibrary("gcc", .{});
camera_app.root_module.linkSystemLibrary("c", .{});
camera_app.root_module.linkSystemLibrary("dl", .{});
camera_app.root_module.addSystemIncludePath(b.path("../../sysroot/usr/include/opencv4"));
camera_app.root_module.linkSystemLibrary("opencv_core", .{});
camera_app.root_module.linkSystemLibrary("opencv_imgproc", .{});
camera_app.root_module.linkSystemLibrary("opencv_imgcodecs", .{});
camera_app.root_module.addSystemIncludePath(b.path("../../sysroot/usr/include/libcamera"));
camera_app.root_module.addSystemIncludePath(b.path("src/libcamera/libcamera-apps"));
camera_app.root_module.linkSystemLibrary("camera", .{});
camera_app.root_module.linkSystemLibrary("camera-base", .{});
camera_app.root_module.linkSystemLibrary("camera_app", .{});
camera_app.root_module.addSystemIncludePath(b.path("../../sysroot/usr/include"));
camera_app.root_module.linkSystemLibrary("boost_program_options", .{});
camera_app.addCSourceFiles(.{
.files = camera_app_source,
.flags = camera_app_flags,
});
b.installArtifact(camera_app);
}
and as you can see from the output
> zig build -Dtarget=arm-linux-gnueabihf.2.31 -Dcpu=cortex_a53
install
└─ install camera_app
└─ compile exe camera_app ReleaseFast arm-linux-gnueabihf.2.31 3 errors
error: ld.lld: undefined symbol: cv::imwrite(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, cv::_InputArray const&, std::__1::vector<int, std::__1::allocator<int>> const&)
note: referenced by CameraHandler.cpp:61 (src/fireforce/camera/CameraHandler.cpp:61)
note: .zig-cache/o/249a1f7f172f5f17e0ed3ff2edc84a46/CameraHandler.o:(CameraHandlerMt::save_image_atomic(cv::Mat const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&))
error: ld.lld: undefined symbol: cv::putText(cv::_InputOutputArray const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)
note: referenced by CameraHandler.cpp:421 (src/fireforce/camera/CameraHandler.cpp:421)
note: .zig-cache/o/249a1f7f172f5f17e0ed3ff2edc84a46/CameraHandler.o:(CameraHandlerMt::cameraConsumerFn(int))
error: ld.lld: undefined symbol: LibcameraApp::LibcameraApp(std::__1::unique_ptr<Options, std::__1::default_delete<Options>>)
note: referenced by CameraHandler.cpp:167 (src/fireforce/camera/CameraHandler.cpp:167)
note: .zig-cache/o/249a1f7f172f5f17e0ed3ff2edc84a46/CameraHandler.o:(CameraHandlerMt::cameraProducerFn())
error: the following command failed with 3 compilation errors:
/home/pollivie/.local/opt/zig-v0.15.1/zig build-exe -lgcc_s -lgcc -lopencv_core -lopencv_imgproc -lopencv_imgcodecs -lcamera -lcamera-base -lcamera_app -lboost_program_options -cflags -DBOOST_NO_CXX98_FUNCTION_BASE -Wno-deprecated-builtins -D_LIBCPP_DISABLE_MACROS -D_GNU_SOURCE -nostdinc++ -- /home/pollivie/workspace/cross_comp/src/fireforce/camera/CameraHandler.cpp /home/pollivie/workspace/cross_comp/src/fireforce/camera/main.cpp /home/pollivie/workspace/cross_comp/src/fireforce/INIReader.cpp -OReleaseFast -target arm-linux-gnueabihf.2.31 -mcpu cortex_a53 -isystem /home/pollivie/workspace/cross_comp/../../sysroot/usr/include/c++/10 -isystem /home/pollivie/workspace/cross_comp/../../sysroot/usr/include/arm-linux-gnueabihf/c++/10 -isystem /home/pollivie/workspace/cross_comp/../../sysroot/usr/include/opencv4 -isystem /home/pollivie/workspace/cross_comp/../../sysroot/usr/include/libcamera -isystem /home/pollivie/workspace/cross_comp/src/libcamera/libcamera-apps -isystem /home/pollivie/workspace/cross_comp/../../sysroot/usr/include -L /home/pollivie/sysroot/usr/lib/gcc/arm-linux-gnueabihf/10 -L /home/pollivie/sysroot/usr/lib/arm-linux-gnueabihf -Mroot -lc++ -lc --cache-dir .zig-cache --global-cache-dir /home/pollivie/.cache/zig --name camera_app --zig-lib-dir /home/pollivie/.local/opt/zig-v0.15.1/lib/ --listen=-
It’s magically adding -lc++