what does clang command option -lm mean? - clang

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.

Related

linker option order causing opencv undefined references [duplicate]

I'm writing a small C program that uses librt. I'm quite surprised that the program won't compile if I place the link flag at the start instead of at the end:
At the moment, to compile the program I do:
gcc -o prog prog.c -lrt -std=gnu99
If I were to do the following, it will fail to find the functions in librt:
gcc -std=gnu99 -lrt -o prog prog.c
Yet, this works with other libraries. I found the issue when attempting to use a simple Makefile. make actually compiled prog.c without liking first (using -c flag) and then did the linking.
This is the Makefile:
CC = gcc
CFLAGS = -std=gnu99
LIBS= -lrt
LDFLAGS := -lrt
prog: prog.o
$(CC) -o prog prog.c -lrt -std=gnu99
The output I would get when typing make would be:
gcc -std=gnu99 -c -o prog.o prog.c
gcc -lrt prog.o -o prog
prog.o: In function `main':
prog.c:(.text+0xe6): undefined reference to `clock_gettime'
prog.c:(.text+0x2fc): undefined reference to `clock_gettime'
collect2: ld returned 1 exit status
make: *** [buff] Error 1
I have now crafted a Makefile that puts the linking at the end of the gcc line, however I'm puzzled why it doesn't work if the linking flag is at the start.
I would appreciate if anybody can explain this to me. Thanks.
As the linker processes each module (be it a library or a object file), it attempts to resolve each undefined symbol while potentially adding to its list of undefined symbols. When it gets to the end of the list of modules, it either has resolved all undefined symbols and is successful or it reports undefined symbols.
In your case, when it processed librt, it had no undefined symbols. Processing proc resulted in clock_gettime being an undefined symbol. gcc will not go back and look in librt for the undefined symbols.
For that reason, you should always have your code first, followed by your libraries, followed by platform provided libraries.
Hope this helps.
From the ld (the GNU linker) documentation (http://sourceware.org/binutils/docs/ld/Options.html#Options):
The linker will search an archive only once, at the location where it is specified on the command line. If the archive defines a symbol which was undefined in some object which appeared before the archive on the command line, the linker will include the appropriate file(s) from the archive. However, an undefined symbol in an object appearing later on the command line will not cause the linker to search the archive again.
So if you specify the library too early, the linker will scan it, but not find anything of interest. Then the linker moves on to the object file produced by the compiler and finds references that need to be resolved, but it has already scanned the library and won't bother looking there again.

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.

How to add linker flags to cmake properly?

I am building opencv-3.1.0 and I want to use ffmpeg, which is installed in custom path /media/sdcard/usr/lib, /media/sdcard/usr/include.
Normally, linker gives me an error:
/usr/lib/gcc/i586-poky-linux/4.9.1/../../../../i586-poky-linux/bin/ld: cannot find -lavcodec
So I gave cmake some additional flags: -DCMAKE_SHARED_LINKER_FLAGS="--library-path /media/sdcard/usr/lib", DCMAKE_INCLUDE_PATH=/media/sdcard/usr/include.
Include seems not to work at all, but as for linker flags, cmake gave me the following output:
Linker flags (Release): --library-path /media/sdcard/usr/lib
Linker flags (Debug): --library-path /media/sdcard/usr/lib
But when I tried to run make, I got an error:
c++: error: unrecognized command line option '--library-path'
My ld knows '--library-path' flag and finds necessary libraries, e.g. if I run
ld -lavcodec --library-path /media/sdcard/usr/lib --verbose
it gives me
attempt to open /media/sdcard/usr/lib/libavcodec.a succeeded
I may be missing some ground understanding of the whole process.

How to create executable and IR at the same time

I would like to create both the executable and LLVM IR using clang at the same time. Is there a way to do that?
I'm currently using
clang -flto -Wl,-plugin-opt=also-emit-llvm -o foo foo.c
and get the error
clang: error: unknown argument: '-plugin-opt=also-emit-llvm'
with a -v invocation, I see that
/usr/bin/ld: unrecognized option '-plugin'
You can do both quite easily. Take a look at the tool:
https://github.com/SRI-CSL/whole-program-llvm

How to make clang stop before a specified LLVM pass and dump the LLVM IR

How do I run clang and have it stop just before a pass, say loop-vectorize, and dump the IR to an .ll file that can be later fed to opt?
opt has a -stop-after= option, but Clang seems to be missing the equivalent option. Here is a failed attempt with Clang 3.7.0rc2:
$ ../build/bin/clang -O2 -mllvm -stop-after=loop-vectorize a.cpp
clang (LLVM option parsing): Unknown command line argument '-stop-after=loop-vectorize'. Try: 'clang (LLVM option parsing) -help'
clang (LLVM option parsing): Did you mean '-print-after=loop-vectorize'?
I've also tried running clang -O0 -emit-llvm -S and then running opt -O2, but the results were different than running clang -O2 directly.
I'm not aware of any way to stop after a specific pass when compiling with Clang, but instead I can offer a hopefully helpful alternative.
First, to address opt and Clang producing different IR files, it may be helpful to compare the pass lists for clang -O2 and opt -O2 manually. This can be done for both by passing -debug-pass=Arguments. When running Clang you will need -mllvm to pass the argument along.
Having done this myself it appears that a different set of passes are being run for each of them but I would suggest confirming for yourself.
To address your initial question, you might simply copy the list of passes run during -O2 only up through loop-vectorize and simply run opt manually passing it the reduced list.

Resources