Odin icon indicating copy to clipboard operation
Odin copied to clipboard

Conflicting raylib and libc Windows libraries

Open deadwanderer opened this issue 1 year ago • 2 comments

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • Operating System & Odin Version:
  • Please paste odin report output:

Odin: dev-2023-08-nightly:9453b238 OS: Windows 10 Enterprise (version: 22H2), build 19045.3324 CPU: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz RAM: 32662 MiB

Expected Behavior

No linker multiple defines errors when using libc with raylib

Current Behavior

What is the current behavior?

Failure Information (for bugs)

When I attempt to use core:c/libc and vendor:raylib at the same time, I get a number of linker errors when compiling:

ucrt.lib(api-ms-win-crt-stdio-l1-1-0.dll) : error LNK2005: fclose already defined in libucrt.lib(fclose.obj)
ucrt.lib(api-ms-win-crt-heap-l1-1-0.dll) : error LNK2005: calloc already defined in libucrt.lib(calloc.obj)
ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) : error LNK2005: _errno already defined in libucrt.lib(errno.obj)
ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) : error LNK2005: system already defined in libucrt.lib(system.obj)
ucrt.lib(api-ms-win-crt-filesystem-l1-1-0.dll) : error LNK2005: _access already defined in libucrt.lib(access.obj)
ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) : error LNK2005: exit already defined in libucrt.lib(exit.obj)
ucrt.lib(api-ms-win-crt-stdio-l1-1-0.dll) : error LNK2005: __acrt_iob_func already defined in libucrt.lib(_file.obj)
ucrt.lib(api-ms-win-crt-stdio-l1-1-0.dll) : error LNK2005: fflush already defined in libucrt.lib(fflush.obj)
libucrt.lib(argv_data.obj) : error LNK2005: __p___argc already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll)
libucrt.lib(argv_data.obj) : error LNK2005: __p___argv already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll)
libucrt.lib(matherr.obj) : error LNK2005: __setusermatherr already defined in ucrt.lib(api-ms-win-crt-math-l1-1-0.dll)

It appears that raylib uses ucrt.lib (I couldn't find an explicit CRT load in raylib's code), and libc pulls in libucrt.lib. When I replaced libucrt.lib with ucrt.lib in my Odin installation's core:c/libc files, the issue went away, and I was able to compile without issues (at least for the small subset of both Raylib and libc that I'm currently using :-) )

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

I use raylib for windowing and graphics, and am attempting to hot-reload a dll. I use libc.system() to copy and delete the DLL before loading and after rebuilding, respectively. Using both these libraries together causes the above linker errors.

Failure Logs

Please include any relevant log snippets or files here.

deadwanderer avatar Sep 11 '23 20:09 deadwanderer

#3307 is an attempt at fixing this

laytan avatar Apr 04 '24 21:04 laytan

Hey, I solved this issue by not using libc and doing the copy and delete through sys/windows.

import "core:sys/windows";
import "core:unicode/utf16";

To copy:

    current_filename_buf, new_filename_buf : [64]u16;
    utf16.encode_string(current_filename_buf[:], "game.dll");
    utf16.encode_string(new_filename_buf[:], dll_name);

    if !windows.CopyFileW(raw_data(current_filename_buf[:]), raw_data(new_filename_buf[:]), false) {
        // success
    }

To delete:

    filename_buf : [64]u16;
    utf16.encode_string(filename_buf[:], dll_to_delete);

    if windows.DeleteFileW(raw_data(filename_buf[:])) {
        // success
    }

Note: I'm sharing this solution because I noticed #3307 has some discussions about this topic and it might not get merged.

nawetimebomb avatar Jun 09 '24 16:06 nawetimebomb