odin-lang / Odin

Odin Programming Language
https://odin-lang.org
BSD 3-Clause "New" or "Revised" License
6.53k stars 569 forks source link

Linking Issue, CRT #3018

Closed jon-lipstate closed 5 months ago

jon-lipstate commented 8 months ago

I found an issue when putting in some debug code (if true do return). if this is placed before the first call to raylib, then the linker fails to link to the CRT. it can be mitigated by calling basically any raylib procedure above the if true do return statement. The linker appends /NODEFAULTLIB:libcmt.

package ehhh
import rl "vendor:raylib"
main :: proc() {
    // rl.CloseWindow() // <-- uncomment this line to remove linker errors
    if true do return
    rl.InitWindow(1, 2, "ehh")
}

odin run .
LINK : error LNK2001: unresolved external symbol mainCRTStartup
reproduce.obj : error LNK2019: unresolved external symbol _tls_index referenced in function runtime._destroy_temp_allocator_fini-10584
reproduce.obj : error LNK2019: unresolved external symbol memset referenced in function runtime.default_context
reproduce.obj : error LNK2019: unresolved external symbol memcpy referenced in function runtime.default_context
reproduce.obj : error LNK2019: unresolved external symbol memmove referenced in function main
reproduce.obj : error LNK2001: unresolved external symbol _fltused
fatal error LNK1120: 6 unresolved externals
flysand7 commented 8 months ago

What happens if you do odin run . -no-crt?

jon-lipstate commented 8 months ago

raylib wants to use the crt so it panics:

odin run . -no-crt
c:/Odin/core/c/libc/types.odin(5:1) Error: Compile time assertion: !ODIN_NO_CRT (`"core:c/libc" cannot be imported when '-no-crt' is used`)
#assert(!ODIN_NO_CRT, `"core:c/libc" cannot be imported when '-n ...

image

The root issue is that no-crt is added to the link step but import raylib (which is not used) is not pruned.

flysand7 commented 8 months ago

Ah yes, I think I understand the issue better now. As much as I'd like to say that it's probably not a problem in this case and can be dismissed as a side effect of the pruning system, I think it might cause issues in the future, where something gets imported transitively and removes that dependency on libc without making any substitutions.

laytan commented 8 months ago

Doesn't seem to be happening on macos/linux

jon-lipstate commented 8 months ago

I notice the same on Ubuntu today as well. So windows-only as an issue