Search code examples
c++cclanglld

lld-link error: no input files when trying to link with libcmt.lib on Windows10


I'm compiling a very simple main.c:

int myfunc(int x, int y){
    return x + y;
}

int main(){
    int res = myfunc(2,2);

    return;
}

I am compiling with the following options:

clang.exe -std=c11 -g -c .\main.c

I get my main.o as the output. Now I go to link using lld-link.exe with the following options:

lld-link.exe -defaultlib:libcmt -libpath:"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\um\x64\" .\main.o

I get the following error: lld-link: error: no input files

But there's clearly main.o as the last parameter, which is supposedly where you put the input files. Also keep in mind that I have to link agaisnt libcmt.lib in order to get the symbols for the mainCRTStartup function.


Solution

  • This seems to be triggered by the trailing backslash in your libpath argument. And I think this actually is due to how windows cmd quoting works, not due to any lld specific issue.

    As the libpath argument ends with x64\", the closing quote is interpreted as an escaped literal quote, and thus .\main.o is interpreted as part of the libpath argument, and tries to run the command even though the quoted string hasn't got a matching closing quote. (Here on stackoverflow, the syntax highlighting actually interprets it the same way.)

    Either change the backslash into a double backslash before the ending quote, or remove the trailing backslash from that option.

    At https://devblogs.microsoft.com/oldnewthing/?p=12833 you can find a more in-depth explanation of the windows shell quoting rules.

    With that fixed, I still had to add -libpath:"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\lib\x64" -libpath:"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\ucrt\x64" for it to actually find libcmt.lib (which is under MSVC) and libucrt.lib.

    (Also, the C code itself failed to compile for me with clang with the given parameters, due to return; in a function returning int, but that may have been just a typo in the example.)