无法使用lld-link.exe(Windows)链接C运行时库(libcmt.lib) [英] Unable to link C runtime library (libcmt.lib) using lld-link.exe (Windows)

查看:1724
本文介绍了无法使用lld-link.exe(Windows)链接C运行时库(libcmt.lib)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LLVM编写语言.我想避免打包cl​​ang并仅使用LLVM工具(例如lld,lld-link).我一直在尝试从我的简单IR代码(testinput.ll)调用printf函数:

I'm writing a language using LLVM. I'd like to avoid having to package clang and simply use the LLVM tools (ex. lld, lld-link). I've been trying to invoke the printf function from my simple IR code (testinput.ll):

; ModuleID = 'Test2'
source_filename = "entry"

@str_0 = private unnamed_addr constant [13 x i8] c"Hello world!\00"

declare i32 @printf(i8*, ...)

define i32 @main() {
entry:
  %anonymous_10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @str_0, i32 0, i32 0))
  ret i32 1234
}

但是无论我尝试什么,我都会收到错误消息:

But I keep receiving errors no matter what I try:

$ clang-cl -fuse-ld=lld-link testinput.ll "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\spectre\x64\libcmt.lib"

注意:我只是通过在系统上搜索libcmt.lib来随机选择链接"... spectre \ x64 \ libcmt.lib ...".

Note: I've chosen the link randomly "... spectre\x64\libcmt.lib ..." by simply searching for libcmt.lib on the system.

错误:

C:\Program Files\LLVM\bin\lld-link: warning: libcmt.lib(loadcfg.obj): undefined symbol: __enclave_config
error: link failed
clang-cl.exe: error: linker command failed with exit code 1 (use -v to see invocation)

我正在使用Windows 10(x64)和LLVM 5.0.有趣的是,使用link.exe(Windows VS工具的链接器),一切都可以正常工作(在我的情况下,这是clang所使用的).

I'm using Windows 10 (x64) with LLVM 5.0. Interestingly, using link.exe (Windows' VS tools' linker) everything works fine (which is what clang uses under the hood in my case).

我已阅读

...如我之前所写,__enclave_config是由链接器填充的变量,但是您必须使用VC链接器,并且必须使用足够新的链接器才能自动填充它....

... As I wrote earlier, __enclave_config is a variable that is filled in by the linker, but you have to use the VC linker, and a linker new enough to be able to automatically fill it in. ...

我相信这里的问题是libcmt.liblld-link链接程序. lld-link版本(LLVM 5.0)是否与我使用的libcmt.lib不兼容,是问题所在吗?

I believe the problem here is libcmt.lib and the lld-link linker. Is the lld-link version (LLVM 5.0) incompatible with the libcmt.lib that I'm using, is that the problem?

编辑:我设法跟踪了c声在幕后的作用,并使用以下命令找到了它:

Edit: I've managed to track down what clang does behind the scenes, and have found it using the following command:

lld-link -out:a.exe -defaultlib:libcmt "-libpath:C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.16.27023\\lib\\x64" "-libpath:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.17763.0\\ucrt\\x64" "-libpath:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.17763.0\\um\\x64" -nologo "test.obj"

很显然,它正在使用lld-link,并且可以正常工作.但是,很奇怪的是,如果使用clang(也许使用-fuse-ld=lld -v选项?)将输入目标文件编译为.LL(LLVM IR),则只有在没有错误的情况下编译.

Clearly it's using lld-link, and it's working. However, strangely enough, it only compiles without errors if the input object file was compiled to .LL (LLVM IR) using clang (maybe using -fuse-ld=lld -v options?).

这很奇怪,是在检查了clang(test.ll)的输出.LL文件后,出现了printf(及其使用的其他一些* printf函数)的完整,源代码(在IR中)定义(在输出的.LL文件中).

What's weird about this, is that upon inspection of the output .LL file from clang (test.ll) the full, source code (in IR) definitions of printf (and some other *printf functions used by it) is present (in the output .LL file).

因此,它会以某种方式在输出.LL,IR代码文件中获取printf本身的定义.

So, somehow it's getting the definitions of printf itself inside the output .LL, IR code file.

据我所知,您不能只是$ llc libcmt.lib testinput.ll吗?那将是链接器的工作...(llc仅接受一个位置参数)

As far as I know, you can't just $ llc libcmt.lib testinput.ll? That'd be the linker's job... (llc accepts only one positional argument)

使用 testinput.ll文件(不是从clang输出)尝试相同的lld-link命令和参数时出现的错误如下:

The error that I'm getting once I try the same lld-link command and arguments with my testinput.ll file (not outputted from clang) is the following:

lld-link: error: <root>: undefined symbol: _mainCRTStartup
lld-link: error: undefined symbol: _printf

推荐答案

结果证明,它比我预期的简单得多.也许这些错误至少对您有所帮助,我本可以避免所有这些混乱...

Turns out, it was much much more simple than I anticipated. Maybe if the errors were at least somewhat helpful, I could have avoided all this confusion...

我已经通过比较clang的输出LL文件了解了这一点,并在开始时发现了一条奇怪的行:

I've figured it out by comparing clang's output LL file, and noticed a curious line at the beginning:

target triple = "x86_64-pc-windows-msvc"

一旦将其添加到我的testinput.ll文件中,lld-link的所有内容都可以正常工作.哇!

Once I added it to my testinput.ll file, everything worked flawlessly with lld-link. Hurray!

这篇关于无法使用lld-link.exe(Windows)链接C运行时库(libcmt.lib)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆