I prepare two very simple c files, main.c and test.c, and use the below command to compile them:
clang --target=aarch64 -c main.c
clang --target=aarch64 -c test.c
But when I use the command clang --target=aarch64 -fuse-ld=lld -v main.o test.o, it actually invoke "/usr/bin/gcc" -fuse-ld=lld
root#6ab3fca322b6:/home/example# clang --target=aarch64 -fuse-ld=lld -v main.o test.o
clang version 13.0.0
Target: aarch64
Thread model: posix
InstalledDir: /usr/local/bin
"/usr/bin/gcc" -fuse-ld=lld -o a.out main.o test.o
ld.lld: error: main.o is incompatible with elf64-x86-64
ld.lld: error: test.o is incompatible with elf64-x86-64
collect2: error: ld returned 1 exit status
clang-13: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
But if I manually invoke ld.lld, everything there is no problem just a warning about "cannot find entry symbol _start;".
ld.lld main.o test.o
Also I try to remove the flag --target=aarch64, it still failed:
root#6ab3fca322b6:/home/example# clang -v main.o test.o
clang version 13.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Candidate multilib: .;#m64
Candidate multilib: 32;#m32
Candidate multilib: x32;#mx32
Selected multilib: .;#m64
"/usr/bin/ld" -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /lib/x86_64-linux-gnu/crt1.o /lib/x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/9/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/usr/local/bin/../lib -L/lib -L/usr/lib main.o test.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/9/crtend.o /lib/x86_64-linux-gnu/crtn.o
ld: error: main.o is incompatible with elf64-x86-64
ld: error: test.o is incompatible with elf64-x86-64
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)
I have soft linked the /usr/bin/ld to /usr/bin/ld.lld, but I don't know why it adds some many flags which I don't specify, e.g., -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2
So my questions are:
why clang --target=aarch64 -fuse-ld=lld can't invoke ld.lld?
why clang can invoke /usr/bin/ld but add some many unspecified flags?
For aarch64, which is the correct way to make clang correctly invoke ld.lld?
I am using clang and lld 13.0.0.
Best Regards,
Simon
Related
Helllo,
On my WSL2 ubuntu 20.04 system, when I use the following command to invoke ld.lld, everything is OK:
clang -fuse-ld=/usr/local/bin/ld.lld ....
But when the option "--target=aarch64" is added, it will invoke gcc.
clang -fuse-ld=/usr/local/bin/ld.lld --target=aarch64
The actual command will be:
'/usr/bin/gcc' -fuse-ld=/usr/local/bin/ld.lld --target=aarch64
Note: when I run the below command, it shows:
simon#LAPTOP-JH2M71LG:~/musl-1.2.2$ clang -fuse-ld=/usr/local/bin/ld.lld --target=aarch64 -v obj/src/unistd/writev.lo
clang version 13.0.0
Target: aarch64
Thread model: posix
InstalledDir: /usr/local/bin
"/usr/bin/gcc" -fuse-ld=/usr/local/bin/ld.lld -o a.out obj/src/unistd/writev.lo
gcc: error: unrecognized command-line option ‘-fuse-ld=/usr/local/bin/ld.lld’
clang-13: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
Note: At the same time I have link edthe ld to ld.lld by using following command:
sudo ln -fs /usr/local/bin/ld.lld /usr/bin/ld
Thanks,
Simon
I want to use opemmp with clang compile in Windows and here is my compile command:
clang++ 1.cpp -o 1.exe -fopenmp=libomp -I=\"C:\\Program Files\\LLVM\\lib\\clang\\9.0.0\\include\" -L=\"C:\\Program Files\\LLVM\\bin\" -Wall -g -O2 -static-libgcc --target=x86_64-w64-mingw -std=c++17
and there is an error:
C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\ld.exe: cannot find -lomp
clang: error: linker command failed with exit code 1 (use -v to see invocation)
but I have the "libomp.lib" file and write the link path in command argument. How to solve it?
Thanks!
In macOS Sierra, installation for xgboost with openmp enabled always fails.
From https://xgboost.readthedocs.io/en/latest/build.html,
I've tried:
cp make/config.mk ./config.mk; make -j4
With,
export CC=/usr/local/Cellar/llvm/4.0.0_1/bin/clang
export CXX=/usr/local/Cellar/llvm/4.0.0_1/bin/clang++
export CXX1X=/usr/local/Cellar/llvm/4.0.0_1/bin/clang++
It fails with,
clang-4.0clang-4.0: : warning: argument unused during compilation: '-pthread' [-Wunused-command-line-argument]warning:
argument unused during compilation: '-pthread' [-Wunused-command-line-argument]
ld: library not found for -lomp
ld: library not found for -lomp
clang-4.0: error: linker command failed with exit code 1 (use -v to see invocation)
llvm support openmp, but it fails with ld: library not found for -lomp
related question
Install xgboost on Mac - ld: library not found
But, clang-omp goes to boneyard, and discontinue with llvm's OpenMP support.
I've drilled down to, (with -v option)
/usr/local/Cellar/llvm/4.0.0_1/bin/clang++ -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -shared -o lib/libxgboost.so build/learner.o build/logging.o build/c_api/c_api.o build/c_api/c_api_error.o build/common/common.o build/common/hist_util.o build/data/data.o build/data/simple_csr_source.o build/data/simple_dmatrix.o build/data/sparse_page_dmatrix.o build/data/sparse_page_raw_format.o build/data/sparse_page_source.o build/data/sparse_page_writer.o build/gbm/gblinear.o build/gbm/gbm.o build/gbm/gbtree.o build/metric/elementwise_metric.o build/metric/metric.o build/metric/multiclass_metric.o build/metric/rank_metric.o build/objective/multiclass_obj.o build/objective/objective.o build/objective/rank_obj.o build/objective/regression_obj.o build/tree/tree_model.o build/tree/tree_updater.o build/tree/updater_colmaker.o build/tree/updater_fast_hist.o build/tree/updater_histmaker.o build/tree/updater_prune.o build/tree/updater_refresh.o build/tree/updater_skmaker.o build/tree/updater_sync.o dmlc-core/libdmlc.a rabit/lib/librabit.a -pthread -lm -fopenmp
SOLVED
cd /usr/local/lib
ln -s /usr/local/Cellar/llvm/4.0.0_1/lib/libomp.dylib libomp.dylib
llvm installation miss its symbolic link.
In my case I solved adding the following linker flag in addition to -lomp:
-Lpath_to_libomp.dylib_folder
or something like:
-Llib/
In my case I used a version of clang that came with Homebrew's llvm instead of the one that came with the Laptop from the factory, and it worked.
I am trying to use llvm-lld to link an object that has been compiled with -flto.
I have the ld in the path call lld with all the arguments provided.
I created a simple C++ program, which I compile with clang++:
#include <stdio.h>
int main() {
printf("Goodbye cruel world\n");
return 0;
}
Without the -flto flag I get an executable, with the -flto I get this error:
warning: ignoring unknown argument: -plugin
warning: ignoring unknown argument: -plugin-opt=mcpu=x86-64
Cannot open /tmp/a-f1a2a7.o: Exec format error
clang-3.8: error: linker command failed with exit code 1 (use -v to see invocation)
Here are the arguments being passed to the linker, I see that with -flto the plugin arguments are provided include this plugin option, yet it seems to be unsupported.
--eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0/../../../../lib64/crt1.o /usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0/../../../../lib64/crti.o /usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0/crtbegin.o -L/usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0 -L/usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0/../../../../x86_64-pc-linux-gnu/lib -L/usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0/../../.. -L/mnt/data/tmp/dev/llvm/prev/bin/../lib -L/lib -L/usr/lib -plugin /mnt/data/tmp/dev/llvm/prev/bin/../lib/LLVMgold.so -plugin-opt=mcpu=x86-64 /tmp/a-f1a2a7.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0/crtend.o /usr/lib64/gcc/x86_64-pc-linux-gnu/5.2.0/../../../../lib64/crtn.o
Is there a way for lld to link this code?
Yes, you can do it by using gold-linker which supports LTO via plugins:
Here is the instructions how to build and run it
I want to get exe with clang and llvm with two ways:
clang -O2 1.c -o 1.exe
clang -S -emit-llvm 1.c
llc 1.ll -filetype=obj
link 1.obj -defaultlib: ??? (MSVCRT or libcmt)
but exe files will different and first exe will faster, how to do it equal?
Or how to see what clang use like defaultlib?
hard: windows 64.
You can add the -v flag on your first command line to see the linker invocation clang will use to build your program. As to the first executable being faster, that could be because you didn't pass any optimization flags in your second case.
I don't have a Windows machine handy, but here's some example output from my mac:
$ clang -v -O2 1.c -o 1.exe
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.9.0 -emit-obj -disable-free -disable-llvm-verifier -main-file-name 1.c -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 236.3 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1 -O2 -fdebug-compilation-dir /Users/carl/Desktop/example -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.9.0 -fencode-extended-block-signature -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -o /var/folders/mk/0mblc5810cjgs0nylrkjxqbm0000gq/T/1-7ac5d9.o -x c 1.c
clang -cc1 version 5.1 based upon LLVM 3.4svn default target x86_64-apple-darwin13.3.0
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.9.0 -o 1.exe /var/folders/mk/0mblc5810cjgs0nylrkjxqbm0000gq/T/1-7ac5d9.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/lib/darwin/libclang_rt.osx.a
You can see the link line down at the bottom. Broken down for readability, its:
ld
-demangle
-dynamic
-arch x86_64
-macosx_version_min 10.9.0
-o 1.exe
$(OBJECT_FILE)
-lSystem
libclang_rt.osx.a