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

order of calling extern function affects linking? #29

Open
expikr opened this issue Jun 6, 2024 · 1 comment
Open

order of calling extern function affects linking? #29

expikr opened this issue Jun 6, 2024 · 1 comment

Comments

@expikr
Copy link

expikr commented Jun 6, 2024

This does not work:

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

pub fn main() void {
    //_ = MessageBoxA(null, "world!", "Hello", 0);
    _ = @extern(
        *const fn(?win.HWND, [*:0]const u8, [*:0]const u8, u32) callconv(win.WINAPI) i32, 
        .{.name="MessageBoxA", .library_name="user32"}
    )(null, "world!", "Hello", 0);
}

// extern "user32" fn MessageBoxA(?win.HWND, [*:0]const u8, [*:0]const u8, u32) callconv(win.WINAPI) i32;
LLD Link... error(link): DLL import library for -l    ╕Φ not found
error: DllImportLibraryNotFound

However, calling the commented function before the @extern symbol works

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

pub fn main() void {
    _ = MessageBoxA(null, "world!", "Hello", 0);
    _ = @extern(
        *const fn(?win.HWND, [*:0]const u8, [*:0]const u8, u32) callconv(win.WINAPI) i32, 
        .{.name="MessageBoxA", .library_name="user32"}
    )(null, "world!", "Hello", 0);
}

extern "user32" fn MessageBoxA(?win.HWND, [*:0]const u8, [*:0]const u8, u32) callconv(win.WINAPI) i32;

But if you swap their order, it doesn't work again:

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

pub fn main() void {
    _ = @extern(
        *const fn(?win.HWND, [*:0]const u8, [*:0]const u8, u32) callconv(win.WINAPI) i32, 
        .{.name="MessageBoxA", .library_name="user32"}
    )(null, "world!", "Hello", 0);
    _ = MessageBoxA(null, "world!", "Hello", 0);
}

extern "user32" fn MessageBoxA(?win.HWND, [*:0]const u8, [*:0]const u8, u32) callconv(win.WINAPI) i32;
LLD Link... error(link): DLL import library for -l    Xv not found
error: DllImportLibraryNotFound
@nektro
Copy link
Owner

nektro commented Jun 6, 2024

given error(link): DLL import library for -l ╕Φ not found
and error(link): DLL import library for -l Xv not found
it looks like there's some memory corruption going on with regards to the .library_name argument to @export.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants