lld undefined symbol: mainCRTStartup
Asked Answered
D

2

7

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?

Dyanna answered 22/4, 2016 at 2:31 Comment(0)
P
3

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.

Projectionist answered 26/8, 2018 at 19:36 Comment(0)
C
2

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

Cellulitis answered 15/4, 2017 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.