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

zig build with c library cannot reference windows api in target i386-windows-gnu #6479

Closed
codehz opened this issue Oct 1, 2020 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior os-windows stage1 The process of building from source via WebAssembly and the C backend.

Comments

@codehz
Copy link
Contributor

codehz commented Oct 1, 2020

(and works in x86_64-windows-gnu/msvc)

the error is

lld: error: undefined symbol: __declspec(dllimport) _RegisterWindowMessageW@4
>>> referenced by vendor\iup\src\win\iupwin_loop.c:34
>>>               libiup.a(iupwin_loop.o):(_iupwinSetCustomQuitMessage)
>>> referenced by vendor\iup\src\win\iupwin_common.c:863
>>>               libiup.a(iupwin_common.o):(_iupdrvBaseRegisterCommonAttrib)
>>> referenced by vendor\iup\src\win\iupwin_dialog.c:2085
>>>               libiup.a(iupwin_dialog.o):(_iupdrvDialogInitClass)

lld: error: undefined symbol: __declspec(dllimport) _PostQuitMessage@4
>>> referenced by vendor\iup\src\win\iupwin_loop.c:63
>>>               libiup.a(iupwin_loop.o):(_IupExitLoop)

lld: error: undefined symbol: __declspec(dllimport) _PostMessageW@16
>>> referenced by vendor\iup\src\win\iupwin_loop.c:65
>>>               libiup.a(iupwin_loop.o):(_IupExitLoop)
>>> referenced by vendor\iup\src\win\iupwin_text.c:0
>>>               libiup.a(iupwin_text.o):(_winTextMsgProc)
>>> referenced by vendor\iup\src\win\iupwin_text.c:1876
>>>               libiup.a(iupwin_text.o):(_winTextMsgProc)
>>> referenced by vendor\iup\src\win\iupwin_text.c:1916
>>>               libiup.a(iupwin_text.o):(_winTextMsgProc)
>>> referenced by vendor\iup\src\win\iupwin_dialog.c:781
>>>               libiup.a(iupwin_dialog.o):(_winDialogBaseProc)
>>> referenced by vendor\iup\src\win\iupwin_dialog.c:370
>>>               libiup.a(iupwin_dialog.o):(_winDialogMDIRefreshMenu)
>>> referenced by vendor\iup\src\win\iupwin_menu.c:279
>>>               libiup.a(iupwin_menu.o):(_iupdrvMenuPopup)
>>> referenced by vendor\iup\src\win\iupwin_list.c:1479
>>>               libiup.a(iupwin_list.o):(_winListEditProc)
>>> referenced by vendor\iup\src\win\iupwin_list.c:1428
>>>               libiup.a(iupwin_list.o):(_winListEditProc)
>>> referenced by vendor\iup\src\win\iupwin_list.c:1418
>>>               libiup.a(iupwin_list.o):(_winListEditProc)
>>> referenced 2 more times
(and more)

basically all windows apis referenced in c are failed to link...

but once I put my own testing c code, everything is working, so I can only repro this issue with iup library

background

I want to use iup library with zig, but I cannot use it binary release, due to some compatibility problems(yes, it doesn't even works in any windows target), so I decided to use source.

it is my build.zig
make sure iup's (3.30) source has been extracted in vendor/iup folder

const std = @import("std");
const Builder = std.build.Builder;

const IUPConfig = struct {
    target: std.zig.CrossTarget,
    name: []const u8,
    source: []const []const u8,
    includes: []const []const u8,
    defines: []const []const u8,

    fn getLibName(comptime self: @This()) []const u8 {
        return "iup" ++ self.name;
    }

    fn getBasePath(comptime self: @This()) []const u8 {
        return "vendor/iup/src" ++ self.name;
    }

    fn getArgs(comptime self: @This()) [self.defines.len + 1][]const u8 {
        var ret: [self.defines.len + 1][]const u8 = undefined;
        ret[0] = "-Wno-everything";
        inline for (self.defines) |def, i| {
            ret[i + 1] = "-D" ++ def;
        }
        return ret;
    }
};

fn buildIUPLib(b: *Builder, comptime info: IUPConfig) !*std.build.LibExeObjStep {
    const ret = b.addStaticLibrary(info.getLibName(), null);
    ret.setTarget(info.target);
    ret.addIncludeDir("vendor/iup/include");
    ret.linkLibC();
    comptime const basepath = info.getBasePath();
    ret.addIncludeDir(basepath);
    inline for (info.source) |rel| {
        const args = info.getArgs();
        ret.addCSourceFile(basepath ++ "/" ++ rel, &args);
    }
    inline for (info.includes) |rel| {
        ret.addIncludeDir(basepath ++ "/" ++ rel);
    }
    return ret;
}
pub fn build(b: *Builder) void {
	const target = b.standardTargetOptions(.{});

	const mode = b.standardReleaseOptions();

	// copy from makefile
    const iup = try buildIUPLib(b, .{
        .name = "",
        .target = target,
        .includes = &[_][]const u8{
            "win", "win/wdl",
        },
        .source = &[_][]const u8{
            "iup_array.c", "iup_callback.c", "iup_dlglist.c", "iup_attrib.c", "iup_focus.c", "iup_font.c", "iup_globalattrib.c", "iup_object.c", "iup_key.c", "iup_layout.c", "iup_ledlex.c", "iup_names.c", "iup_ledparse.c", "iup_predialogs.c", "iup_register.c", "iup_scanf.c", "iup_show.c", "iup_str.c", "iup_func.c", "iup_childtree.c", "iup.c", "iup_classattrib.c", "iup_dialog.c", "iup_assert.c", "iup_messagedlg.c", "iup_timer.c", "iup_image.c", "iup_label.c", "iup_fill.c", "iup_zbox.c", "iup_colordlg.c", "iup_fontdlg.c", "iup_filedlg.c", "iup_strmessage.c", "iup_menu.c", "iup_frame.c", "iup_user.c", "iup_button.c", "iup_radio.c", "iup_toggle.c", "iup_progressbar.c", "iup_text.c", "iup_val.c", "iup_box.c", "iup_hbox.c", "iup_vbox.c", "iup_cbox.c", "iup_class.c", "iup_classbase.c", "iup_maskmatch.c", "iup_mask.c", "iup_maskparse.c", "iup_tabs.c", "iup_spin.c", "iup_list.c", "iup_getparam.c", "iup_link.c", "iup_sbox.c", "iup_scrollbox.c", "iup_normalizer.c", "iup_tree.c", "iup_split.c", "iup_layoutdlg.c", "iup_recplay.c", "iup_progressdlg.c", "iup_expander.c", "iup_open.c", "iup_table.c", "iup_canvas.c", "iup_gridbox.c", "iup_detachbox.c", "iup_backgroundbox.c", "iup_linefile.c", "iup_config.c", "iup_flatbutton.c", "iup_animatedlabel.c", "iup_draw.c", "iup_flatframe.c", "iup_flattabs.c", "iup_flatscrollbar.c", "iup_flatscrollbox.c", "iup_gauge.c", "iup_dial.c", "iup_colorbar.c", "iup_colorbrowser.c", "iup_colorhsi.c", "iup_flatlabel.c", "iup_dropbutton.c", "iup_flattoggle.c", "iup_flatseparator.c", "iup_space.c", "iup_multibox.c", "iup_flatlist.c", "iup_globalsdlg.c", "iup_flatval.c", "iup_loop.c", "iup_thread.c", "iup_classinfo.c", "iup_elempropdlg.c", "iup_export.c", "iup_flattree.c",
            "win/iupwin_common.c", "win/iupwin_brush.c", "win/iupwin_focus.c", "win/iupwin_font.c", "win/iupwin_globalattrib.c", "win/iupwin_handle.c", "win/iupwin_key.c", "win/iupwin_str.c", "win/iupwin_loop.c", "win/iupwin_open.c", "win/iupwin_tips.c", "win/iupwin_info.c", "win/iupwin_dialog.c", "win/iupwin_messagedlg.c", "win/iupwin_timer.c", "win/iupwin_image.c", "win/iupwin_label.c", "win/iupwin_canvas.c", "win/iupwin_frame.c", "win/iupwin_fontdlg.c", "win/iupwin_filedlg.c", "win/iupwin_dragdrop.c", "win/iupwin_button.c", "win/iupwin_draw.c", "win/iupwin_toggle.c", "win/iupwin_clipboard.c", "win/iupwin_progressbar.c", "win/iupwin_text.c", "win/iupwin_val.c", "win/iupwin_touch.c", "win/iupwin_tabs.c", "win/iupwin_menu.c", "win/iupwin_list.c", "win/iupwin_tree.c", "win/iupwin_calendar.c", "win/iupwin_datepick.c",
            "win/iupwin_draw_wdl.c", "win/iupwin_draw_gdi.c", "win/iupwin_image_wdl.c", "win/wdl/backend-d2d.c", "win/wdl/backend-dwrite.c", "win/wdl/backend-gdix.c", "win/wdl/backend-wic.c", "win/wdl/bitblt.c", "win/wdl/brush.c", "win/wdl/cachedimage.c", "win/wdl/canvas.c", "win/wdl/draw.c", "win/wdl/fill.c", "win/wdl/font.c", "win/wdl/image.c", "win/wdl/init.c", "win/wdl/memstream.c", "win/wdl/misc.c", "win/wdl/path.c", "win/wdl/string.c", "win/wdl/strokestyle.c",
            "win/iupwindows_main.c", "win/iupwindows_help.c", "win/iupwindows_info.c",
        },
        .defines = &[_][]const u8{
            "UNICODE", "_WIN32_WINNT=0x0601", "_WIN32_IE=0x0900", "WINVER=0x0601", "NOTREEVIEW", "COBJMACROS", "_UNICODE", "USE_NEW_DRAW",
        },
    });

	const exe = b.addExecutable("ndslist", "src/main.zig");
    exe.addSystemIncludeDir("vendor/iup/include");
    exe.linkLibC();
    exe.linkSystemLibraryName("gdi32");
    exe.linkSystemLibraryName("uuid");
    exe.linkSystemLibraryName("ole32");
    exe.linkSystemLibraryName("winspool");
    exe.linkSystemLibraryName("comdlg32");
    exe.linkSystemLibraryName("advapi32");
    exe.linkSystemLibraryName("user32");
    exe.linkSystemLibraryName("ole32");
    exe.linkSystemLibraryName("shell32");
    exe.linkSystemLibraryName("comctl32");
    exe.linkLibrary(iup);
    exe.setTarget(target);
    exe.setBuildMode(mode);
    exe.install();
}

the testing code:

const std = @import("std");
const iup = @cImport({
    @cInclude("iup.h");
});

pub fn main() anyerror!void {
    std.log.info("version: {}", .{iup.IupVersion()});
}
@LemonBoy
Copy link
Contributor

LemonBoy commented Oct 1, 2020

This should be easy to fix, drop this file in lib/libc/mingw/lib32/ and it should compile just fine (send a PR if it does).

@alexnask alexnask added bug Observed behavior contradicts documented or intended behavior os-windows stage1 The process of building from source via WebAssembly and the C backend. labels Oct 1, 2020
@codehz
Copy link
Contributor Author

codehz commented Oct 1, 2020

This should be easy to fix, drop this file in lib/libc/mingw/lib32/ and it should compile just fine (send a PR if it does).

yes, it fix most of error...

but

lld: error: <root>: undefined symbol: _DllMainCRTStartup
lld: error: undefined symbol: __stack_chk_fail
lld: error: undefined symbol: __stack_chk_guard
lld: error: undefined symbol: sin
...

seems like crt symbols?

PS: there no dynamic library in the build script..

(maybe related to #6250

yes, I back to 58ee5f4 now it is working

@alexnask now the problem is related to stage-2 it break again in latest zig...

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 os-windows stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

3 participants