Skip to content

Update zig.lua#7406

Closed
Miou-zora wants to merge 1 commit intoxmake-io:devfrom
Miou-zora:dev
Closed

Update zig.lua#7406
Miou-zora wants to merge 1 commit intoxmake-io:devfrom
Miou-zora:dev

Conversation

@Miou-zora
Copy link

  • Before adding new features and new modules, please go to issues to submit the relevant feature description first.
  • Write good commit messages and use the same coding conventions as the rest of the project.
  • Please commit code to dev branch and we will merge into master branch in feature
  • Ensure your edited codes with four spaces instead of TAB.

  • 增加新特性和新模块之前,请先到issues提交相关特性说明,经过讨论评估确认后,再进行相应的代码提交,避免做无用工作。
  • 编写友好可读的提交信息,并使用与工程代码相同的代码规范,代码请用4个空格字符代替tab缩进。
  • 请提交代码到dev分支,如果通过,我们会在特定时间合并到master分支上。
  • 为了规范化提交日志的格式,commit消息,不要用中文,请用英文描述。

Fix #7395

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the Xmake build system's integration with the Zig programming language, specifically improving the linking process for shared libraries on Windows. It introduces a refined mechanism to correctly identify and include necessary object and source files during the linking phase, resolving a known issue and ensuring more robust build outcomes for Zig projects.

Highlights

  • New Link Input File Handling: Introduced a new local function _linkfiles to specifically manage and filter object files for linking, particularly for shared libraries on Windows platforms when using the Zig toolchain.
  • Updated Linking Functions: The linkargv and link functions were modified to incorporate the new _linkfiles logic and accept an additional opt parameter, ensuring correct handling of Zig-generated shared library dependencies.
  • Bug Fix: This change addresses and fixes issue Zig dynamic library doesn't export function on MSVC #7395, which likely involved problems with linking Zig shared libraries on Windows.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • xmake/modules/core/tools/zig.lua
    • Added a new local function _linkfiles to manage link input files, particularly for shared libraries on Windows.
    • Modified the linkargv function to accept an optional opt parameter and utilize the new _linkfiles function.
    • Modified the link function to accept an optional opt parameter and pass it to linkargv.
Activity
  • No human activity (comments, reviews) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a fix for linking shared libraries with Zig on Windows when the project contains source files from other languages. A new local function, _linkfiles, has been added to correctly filter and assemble the list of files for the Zig linker, addressing the issue of unresolved external symbols. The changes are logical and effectively solve the problem. I have one suggestion to refactor the new function for improved readability by reducing nested logic.

Comment on lines +28 to +49
local function _linkfiles(self, objectfiles, targetkind, opt)
opt = opt or {}
local target = opt.target
if targetkind == "shared" and target and target:is_plat("windows") then
local sourcebatch = target:sourcebatches()["zig.build"]
if sourcebatch and sourcebatch.sourcefiles and sourcebatch.objectfiles then
local zigobjectfiles = {}
for _, objectfile in ipairs(sourcebatch.objectfiles) do
zigobjectfiles[path.translate(objectfile)] = true
end
local linkfiles = {}
for _, objectfile in ipairs(table.wrap(objectfiles)) do
if not zigobjectfiles[path.translate(objectfile)] then
table.insert(linkfiles, objectfile)
end
end
table.join2(linkfiles, sourcebatch.sourcefiles)
return linkfiles
end
end
return objectfiles
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This function can be slightly refactored to improve readability by reducing nesting. Using guard clauses at the beginning of the function makes the main logic path clearer and the code easier to follow.

local function _linkfiles(self, objectfiles, targetkind, opt)
    opt = opt or {}
    local target = opt.target
    if not (targetkind == "shared" and target and target:is_plat("windows")) then
        return objectfiles
    end

    local sourcebatch = target:sourcebatches()["zig.build"]
    if not (sourcebatch and sourcebatch.sourcefiles and sourcebatch.objectfiles) then
        return objectfiles
    end

    local zigobjectfiles = {}
    for _, objectfile in ipairs(sourcebatch.objectfiles) do
        zigobjectfiles[path.translate(objectfile)] = true
    end

    local linkfiles = {}
    for _, objectfile in ipairs(table.wrap(objectfiles)) do
        if not zigobjectfiles[path.translate(objectfile)] then
            table.insert(linkfiles, objectfile)
        end
    end

    table.join2(linkfiles, sourcebatch.sourcefiles)
    return linkfiles
end

end
end
return objectfiles
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? why? I've fixed it (#7395). The current patch is rather hacky and cannot be approved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this one (#7395) but it still didn't work, maybe I'm wrong but I wasn't able to have access to symbols even when I was on dev branch

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works for me. -lc has been added.

c:\users\wangrunqing\appdata\local.xmake\packages\z\zig\0.15.2\ebe55ddddf714323a6cd2869c52a8e6b\zig.exe build-lib -dynamic -fPIC -target x86_64-windows-msvc -lc -lkernel32 -lntdll -fstrip -dead_strip -femit-bin=build\windows\x64\release\ZigDynLibWindows.dll build.objs\ZigDynLibWindows\windows\x64\release\src\exported_function.zig.obj

PS C:\Users\wangrunqing\Downloads\Zig-DynLib-Windows> xmake f -c
checking for platform ... windows (x64)
checking for Microsoft C/C++ Compiler (x64) ... ok
checking for Microsoft Visual Studio (x64) version ... 2026
PS C:\Users\wangrunqing\Downloads\Zig-DynLib-Windows> xmake -rv
checking for C:\Users\wangrunqing\AppData\Local\.xmake\packages\z\zig\0.15.2\ebe55ddddf714323a6cd2869c52a8e6b\zig ... c:\users\wangrunqing\appdata\local\.xmake\packages\z\zig\0.15.2\ebe55ddddf714323a6cd2869c52a8e6b\zig.exe
checking for the zig compiler (zc) ... zig.exe
checking for c:\users\wangrunqing\appdata\local\.xmake\packages\z\zig\0.15.2\ebe55ddddf714323a6cd2869c52a8e6b\zig.exe ... ok
checking for flags (-O ReleaseFast) ... ok
checking for flags (--cache-dir build\.objs\ZigDynLibWindows\windows\x64\release\zig-cache) ... ok
checking for cl.exe ... C:\Program Files\Microsoft Visual Studio\18\Insiders\VC\Tools\MSVC\14.50.35503\bin\HostX64\x64\cl.exe
checking for the c++ compiler (cxx) ... cl.exe
[ 23%]: <ZigDynLibWindows> compiling.release src\exported_function.zig
c:\users\wangrunqing\appdata\local\.xmake\packages\z\zig\0.15.2\ebe55ddddf714323a6cd2869c52a8e6b\zig.exe build-obj -target x86_64-windows-msvc -fPIC -O ReleaseFast --cache-dir build\.objs\ZigDynLibWindows\windows\x64\release\zig-cache -femit-bin=build\.objs\ZigDynLibWindows\windows\x64\release\src\exported_function.zig.obj src\exported_function.zig
checking for C:\Users\wangrunqing\AppData\Local\.xmake\packages\z\zig\0.15.2\ebe55ddddf714323a6cd2869c52a8e6b\zig ... c:\users\wangrunqing\appdata\local\.xmake\packages\z\zig\0.15.2\ebe55ddddf714323a6cd2869c52a8e6b\zig.exe
checking for the zig shared library linker (zcsh) ... zig.exe
[ 47%]: <ZigDynLibWindows> linking.release ZigDynLibWindows.dll
c:\users\wangrunqing\appdata\local\.xmake\packages\z\zig\0.15.2\ebe55ddddf714323a6cd2869c52a8e6b\zig.exe build-lib -dynamic -fPIC -target x86_64-windows-msvc -lc -lkernel32 -lntdll -fstrip -dead_strip -femit-bin=build\windows\x64\release\ZigDynLibWindows.dll build\.objs\ZigDynLibWindows\windows\x64\release\src\exported_function.zig.obj
checking for cl.exe ... C:\Program Files\Microsoft Visual Studio\18\Insiders\VC\Tools\MSVC\14.50.35503\bin\HostX64\x64\cl.exe
checking for the c compiler (cc) ... cl.exe
[100%]: build ok, spent 10.969s

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the dll file contains exported symbols ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's simply a problem with missing libc library symbols; adding -lc will solve it. I don't know what you mean by "exported symbols".

@waruqi
Copy link
Member

waruqi commented Mar 20, 2026

I believe #7395 has been fixed, at least it works for me. If you still have problems, please provide more error logs.

@waruqi waruqi closed this Mar 20, 2026
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

Successfully merging this pull request may close these issues.

Zig dynamic library doesn't export function on MSVC

2 participants