Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken .dll exported functions when libCpp is linked #19746

Open
tuket opened this issue Apr 23, 2024 · 0 comments
Open

Broken .dll exported functions when libCpp is linked #19746

tuket opened this issue Apr 23, 2024 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@tuket
Copy link

tuket commented Apr 23, 2024

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 tuket added the bug Observed behavior contradicts documented or intended behavior label Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

1 participant