Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code coverage in clang

I'm trying to generate code coverage files for a small C program compiled with clang on Debian Linux. Here's what I've done:

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 things. Now trying to enable 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 include 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)

Finding the library:

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 more verbose output of the last command: http://pastie.org/8468331. What concerns me there:

  • the linker uses tons of gcc libraries to link with (though this may be a result of llvm not having it's own binunitls);
  • profiling library is being searched for at /usr/bin/../lib/libprofile_rt.a instead of the path I provided.

If we pass the arguments to the linker the output is 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 do I do wrong?

like image 214
Sergey Mikhanov Avatar asked Nov 09 '13 19:11

Sergey Mikhanov


People also ask

What is GCOV code coverage?

Gcov is a source code coverage analysis and statement-by-statement profiling tool. Gcov generates exact counts of the number of times each statement in a program is executed and annotates source code to add instrumentation. Gcov comes as a standard utility with the GNU Compiler Collection (GCC) suite.

How do you test clang?

The cmake build tool is set up to create Visual Studio project files for running the tests, "clang-test" being the root. Therefore, to run the test from Visual Studio, right-click the clang-test project and select "Build".

How do you use LLVM COV?

To use llvm-cov gcov, you must first build an instrumented version of your application that collects coverage data as it runs. Compile with the -fprofile-arcs and -ftest-coverage options to add the instrumentation. (Alternatively, you can use the --coverage option, which includes both of those other options.)

What is clang programming?

clang is a C, C++, and Objective-C compiler which encompasses preprocessing, parsing, optimization, code generation, assembly, and linking. Depending on which high-level mode setting is passed, Clang will stop before doing a full link.


1 Answers

Try changing the order of your link line from

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

Ok, doesn't seem like the linker is getting your -L for some reason. Maybe try

clang --coverage  -Wl,L/usr/lib/llvm-3.0/lib *.c -lprofile_rt
like image 104
Charlie Burns Avatar answered Sep 29 '22 20:09

Charlie Burns