odin-lang / Odin

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

Linking fails on dev-2024-05 with: cannot find crtbegin.o #3597

Closed eliaspekkala closed 2 months ago

eliaspekkala commented 3 months ago

Problem

./build_odin.sh succeeds on dev-2024-04a and prints the demo.

./build_odin.sh fails on dev-2024-05 with the following output:

~/Documents/odin $ ./build_odin.sh 
+ /usr/lib/llvm17/bin/clang++ src/main.cpp src/libtommath.cpp -Wno-switch -Wno-macro-redefined -Wno-unused-value '-DODIN_VERSION_RAW="dev-2024-05"' '-DGIT_SHA="2250eb3e7"' '-std=c++14' -I/usr/lib/llvm17/include '-std=c++17' -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -L/usr/lib/llvm17/lib -g -pthread -lm -lstdc++ -ldl /usr/lib/llvm17/lib/libLLVM-17.so '-Wl,-rpath=$ORIGIN' -o odin
+ set +x
/usr/bin/ld: cannot find crtbegin.o: No such file or directory
/usr/bin/ld: cannot find -lgcc: No such file or directory
/usr/bin/ld: cannot find -lgcc: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Somewhere between dev-2024-04a and dev-2024-05 an issue was introduced, or have the build requirements changed?

System

OS: Alpine Linux edge, Linux 6.6.30-0-lts Arch: x86_64 Libc: musl

Version

Odin: dev-2024-05:2250eb3e7 Backend: LLVM 17.0.6

marcs-feh commented 2 months ago

I also had the same issue on Alpine. Here's a workaround you can do:

odin build your_dir -reloc-mode:pic -build-mode:obj
clang -fPIE your_dir.o -o your_executable

I have no idea why it's trying to link -lgcc.

doedre commented 2 months ago

git bisect pointed me to d1a1e8f646c5b959be638aa955856f686b11a4f3 as the first faulty commit. Updating to LLVM 18.1.6 did not resolve this issue. Don't see where it tries to link -lgcc either.

Kelimion commented 2 months ago

I can't replicate it on Ubuntu 24.04. For LLVM 18:

[10:18] jeroen@CALISTO:/mnt/w/Odin $ ./build_odin.sh
+ /usr/lib/llvm-18/bin/clang++ src/main.cpp src/libtommath.cpp -Wno-switch -Wno-macro-redefined -Wno-unused-value -DODIN_VERSION_RAW="dev-2024-05" -DGIT_SHA="856537f0c" -std=c++14 -I/usr/lib/llvm-18/include -std=c++17 -fno-exceptions -funwind-tables -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -L/usr/lib/llvm-18/lib -g -pthread -lm -lstdc++ -ldl /usr/lib/llvm-18/lib/libLLVM-18.so -Wl,-rpath=$ORIGIN -o odin
+ set +x

# the basics
Hellope, World! from /mnt/w/Odin/demo.
<snip>

LLVM 17 either:

jeroen@CALISTO:/mnt/w/Odin $ LLVM_CONFIG=llvm-config-17 ./build_odin.sh
+ /usr/lib/llvm-17/bin/clang++ src/main.cpp src/libtommath.cpp -Wno-switch -Wno-macro-redefined -Wno-unused-value -DODIN_VERSION_RAW="dev-2024-05" -DGIT_SHA="856537f0c" -std=c++14 -I/usr/lib/llvm-17/include -std=c++17 -fno-exceptions -funwind-tables -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -L/usr/lib/llvm-17/lib -g -pthread -lm -lstdc++ -ldl /usr/lib/llvm-17/lib/libLLVM-17.so -Wl,-rpath=$ORIGIN -o odin
+ set +x

# the basics
Hellope, World! from /mnt/w/Odin/demo.
doedre commented 2 months ago

It can be temporarily fixed by specifying your target triple here. On my setup it's x86_64-alpine-linux-musl, so it should look like this:

bc->link_flags = concatenate3_strings(permanent_allocator(),
  str_lit("-target "), str_lit("x86_64-alpine-linux-musl"), str_lit(" "));

Tried to compile ols with it, works fine. Can't really come up with a better solution yet, as I'm not very familiar with the codebase.

eliaspekkala commented 2 months ago

Thanks. I can confirm that this temporary fix works.

@laytan Seeing as you have worked on this part of the codebase before, do you know how we could make a more permanent fix to this issue?

laytan commented 2 months ago

From the temporary fix it seems like using the -gnu suffixed targets makes it link in gcc?

Maybe this line needs changing: https://github.com/odin-lang/Odin/blob/d99e1616cf0199895ede2a82490197a9e5ccebd9/src/build_settings.cpp#L968

Something like x86_64-pc-linux-elf instead?

But these are just initial guesses, I will try to find some time to test/reproduce this