zig icon indicating copy to clipboard operation
zig copied to clipboard

Broken .dll exported functions when libCpp is linked

Open tuket opened this issue 1 year ago • 0 comments

Zig Version

0.13.0-dev.30+6fd09f8d2

Steps to Reproduce and Observed Behavior

Please find attached a .zip with a minimum reproducible example. test.zip

I'm testing this in Windows 10.

In this project you will find a build.zig, and 3 different source files (root.zig, root.c, root.cpp).

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const lib = b.addSharedLibrary(.{
        .name = "test",
        //.root_source_file = b.path("src/root.zig"),
        .target = target,
        .optimize = optimize,
        .strip = true,
    });

    //if (false) {
    lib.addCSourceFile(.{
        .file = .{ .path = "src/root.c" },
        .flags = &.{"-std=c99"},
    });
    //} else {
    lib.addCSourceFile(.{
        .file = .{ .path = "src/root.cpp" },
        .flags = &.{"-std=c++11"},
    });
    //}

    lib.linkLibC();
    lib.linkLibCpp();

    b.installArtifact(lib);
}

If you compile the project as is (zig build cmd), and inspect the exported functions of the .dll, you will see the functions of the .c and .cpp source files correctly exported. I use DLL Export Viewer for this:

image

Now, if you un-comment line 9 of build.zig,

    const lib = b.addSharedLibrary(.{
        .name = "test",
        .root_source_file = b.path("src/root.zig"), // uncomment this
        .target = target,
        .optimize = optimize,
        .strip = true,
    });

Now all the C/C++ functions are not exported anymore. Only the .zig one is exported.

dllexp_XXMqzSznaR

I'm not sure if this intended behavior or not, I though I would just point it out beause it seems weird. But it's not the main issue I want to report.

Comment back line 9.

In root.cpp un-comment line 10

int foo(Data d)
{
    delete[] (char*)d.p; // un-comment this
    return 5;
}

This seems to force the use of libCpp and thus links it (the .dll becomes way bigger). As a result, all the C exported functions from our source files seem to disappear!

dllexp_yV2UOmWhq4

Expected Behavior

The functions of our C/C++ source files should be exported, regardless whether we are linking libCpp or not.

tuket avatar Apr 23 '24 07:04 tuket