【问题标题】:Code coverage in clangclang 中的代码覆盖率
【发布时间】:2013-11-21 19:30:41
【问题描述】:

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

我做错了什么?

【问题讨论】:

    标签: c linux clang llvm code-coverage


    【解决方案1】:

    尝试更改链接行的顺序

    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 --coverage *.c /usr/lib/llvm-3.0/lib/libprofile_rt.a"
    • 这也无济于事。看起来--coverage 标志明确指定了要链接到的库的错误路径
    • 是的,看起来有点像。 :-( 试试我回答的最后一个。如果这不起作用,我不知道....
    • 你的意思可能是-Wl,-L/usr/lib/llvm-3.0/lib(带有额外的破折号),它仍然不起作用
    【解决方案2】:

    我能够解决此问题的唯一方法是创建一个指向 LLVM/clang 正在查找库的位置的符号链接。我认为这是包维护者管理主机系统使用哪个功能库的一种方式。

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

    Coverage 和其他可选的-f<***> 配置文件功能按预期工作。我可以通过添加详细的-v 开关来验证这一点。

    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 -x c 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 2012-06-30
      • 2013-10-17
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      相关资源
      最近更新 更多