Clang code coverage

I am trying to create code coverage files for a small C program compiled with clang on Debian Linux. Here is what I did:

neuron@debian :~/temp$ ls main.c test.c test.h neuron@debian :~/temp$ clang *.c neuron@debian :~/temp$ ./a.out 0 

This is exactly as expected, I can compile and run. Now try turning on coverage.

 neuron@debian :~/temp$ clang --coverage *.c /usr/bin/ld: cannot find /usr/bin/../lib/libprofile_rt.a: No such file or directory clang: error: linker command failed with exit code 1 (use -v to see invocation) 

Trying to enable the library for linking.

 neuron@debian :~/temp$ clang --coverage -lprofile_rt *.c /usr/bin/ld: cannot find -lprofile_rt clang: error: linker command failed with exit code 1 (use -v to see invocation) 

Library Search:

 neuron@debian :~/temp$ find / -name \*profile_rt\* 2>/dev/null /usr/lib/llvm-3.0/lib/libprofile_rt.so /usr/lib/llvm-3.0/lib/libprofile_rt.a neuron@debian :~/temp$ clang --coverage -lprofile_rt -L/usr/lib/llvm-3.0/lib *.c /usr/bin/ld: cannot find /usr/bin/../lib/libprofile_rt.a: No such file or directory clang: error: linker command failed with exit code 1 (use -v to see invocation) 

Here's a more detailed output from the last command: http://pastie.org/8468331 . What concerns me there:

  • the linker uses tons of gcc libraries for communication (although this may be the result of llvm not having its own binouts);
  • It /usr/bin/../lib/libprofile_rt.a at the profiling library in /usr/bin/../lib/libprofile_rt.a instead of the path I provided.

If we pass arguments to the linker, the output will be the same:

 neuron@debian :~/temp$ clang --coverage -Wl,-L/usr/lib/llvm-3.0/lib *.c -lprofile_rt /usr/bin/ld: cannot find /usr/bin/../lib/libprofile_rt.a: No such file or directory clang: error: linker command failed with exit code 1 (use -v to see invocation) 

What am I doing wrong?

+6
source share
2 answers

Try changing the order of your link line with

 clang --coverage -lprofile_rt -L/usr/lib/llvm-3.0/lib *.c 

to

 clang --coverage -L/usr/lib/llvm-3.0/lib *.c -lprofile_rt 

Well, it looks like the linker is not getting your -L for some reason. Maybe try

 clang --coverage -Wl,L/usr/lib/llvm-3.0/lib *.c -lprofile_rt 
+2
source

The only way to solve this problem is to create a symlink to where LLVM / clang is looking for the library. I think this is a way for package developers to manage the feature library that the host system uses.

 ln -s /usr/lib/llvm-3.0/lib/libprofile_rt.a /usr/lib/libprofile_rt.a 

Coverage and other optional profile functions -f<***> work as expected. I can verify this by adding the verbose -v switch.

 Debian clang version 3.0-6.2 (tags/RELEASE_30/final) (based on LLVM 3.0) Target: arm-unknown-linux-gnueabihf Thread model: posix "/usr/bin/clang" -cc1 -triple armv4t-unknown-linux-gnueabihf -S -disable-free -disable-llvm-verifier -main-file-name example.c -mrelocation-model static -mdisable-fp-elim -mconstructor-aliases -target-abi apcs-gnu -target-cpu arm1136jf-s -mfloat-abi hard -target-linker-version 2.22 -momit-leaf-frame-pointer -v -femit-coverage-notes -femit-coverage-data -resource-dir /usr/bin/../lib/clang/3.0 -fmodule-cache-path /var/tmp/clang-module-cache -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/clang/3.0/include -internal-externc-isystem /usr/include/arm-linux-gnueabihf -internal-externc-isystem /usr/include -ferror-limit 19 -fmessage-length 130 -fno-signed-char -fgnu-runtime -fobjc-runtime-has-arc -fobjc-runtime-has-weak -fobjc-fragile-abi -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/example-lLKOP1.s -xc example.c clang -cc1 version 3.0 based upon llvm 3.0 hosted on arm-unknown-linux-gnueabihf ignoring nonexistent directory "/usr/bin/../lib/clang/3.0/include" ignoring nonexistent directory "/usr/bin/../lib/clang/3.0/include" ignoring duplicate directory "/usr/local/include" ignoring duplicate directory "/usr/include/arm-linux-gnueabihf" ignoring duplicate directory "/usr/include/arm-linux-gnueabihf" ignoring duplicate directory "/usr/include/arm-linux-gnueabihf" ignoring duplicate directory "/usr/include" #include "..." search starts here: #include <...> search starts here: /usr/local/include /usr/include/arm-linux-gnueabihf /usr/include /usr/include/clang/3.0/include/ /usr/lib/gcc/arm-linux-gnueabihf/4.6/include/ /usr/lib/gcc/arm-linux-gnueabihf/4.6/include-fixed/ End of search list. "/usr/bin/as" -o /tmp/example-WbJHFT.o /tmp/example-lLKOP1.s "/usr/bin/ld" -X --hash-style=both --build-id --eh-frame-hdr -m armelf_linux_eabi -dynamic-linker /lib/ld-linux-armhf.so.3 -o example.o /usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crt1.o /usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crti.o /usr/lib/gcc/arm-linux-gnueabihf/4.6/crtbegin.o -L/usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf -L/lib/arm-linux-gnueabihf -L/usr/lib/arm-linux-gnueabihf -L/usr/lib/gcc/arm-linux-gnueabihf/4.6 -L/usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf -L/usr/lib/gcc/arm-linux-gnueabihf/4.6/../../.. -L/lib/arm-linux-gnueabihf -L/lib -L/usr/lib/arm-linux-gnueabihf -L/usr/lib /tmp/example-WbJHFT.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed \ /usr/lib/gcc/arm-linux-gnueabihf/4.6/crtend.o \ /usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crtn.o \ /usr/bin/../lib/libprofile_rt.a 
+2
source

Source: https://habr.com/ru/post/957749/


All Articles