Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lld undefined symbol: mainCRTStartup

Tags:

llvm-clang

lld

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?

like image 410
J.Doe Avatar asked Oct 18 '22 11:10

J.Doe


2 Answers

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.

like image 61
thakis Avatar answered Dec 30 '22 19:12

thakis


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

like image 23
keyboardsmoke Avatar answered Dec 30 '22 18:12

keyboardsmoke