lld undefined symbol: mainCRTStartup

老子叫甜甜 提交于 2019-12-19 08:31:49

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!