My cpp code:
int main(int argc, char** argv) {}
I use the following command to compile and link it:
// I want to read the result ir
clang -S -emit-llvm main.cpp
// I want to compile directly from ir
llc -filetype=obj main.ll
lld -flavor link main.obj // <root>: undefined symbol: mainCRTStartup Link failed
Did I miss something?
You need to pass libcmt.lib
too, like so: lld-link main.obj libcmt.lib
.
If you run clang-cl main.cpp /FA
and then look at main.asm, you'll see these lines:
.section .drectve,"yn"
.ascii " /DEFAULTLIB:libcmt.lib"
.ascii " /DEFAULTLIB:oldnames.lib"
That's what causes libcmt.lib to be linked automatically when you don't go through llc.
mainCRTStartup is a function defined by the CRT (which clang is probably implicitly using in the first step, and generates an IR file with mainCRTStartup as the entry point)
Try passing -nostdlib to clang in the first step
This will mean you won't be able to use standard library functions, though
If you want to use CRT functions but link with lld you need to link in the native libraries
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With