2

我第一次尝试让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>
         ^~~~~~~~~
4

1 回答 1

0

可能您应该只使用工具链文件。通常在我的项目中(尤其是交叉编译),我会创建一个专用文件夹,用于放置我所有的工具链和其他 cmake 命令。

my_proj
    cmake 
        other_gcc.cmake
    CMakeLists.txt
    ...

例如,这是other_gcc.cmake我用于交叉编译的一个版本:

# Define our host system
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR aarch64)

# Define the cross compiler locations
set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)

# Define the sysroot path for the RaspberryPi distribution in our tools folder
#set(CMAKE_FIND_ROOT_PATH $ENV{AARCH64_LINUX_TOOLS_DIR}/aarch64-linux-gnu/sysroot)

# Use our definitions for compiler tools
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

这与您相关,因为您还需要更改编译器。可能您可以CMAKE_SYSTEM_为您的用例注释掉前 3 个命令。

然后在配置步骤中,您只需告诉 Cmake 您要使用此工具链文件:

cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/other_gcc.cmake ..
于 2021-07-16T08:26:19.540 回答