简体   繁体   中英

(clang / llvm-mc / lld) hello world (x86-64 windows & linux)

Is llvm able to compile, assemble and link x86-64 code on windows and/or linux using llvm-mc and lld?

If so, is there a hello-world level example out there? The documentation is pretty sparse at present.


I've tried building a simple hello world (main0.cpp) using trunk LLVM (with clang & lld).

main0.cpp:

int main(int argc, char const* argv[])
    { return 0; }

Compile (no errors):

[MY-LLVM]/clang -S -o main0.s main0.cpp

Assemble (no errors):

[MY-LLVM]/llvm-mc -arch=x86-64 -triple=x86_64-linux-gnu -o main0.o main0.s

Link ( ): ):

[MY-LLVM]/lld -flavor gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -L/usr/lib/gcc/x86_64-linux-gnu/4.8 -L/usr/lib/x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib -L/<MY-LLVM>/lib -L/lib -L/usr/lib -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/x86_64-linux-gnu/crt1.o /usr/lib/x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o /usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o /usr/lib/x86_64-linux-gnu/crtn.o -o main0.run main.o

Link error:

lld: unknown input file format for file main.o


I obtained the linker command via clang -o main0.run main0.cpp -### (replacing ld with [MY-LLVM]/lld -flavor gnu ).

I assume that I'm either generating the wrong type of object file when assembling, or using the wrong parameters when linking.

Does anyone know how to do this right?


(My ultimate aim is to get full C++14 working on win64 (without massive hacks), but I'm struggling with getting trunk clang to work with the mingw tools so I thought I'd try pure LLVM).

lld developer here.

lld self hosts on both Linux and Windows, so I would expect them to work here. The way I generally get clang to use lld is to make a symlink to lld named ld and add it to PATH. lld will behave like gnu-ld in this case.

As for llvm-mc, it is not an assembler. It is a tool for testing the MC layer of llvm. By default it simply parses the assembly and prints it back out. This is why lld is rejecting the main0.o file, as it's actually a text file.

The correct thing to do here without having lld as ld in the path is either:

clang -c -o main0.o main0.cpp

or:

clang -S -o main0.s main0.cpp
clang -c -o main0.o main0.s

If you really want the assembly for some reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM