问题
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?
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/36783764/lld-undefined-symbol-maincrtstartup