【问题标题】:How do I link with clang on windows?如何在 Windows 上与 clang 链接?
【发布时间】:2021-12-03 11:25:16
【问题描述】:

这里有一些程序集。使用 windows clang 我可以写 clang -c my.s 并获得一个目标文件。我可以使用链接它

"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64\link.exe" n.o /ENTRY:main

(libpath是c:/Program Files (x86)/Windows Kits/10/Lib/10.0.20348.0/um/x64/)

但是我不知道如何使用 clang 的链接器来做到这一点。我不断收到“错误:my.o:未知文件类型”

我在我的 linux 机器上尝试过

clang my.s --target=x86_64-windows-gnu "-L/run/media/eric/win10/Program Files (x86)/Windows Kits/10/Lib/10.0.20348.0/um/x64/"

我得到了大量缺失的库。我真的认为没有必要。我需要的一切似乎都在 Kernel32.lib 中。我只想将 Kernel32 与我的程序集链接起来。 (或仅使用 clang 的窗口)

    .text
    .def     @feat.00;
    .scl    3;
    .type   0;
    .endef
    .globl  @feat.00
.set @feat.00, 0
    .intel_syntax noprefix
    .file   "my.c"
    .def     main;
    .scl    2;
    .type   32;
    .endef
    .globl  main                            # -- Begin function main
    .p2align    4, 0x90
main:                                   # @main
.seh_proc main
# %bb.0:
    sub rsp, 56
    .seh_stackalloc 56
    .seh_endprologue
    mov dword ptr [rsp + 52], 0
    mov ecx, 4294967285
    call    GetStdHandle
    mov qword ptr [rsp + 40], rax
    mov rcx, qword ptr [rsp + 40]
    lea rdx, [rip + text_0]
    mov r8d, 10
    xor eax, eax
    mov r9d, eax
    mov qword ptr [rsp + 32], 0
    call    WriteFile
    mov ecx, 12
    call    ExitProcess
    int3
    .seh_endproc
                                        # -- End function
    .section    .rdata,"dr",discard,text_0
    .globl  text_0 # @text_0
text_0:
    .asciz  "New Text!\n"

    .section    .drectve,"yn"
    .ascii  " /DEFAULTLIB:kernel32.lib"
    .addrsig

【问题讨论】:

  • 使用 WSL(仅限 Windows 10)并本机编译没有任何问题。
  • 我找到了一个解决方案,但它有效,但我认为我不喜欢它。它需要复制和重命名 kernel32.Lib。 mingw 我相信 zig 使用 def 文件,而不是纯文本。如果可能的话,我更喜欢使用它。我不知道如何使用 def 文件来链接

标签: windows linker clang x86-64 ld


【解决方案1】:

如果您使用的是 Windows 10,则确实需要使用 mingw。使用 WSL(Linux 的 Windows 子系统),安装 Ubuntu 或其他发行版(通过 appstore https://docs.microsoft.com/en-us/windows/wsl/wsl-config)并使用“普通”GCC 和其他 linux 工具:

我用

piotr@PiotrLaptop02:/mnt/c/Users/Piotr$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.1 LTS
Release:        20.04
Codename:       focal
piotr@PiotrLaptop02:/mnt/c/Users/Piotr$

【讨论】:

    猜你喜欢
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多