clang 中的代码覆盖率

标签 c linux clang llvm code-coverage

我正在尝试为在 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

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

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)

尝试包含用于链接的库。

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)

寻找图书馆:

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)

这是最后一个命令的更详细的输出: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)

我做错了什么?

最佳答案

尝试更改链接行的顺序

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

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

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

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

关于clang 中的代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19881928/

相关文章:

c - 为什么 strcmp 在 c 中不起作用?

c++ - 无用的反斜杠会产生定义明确的字符串常量吗?

php - Symfony2 添加新路由后返回空白页

linux - 使用 awk 更改文件的特定行

formatting - 在没有 -style=file 选项的情况下调用 clang-format 的默认行为是什么?

shell - 如何在 fish shell 中使用compile_commands.json?

c - INT_MAX+1 = INT_MIN 是有符号整数吗?

c - 在 if 语句中声明变量 (ANSI C)

java - Symfony2 Assetic yuicompressor 未知错误

clang - 如何使用 Clang 获取当前版本的 AST?