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

figure out why linking against static MSVCRT isn't working #2064

Closed
andrewrk opened this issue Mar 14, 2019 · 1 comment · Fixed by #2864
Closed

figure out why linking against static MSVCRT isn't working #2064

andrewrk opened this issue Mar 14, 2019 · 1 comment · Fixed by #2864
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. os-windows stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@andrewrk
Copy link
Member

Here in link.cpp I had to force is_dynamic to be true to fix regressions after 5d2edac.

static void add_nt_link_args(LinkJob *lj, bool is_library) {
    CodeGen *g = lj->codegen;

    if (lj->link_in_crt) {
        bool is_dynamic = true; // g->is_dynamic;
        const char *lib_str = is_dynamic ? "" : "lib";
        const char *d_str = (g->build_mode == BuildModeDebug) ? "d" : "";

        if (!is_dynamic) {
            Buf *cmt_lib_name = buf_sprintf("libcmt%s.lib", d_str);
            lj->args.append(buf_ptr(cmt_lib_name));
        } else {
            Buf *msvcrt_lib_name = buf_sprintf("msvcrt%s.lib", d_str);
            lj->args.append(buf_ptr(msvcrt_lib_name));
        }

        Buf *vcruntime_lib_name = buf_sprintf("%svcruntime%s.lib", lib_str, d_str);
        lj->args.append(buf_ptr(vcruntime_lib_name));

        Buf *crt_lib_name = buf_sprintf("%sucrt%s.lib", lib_str, d_str);
        lj->args.append(buf_ptr(crt_lib_name));

        //Visual C++ 2015 Conformance Changes
        //https://msdn.microsoft.com/en-us/library/bb531344.aspx
        lj->args.append("legacy_stdio_definitions.lib");

        // msvcrt depends on kernel32
        lj->args.append("kernel32.lib");

We should be able to do:

-        bool is_dynamic = true; // g->is_dynamic;
+        bool is_dynamic = g->is_dynamic;

And have the full zig test suite pass on Windows.

On the other hand, this issue may become unnecessary if we get a Windows libc shipping with Zig with #514.

@andrewrk andrewrk added contributor friendly This issue is limited in scope and/or knowledge of Zig internals. os-windows stage1 The process of building from source via WebAssembly and the C backend. labels Mar 14, 2019
@andrewrk andrewrk added this to the 0.5.0 milestone Mar 14, 2019
andrewrk added a commit that referenced this issue Mar 14, 2019
@Sahnvour
Copy link
Contributor

I think this is an issue only when building the self-hosted compiler, right?

When applying your diff, it fails with many linker warnings and errors such as

lld: warning: LLVMAArch64CodeGen.lib(AArch64TargetMachine.obj): locally defined symbol imported: free (defined in libucrtd.lib(free.obj)) [LNK4217]

Indicating that it expects to import free from a DLL, but found it in the object files it is currently linking.

lld: warning: zig_cpp.lib(zig_llvm.cpp.obj): locally defined symbol imported: free (defined in libucrtd.lib(free.obj)) [LNK4217]

I guess it's also built with /MD[d] for the zig0 and zig1 targets, at least visual studio generated projects say so.

lld: error: undefined symbol: __imp_strdup
>>> referenced by zig_cpp.lib(zig_llvm.cpp.obj):($LN8)

__imp_XYZ are used when linking with a DLL.

By default, on windows, LLVM seems to build with /MD[d]. Could it be that, following our LLVM build instructions, a fully static build of self-hosted zig is impossible since LLVM expects dynamic CRT libraries?
It would also require rebuilding some steps of zig staticly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. os-windows stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants