如何使用 Clang 编译器和 CMake 进行分析

use*_*362 3 c++ profiling cmake clang

问题

\n

1/output当我想使用clang编译器进行分析时,我应该期待什么?\n2/对于使用编译器CMake` 作为构建工具的程序
,我该如何做?profilingC++ projectclangand

\n

重新分析我所使用的内容

\n

1/ 首先,我使用valgrind工具来检查cpp executable.
\n2/ 后来,我使用g++ compiler并经历了这个,我看到了使用gprof. 使用gprof我已经通过命令行完成的事情。从这个来源我了解到gprof可以提供文本文件的输出(称为analysis.txt),其中写入了函数调用号、执行时间等。

\n

我迄今为止所采取的目标和方法

\n
    \n
  • 现在,在我的项目中,我只能使用clang编译器和CMake构建工具。我读过 clang 文档,主要是这个和这个
  • \n
  • 在第一次试验中,我将两个放在cpp file同一个目录中(这显然不是所需的项目结构),并按照以下命令查看 Instrumentation 是如何完成的以及outcome接下来会发生什么
  • \n
\n
clang++-10 -fprofile-instr-generate -fcoverage-mapping test_gprof.cpp test_gprof_new.cpp -o code_coverage\nLLVM_PROFILE_FILE="code_coverage.profraw" ./code_coverage\nllvm-profdata merge -sparse code_coverage.profraw -o code_coverage.profdata\nllvm-cov show -show-line-counts-or-regions --show-regions=1 --show-expansions ./code_coverage -instr-profile=code_coverage.profdata\nllvm-cov report ./code_coverage -instr-profile=code_coverage.profdata\n
Run Code Online (Sandbox Code Playgroud)\n

我真的不确定我是否遵循了正确的步骤,但我希望看到一些analysis统计数据。

\n
    \n
  • 终于,我看到了一个report我什么都看不懂的东西。在这里,我想到的第一个问题是,当我这样做时,我到底能期待什么profiling
  • \n
  • 而且,我不知道如何在CMakeclang 编译器中激活这个分析过程。下面给出了一个与真实文件夹结构相似的虚拟文件夹结构
  • \n
\n
clang_profile_cmake/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 CMakeLists.txt\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 example\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.cpp\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 include\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 test_gprof.h\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test_gprof_new.h\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 test_gprof.cpp\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test_gprof_new.cpp\n
Run Code Online (Sandbox Code Playgroud)\n

最近搜索(仍然无法通过CMake、Clang编译器生成任何分析数据)

\n
    \n
  • 我必须将 LLVM 添加到我的项目中。我已经关注了这个
  • \n
  • 我正在使用的CMakeLists.txt 文件可在此处找到
  • \n
  • 不明白如何/在哪里/在哪一步中启用分析标志(、、-fprofile-instr-generate -fcoverage-mapping...等)LLVM_PROFILE_FILEllvm-profdata merge
  • \n
\n

use*_*362 5

我发现,我必须学习更多如何处理,CMake因为总答案就在我面前,现在我刚刚在 CMake 中添加了这些命令。

clang++-10 -fprofile-instr-generate -fcoverage-mapping test_gprof.cpp test_gprof_new.cpp -o code_coverage

以下是当前的 CMake 文件

# Set Clang Compiler
set(CMAKE_CXX_COMPILER "/usr/bin/clang++-10")
set(CMAKE_CXX_COMPILER clang++-10)

# Set llvm clang instrumentation compile flags
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping")

# Project name
project(clang_profiling_cmake)

# Next will be filled with common CMake Pattern
Run Code Online (Sandbox Code Playgroud)

要构建或查看分析步骤,可以使用以下脚本。就我而言,我已将输出可执行文件名称设置为clang_prof_exec.

rm -rf bin/ build/ lib/
mkdir build
cd build
cmake ..
make
cd ../bin

# creation of profraw file by executing the executable binary
LLVM_PROFILE_FILE="clang_prof_exec.profraw" ./clang_prof_exec

# Creation of profile data
llvm-profdata merge -sparse clang_prof_exec.profraw -o clang_prof_exec.profdata

# following commands are needed to investigate profiling output. Use any of these
llvm-cov show ./clang_prof_exec -instr-profile=clang_prof_exec.profdata
llvm-cov report ./clang_prof_exec -instr-profile=clang_prof_exec.profdata
Run Code Online (Sandbox Code Playgroud)