Eri*_*und 45 c++ cmake header-files
该工具include-what-you-use可用于检测不需要的标头.我正在使用CMake来完成我的C++软件项目.如何指示CMake在我的软件项目的源文件上自动运行包含什么?
Eri*_*und 51
CMake 3.3引入了新的目标属性CXX_INCLUDE_WHAT_YOU_USE,可以将其设置为程序的路径include-what-you-use.例如,这个CMakeLists.txt
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
add_executable(hello main.cc)
find_program(iwyu_path NAMES include-what-you-use iwyu)
if(NOT iwyu_path)
message(FATAL_ERROR "Could not find the program include-what-you-use")
endif()
set_property(TARGET hello PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu_path})
Run Code Online (Sandbox Code Playgroud)
能够构建main.cc文件
#include <iostream>
#include <vector>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
同时include-what-you-use发出警告说不需要包含的标题向量.
user@ubuntu:/tmp$ ls ~/hello
CMakeLists.txt main.cc
user@ubuntu:/tmp$ mkdir /tmp/build
user@ubuntu:/tmp$ cd /tmp/build
user@ubuntu:/tmp/build$ ~/cmake-3.3.0-rc2-Linux-x86_64/bin/cmake ~/hello
-- The C compiler identification is GNU 4.9.2
-- The CXX compiler identification is GNU 4.9.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/build
user@ubuntu:/tmp/build$ make
Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/main.cc.o
Warning: include-what-you-use reported diagnostics:
/home/user/hello/main.cc should add these lines:
/home/user/hello/main.cc should remove these lines:
- #include <vector> // lines 2-2
The full include-list for /home/user/hello/main.cc:
#include <iostream> // for operator<<, basic_ostream, cout, endl, ostream
---
[100%] Linking CXX executable hello
[100%] Built target hello
user@ubuntu:/tmp/build$ ./hello
Hello World!
user@ubuntu:/tmp/build$
Run Code Online (Sandbox Code Playgroud)
如果你想传递自定义选项include-what-you-use,比如--mapping_file你可以通过
set(iwyu_path_and_options
${iwyu_path}
-Xiwyu
--mapping_file=${my_mapping})
set_property(TARGET hello
PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu_path_and_options})
Run Code Online (Sandbox Code Playgroud)
Ala*_*son 23
如果您无法访问CMake 3.3,则include-what-you-use附带一个名为iwyu_tool.py的python工具,可以执行您想要的操作.
它的工作原理是解析一个使用CMake轻松生成的clang编译数据库.
假设您的项目已有CMake构建目录,首先需要告诉CMake生成编译数据库:
$ cd build
$ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .
Run Code Online (Sandbox Code Playgroud)
这将生成一个文件,compile_commands.json其中包含项目中每个目标文件的编译器调用.您无需重建项目.
您现在可以include-what-you-use通过在构建目录上运行python工具来运行您的项目:
$ python /path/to/iwyu_tool.py -p .
Run Code Online (Sandbox Code Playgroud)
以下代码段可用于将iwyu目标添加到cmake项目.
# Generate clang compilation database
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(PythonInterp)
find_program(iwyu_tool_path NAMES iwyu_tool.py)
if (iwyu_tool_path AND PYTHONINTERP_FOUND)
add_custom_target(iwyu
ALL # Remove ALL if you don't iwyu to be run by default.
COMMAND "${PYTHON_EXECUTABLE}" "${iwyu_tool_path}" -p "${CMAKE_BINARY_DIR}"
COMMENT "Running include-what-you-use tool"
VERBATIM
)
endif()
Run Code Online (Sandbox Code Playgroud)
该include-what-you-use二进制需要在你的路径任何上述的正常工作.
小智 6
您还可以通过设置cmake变量在cmake脚本之外全局启用它:
cmake -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE="iwyu" <builddir>
Run Code Online (Sandbox Code Playgroud)
然后它将在每个CXX目标上调用它.
| 归档时间: |
|
| 查看次数: |
13648 次 |
| 最近记录: |