clang中的代码覆盖率

Ser*_*nov 6 c linux code-coverage llvm clang

我正在尝试为在Debian Linux上使用clang编译的小型C程序生成代码覆盖文件.这就是我所做的:

neuron@debian:~/temp$ ls
main.c  test.c  test.h
neuron@debian:~/temp$ clang *.c
neuron@debian:~/temp$ ./a.out 
0
Run Code Online (Sandbox Code Playgroud)

这完全符合预期,我可以编译和运行.现在尝试启用覆盖率.

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)
Run Code Online (Sandbox Code Playgroud)

试图包含用于链接的库.

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)
Run Code Online (Sandbox Code Playgroud)

找到图书馆:

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)
Run Code Online (Sandbox Code Playgroud)

这是最后一个命令的详细输出:http://pastie.org/8468331.那里有什么问题:

  • 链接器使用大量的gcc库进行链接(尽管这可能是因为llvm没有自己的binunitls);
  • 正在搜索分析库/usr/bin/../lib/libprofile_rt.a而不是我提供的路径.

如果我们将参数传递给链接器,则输出是相同的:

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)
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

Cha*_*rns 2

尝试更改链接线的顺序

clang --coverage -lprofile_rt -L/usr/lib/llvm-3.0/lib *.c
Run Code Online (Sandbox Code Playgroud)

clang --coverage  -L/usr/lib/llvm-3.0/lib *.c -lprofile_rt
Run Code Online (Sandbox Code Playgroud)

好吧,似乎链接器由于某种原因没有得到你的 -L 。也许尝试一下

clang --coverage  -Wl,L/usr/lib/llvm-3.0/lib *.c -lprofile_rt
Run Code Online (Sandbox Code Playgroud)