Search code examples
c++llvmlld

how to link libstdc++ using llvm lld


I want to link test.ll and libstdc++.a in window OS.

I have simple test IR code like this.

@.str = internal constant [14 x i8] c"hello, world\0A\00"

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

define i32 @main(i32 %argc, i8** %argv) nounwind {
entry:
    %tmp1 = getelementptr [14 x i8], [14 x i8]* @.str, i32 0, i32 0
    %tmp2 = call i32 (i8*, ...) @printf( i8* %tmp1 ) nounwind
    ret i32 0
}

I successed compile test.ll to test.obj with llc.

Now I want to make test.exe file with lld not gcc, cl

I`ve try this command but dosen't work...

lld -flavor link /entry:main /implib:libstdc++.a test.obj

It return this.

test.obj: undefined symbol: printf
error: link failed

My LLVM version is 4.0.0, lld version is 4.0.0

I Shoud use GCC? Help me Please. Thank you.


Solution

  • printf is not provided by libstdc++, you need to link to a C standard library like msvcrt.
    /entry:main isn’t necessary since main will be called by the crt.
    /implib is not the option you are looking for. It specifies the name of the import lib to generate.

    Using g++ test.obj successfully links your test.obj (created with clang-cl -c test.ll) to the mingw-w64-crt and creates a runnable program.
    clang++ test.ll (this is mingw-w64 clang in msys2) does effectively the same.
    You can check the invocation with -v to find out which objects and libraries were linked.

    You can also link to the static microsoft crt with: clang-cl -fuse-ld=lld-link test.ll libcmt.lib

    Not using lld with clang-cl currently (LLVM 4.0) doesn’t seem to work.