What is the exact semantic of export

Hi guys,
In my understanding, export is used to expose a Zig function to the c/c++ code, this works fine in case of executable but in case of a dll the export keyword will also export the function to the dll. This may not be what wanted in some scenario. For example,

const std = @import("std");
const win = std.os.windows;

const reason = enum(u8) {
    DLL_PROCESS_DETACH = 0,
    DLL_PROCESS_ATTACH = 1,
    DLL_THREAD_ATTACH = 2,
    DLL_THREAD_DETACH = 3,
};
// the mingW DllMainCRTStartup
extern "C" fn DllMainCRTStartup(inst: win.HINSTANCE, dllreason: win.DWORD, reserved: win.LPVOID) callconv(.c) win.BOOL; 

pub export fn _DllMainCRTStartup(inst: win.HINSTANCE, dllreason: win.DWORD, reserved: win.LPVOID) callconv(.c) win.BOOL {
    return DllMainCRTStartup(inst, dllreason, reserved);
}
// export to be called by mingW DllMainCRTStartup
pub export fn DllMain(_: win.HINSTANCE, _: win.DWORD, _: win.LPVOID) callconv(.winapi) win.BOOL {
    return .TRUE;
}

DllMain is exported to the final dll, which is not what wanted.

That is the point of export, if you don’t want it exported then don’t export it

1 Like

If I do not add the export modifier to DllMain, it will not be called by mingW DllMainCRTStartup. Is there any other way?

found a workaround : move the code of DllMain into _DllMainCRTStartup then there is no need for DllMain

the final solution

const std = @import("std");
const win = std.os.windows;

extern "C" fn DllMainCRTStartup(handle: win.HINSTANCE, fdwReason: win.DWORD, reserved: win.LPVOID) callconv(.c) win.BOOL;

pub fn DllMain(handle: win.HINSTANCE, fdwReason: win.DWORD, reserved: win.LPVOID) win.BOOL {
    // call DllMainCRTStartup to initialize C runtime
    // because the dll may link to c/c++ code
    return DllMainCRTStartup(handle, fdwReason, reserved);
}

This is effectively the same, its just that zig is exporting _DllMainCRTStart for you, which is calling your DllMain.

Sorry, I assumed you wanted to do the exporting yourself since I mentioned this in your other thread.