windows - 如何在Windows上使用clang从命令行传递链接描述文件?

标签 windows visual-studio linker clang linker-scripts

我正在使用Windows操作系统,而Clang版本是8.0.0。我已经安装了Visual Studio社区。

我正在尝试使用链接程序脚本来为我的程序之一自定义内存映射。

我正在从命令行编译和执行程序。
这些是我在Windows上尝试过的选项

clang main.c -ffreestanding -nostartfiles -nodefaultlibs -Wl,-Tlinker.ld -o main

引发的错误为:

LINK : warning LNK4044: unrecognized option '/T'; ignored
linker.ld : fatal error LNK1107: invalid or corrupt file: cannot read at 0x76
clang: error: linker command failed with exit code 1107 (use -v to see invocation)


当我尝试-fuse-ld=lld-link时,错误有所不同
lld-link: warning: ignoring unknown argument: -Tlinker.ld
lld-link: error: <root>: undefined symbol: mainCRTStartup 
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我想知道为什么

same commands work on ubuntu but not on windows?



也请让我知道传递链接脚本的解决方案。

Windows上是否有用于clang的命令,这些命令应同时使用链接程序脚本和clang链接程序lld-link

最佳答案

Windows Visual Studio帮助与clang-cl

clang-cl   options files  -fuse-ld=lld-link -v

-v显示运行和使用详细输出的命令

-fuse-ld = lld-link 对于Windows使用ld链接器lld-link

-fuse-ld = lld 对于Linux使用ld链接器ld.lld
cmake -E env LDFLAGS="-fuse-ld=lld-link" PATH="<path\to\ninja>" 
  cmake -H. -G Ninja -Bbuild 
     -DCMAKE_C_COMPILER:PATH="%ProgramFiles(x86)%\LLVM\bin\clang.exe" 
     -DCMAKE_CXX_COMPILER:PATH="%ProgramFiles(x86)%\LLVM\bin\clang.exe" 
     -DCMAKE_C_COMPILER_ID="Clang" 
     -DCMAKE_CXX_COMPILER_ID="Clang" 
     -DCMAKE_SYSTEM_NAME="Generic"

LLVM Windows Linker support

Microsoft Visual Studio 2019 Linker options

cmake, ninja, clang-cl on Windows

Clang-cl manual on Windows

LLVM extensions for LLD and clang-cl

(solved) research on ld-link with Windows

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

target triple = "x86_64-pc-windows-msvc" add to .LL file



将其添加到testinput.ll文件后,lld-link即可正常运行。欢呼!

关于windows - 如何在Windows上使用clang从命令行传递链接描述文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57340007/

相关文章:

c# - 如何在 C# 的 Windows 服务中执行批处理脚本?

python-3.x - "PythonAccumulatorV2 does not exist"- 在 Jupyter Notebook 中运行 SparkContext() 时

c - 如何为 Visual Studio 2005 安装 pthread_win32(Windows pthread/posix 线程库)?

visual-studio - Devenv ProjectConfig 开关 - Win32 和 x64

c++ - Visual Studio 2010 : Linker > Command Line contains unneeded . 库

visual-c++ - 如何在 Visual C++ 中将一些函数分组到 .exe 输出的新内存部分

c - Windows 和 Linux 从文件输入的字符串长度不同,但标准输入 strcmp 不工作

.net - 为什么我无法从 Visual Studio UI 在核心框架和完整框架之间切换目标?

javascript - 在不安装的情况下使用 AjaxMin

c++ - PATH 环境变量没有影响? (VS2010)