【发布时间】:2021-07-06 18:33:32
【问题描述】:
我正在尝试让 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 在下面的建议,在 iwyu 命令中添加 sysroot 似乎会使 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>
^~~~~~~~~
【问题讨论】:
-
--sysroot /path/to/gcc-4.9.3/lib/gcc/x86_64-unknown-linux-gnu/4.9.3/?