I just tried out the latest LLVM and Clang trunk versions. They compiled without a single warning out of the box, but I’m having trouble linking a Hello, World! example. My code is
#include <stdio.h>
int main(){
printf("Hello, World!\n");
}
If I compile using
clang test.c
I get the following error
/usr/bin/ld: crt1.o: No such file: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Using -v shows that the GNU ld is invoked as:
"/usr/bin/ld" --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o a.out crt1.o crti.o crtbegin.o -L -L/../../.. /tmp/cc-0XJTsG.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed crtend.o crtn.o
But I have the crt1.o object file!
locate crt1.o
Output:
/usr/lib/Mcrt1.o
/usr/lib/Scrt1.o
/usr/lib/crt1.o
/usr/lib/gcrt1.o
This also works:
clang -c test.c
gcc test.o
And of course
gcc test.c
What I further tried:
clang -Xlinker "-L /usr/lib" test.c
/usr/bin/ld: crt1.o: No such file: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
clang -Xlinker "-L /usr/lib" test.c -v
"/usr/bin/ld" --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o a.out crt1.o crti.o crtbegin.o -L -L/../../.. -L /usr/lib /tmp/cc-YsI9ES.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed crtend.o
I also tried copying the crt1.o file into the current directory. That seemed to work. Well, it didn’t compile because after that crti.o was missing.
My Linux distribution is Ubuntu.
Well, I don’t really know what to try next. I don’t see how I could fix clang nor do I have an idea on how to inject the necessary path in the ld invocation. Any ideas?
It seems to be the Clang version which can’t detect the host’s Linux version and GCC version…
This code in Clang which must add path to the crt*:
llvm → tools → clang → lib → Driver → Tools.cppand the GetFilePath will try to search asked files in
getFilePaths()list of current ToolChain (fileclang/lib/Driver/ToolChains.cpp). If it can’t find a file it will return the Name unchanged.