我第一次尝试让include-what-you-use与我的代码库一起工作。
我的 CentOS6 系统默认安装了 g++ 4.4.7。我将 g++ 4.9.3 用于我的 C++11 项目。这四个选择是不灵活的。
我有效地为我的项目构建了 cmake 数据库,如下所示:
%> export CXX=/path/to/gcc-4.9.3/bin/g++
%> export CC=/path/to/gcc-4.9.3/bin/gcc
%> mkdir $HOME/dev/build
%> cd $HOME/dev/build
%> cmake .. -GNinja -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE=/path/to/iwyu-6/bin/include-what-you-use
当我编译我的项目时,我得到这样的输出:
...
Warning: include-what-you-use reported diagnostics:
In file included from ../dev/src/whatever/foo.cc:13:
In file included from ../dev/src/whatever/foo.h:17:
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/string:42:
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/char_traits.h:41:
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:61:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/cstddef:44:10: fatal error: 'stddef.h' file not found
#include <stddef.h>
^~~~~~~~~~
../dev/src/whatever/foo.h should add these lines:
../dev/src/whatever/foo.h should remove these lines:
- #include <deque> // lines 18-18
The full include-list for ../dev/src/trekcc/pss_dsl/pssTypes.h:
#include <string> // for string
...
伟大的!我不需要deque.h
那个文件,所以这个工具有点工作!
不过,这些警告令人沮丧……当然stddef.h
是在我的实际编译中发现的,因为它实际上是在 gcc-4.9.3 下定义的:
/path/to/gcc-4.9.3/lib/gcc/x86_64-unknown-linux-gnu/4.9.3/include/stddef.h
问:如何更改我的${CMAKE_CXX_INCLUDE_WHAT_YOU_USE}
变量,以使工具如下所示gcc-4.9.3
而不是默认值gcc-4.4.7
?
请注意,如果我要求cmake
导出 JSON 编译命令,则此条目类似于:
{
"directory": "/home/me/dev/build",
"command": "/tools/gnu/gcc-4.9.3/bin/g++ -I../dev/foo -I../dev/bar -I/path/to/boost_1_66_0/include -I/path/to/graphviz-2.38.0/include -I/path/to/graphviz-2.38.0/include/graphviz -I../include -Wall --std=c++11 -Wno-parentheses -fPIC -D_GLIBCXX_USE_CXX11_ABI=0 -g -o dev/whatever/foo.cc.o -c /home/me/dev/src/whatever/foo.cc",
"file": "/home/me/dev/src/whatever/foo.cc"
}
谢谢!
编辑:
根据下面@KamilCuk 的建议,sysroot
在 iwyu 命令中添加 a 似乎会使 4.4.7 警告消失。
%> cmake .. -GNinja -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE="/path/to/iwyu-6/bin/include-what-you-use;-Xiwyu;--mapping_file=../iwyu.imp;--sysroot;/path/to/gcc-4.9.3/lib/gcc/x86_64-unknown-linux-gnu/4.9.3"
它找不到像现在这样的基础知识<map>
,<vector>
但是 4.4.7 警告已经消失,这告诉我我们走在正确的轨道上!
使用--sysroot /path/to/gcc-4.9.3/include/c++/4.9.3
并不能解决这个问题。对于许多常见文件,我收到很多这样的错误:
Warning: include-what-you-use reported diagnostics:
../dev/whatever/foo.cc:12:10: fatal error: 'sstream' file not found
#include <sstream>
^~~~~~~~~