Where are all the builtin macros defined in clang? - clang

I see that __GNUC__ is availble in clang, but they are not found here. Is there a place that has a complete list of all builtin macros in clang?
https://clang.llvm.org/docs/LanguageExtensions.html

Any macros defined by clang can be found with this command:
clang -dM -E -x c /dev/null
And any macros defined by clang++ can be found by this command:
clang++ -dM -E -x c++ /dev/null
Reference: this link

Related

Why gcc linker instead of ld.lld is invoked when target is specified in clang command?

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

what does clang command option -lm mean?

I found someone says that -lm means linking to the math library.
I want to figure out the meaning by using build-in help in the terminal.
So I run the $ clang --help
I don't find any stuff related to -l or -m
How can I get the meaning of -lm with the self-help terminal?
The -l{name} flag tells the linker to link against lib{name}. So -lm links against libm, the c math library.
This isn't a flag to the clang compiler but passed to the linker, which is why you won't find it with clang --help or man clang.
If you run clang with the --verbose flag you will see the invocation of the linker (in my case /usr/bin/ld) as the last step.By running man ld or ld --help (or whichever linker your clang version is using) you will find the documentation.
Keep in mind that not all flags are passed to the linker in this way (-l is probably passed for compatibility with the gcc compiler as melpomene said). To make sure an option is passed, use the -Wl option of clang.

clang target option for riscv64 doesn't work

I'm trying to use clang to make IR for riscv64.
When I use llc --version, it shows many targets including riscv64.
But when I use the following command:
clang -target riscv64 hello.c
clang -target riscv64-unknown-linux hello.c
It shows clang-4.0: error: unknown target triple 'riscv64-unknown-linux', please use -triple or -arch
I'm not using ucb-bar's llvm for riscv. I'm using riscv from upstream of llvm.org.
Did I do something wrong or do I have to do something before building llvm?
Try elf instead of linux.
clang -target riscv32-unknown-elf
clang -target riscv64-unknown-elf
Edit: I created this repo for everyone interested in using RISC-V with LLVM.

Adding run-time shared library search-path to executable at compile-time | clang | Ubuntu

An executable I'm compiling needs the rpath to a library file at runtime. Currently, I'm compiling the executable with,
clang -O3 -mllvm -polly -mllvm -polly-target=gpu vector_add.c -lGPURuntime -ldl
And then using either of the following methods to provide the rpath,
Adding it to LD_LIBRARY_PATH
Using patchelf --set-rpath $RPATH a.out
I need a method to indicate the rpath in the clang .... command itself.
I'm running clang5.0.0-svn(7cf8dd5ce168bed45b57e019149e33300c56f94b) and llvm-svn(85f508cd9dba8a982471d98c4f649fb0d63f3451) with ld.gold in Ubuntu 14.04 x86_64.
Thank You !
Use clang ... -Wl,-rpath,/path/to/run-time/library's/dir/. It's a gcc style option that works in clang too.

Clang linker issues (from source, to gcc-snapshot)

I cannot seem to get this to work. I configured with the --with-gcc-toolchain= where after the equals I put the directory where gcc was (/usr/lib/gcc-snapshot/bin).
I also looked into the answers to " clang linker problem" but I do not see how I can get the accepted answer to find the correct location, and the symlink answer would have worked, except that all of the directories that it is searching exist (copying the lib folder from gcc-snapshot to one of the include locations does not seem to help).
As suggested in the other question, the output of clang++ test.cpp -v:
bob#bob:~/programming$ clang++ test.cpp -v
clang version 3.3 (trunk 171350)
Target: x86_64-unknown-linux-gnu
Thread model: posix
"/home/bob/programming/build/Release+Asserts/bin/clang" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -main-file-name test.cpp -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-linker-version 2.22 -momit-leaf-frame-pointer -v -resource-dir /home/bob/programming/build/Release+Asserts/bin/../lib/clang/3.3 -fmodule-cache-path /var/tmp/clang-module-cache -internal-isystem /usr/local/include -internal-isystem /home/bob/programming/build/Release+Asserts/bin/../lib/clang/3.3/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir /home/bob/programming -ferror-limit 19 -fmessage-length 80 -mstackrealign -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -backend-option -vectorize-loops -o /tmp/test-PWiB4M.o -x c++ test.cpp
clang -cc1 version 3.3 based upon LLVM 3.3svn default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/home/bob/programming/build/Release+Asserts/bin/../lib/clang/3.3/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
test.cpp:1:10: fatal error: 'iostream' file not found
#include
^
1 error generated.
clang version 3.3 (trunk revision 171350)
EDIT: There are no .o files in the Release+Asserts folder for the build. Is this normal? If not, why is this the case, and how can I fix it (if I can point it as clang's stdlib, then that works as well).
Edit 2: It also fails to compile int main(){return 0;} as it can't find crtbegin.o. Full output is as follows:
/usr/bin/ld: cannot find crtbegin.o: No such file or directory
/usr/bin/ld: cannot find -lgcc
/usr/bin/ld: cannot find -lgcc_s
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The results after -v are the same as the above, but with the ending error being different.
Config.log:
The full file can be found here, but the first (non-comment) line is: $ ../llvm/configure --with-gcc-toolchain=/usr/lib/gcc-snapshot/gcc
The issue that we've identified is that gcc was installed with --prefix=/usr/lib/gcc-snapshot configure argument. and llvm was configured with --with-gcc-toolchain=/usr/lib/gcc-snapshot/gcc argument. It should be the same as the gcc installation prefix, i.e. --with-gcc-toolchain=/usr/lib/gcc-snapshot.
The question's current first line says " I configured with the --with-gcc-toolchain= where after the equals I put the directory where gcc was (/usr/lib/gcc-snapshot/bin).", and the current last line says "first (non-comment) line is: $ ../llvm/configure --with-gcc-toolchain=/usr/lib/gcc-snapshot/gcc"
That's puzzling, you might have thought that you had put the flag --with-gcc-toolchain=/usr/lib/gcc-snapshot/bin but you had actually put --with-gcc-toolchain=/usr/lib/gcc-snapshot/gcc
Regardless, I think you should try the following.
remove or rename the build directory (the directory where you ran the configure command, i.e. the directory where you found the config.log file) I assume it's the /home/bob/programming/build directory.
now create /home/bob/programming/build directory again. it should be empty now.
cd /home/bob/programming/build
run the following configure command
../llvm/configure --with-gcc-toolchain=/usr/lib/gcc-snapshot
follow through with the rest of your build process.
The issue seems to be that you didn't specify the gcc-toolchain prefix correctly. It should be the same as the --prefix parameter you used while configuring the gcc.
Report back the results here.
Also see Clang 3.2 build broken after building gcc 4.7

Resources