【问题标题】:include-what-you-use with non-default g++ versioninclude-what-you-use 与非默认 g++ 版本
【发布时间】: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"

它现在找不到像 &lt;map&gt;&lt;vector&gt; 这样的基本信息,但 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/?

标签: c++ cmake iwyu


【解决方案1】:

也许您应该只使用工具链文件。通常在我的项目中(尤其是交叉编译),我会创建一个专门的文件夹来放置我的所有工具链和其他 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)

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

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

cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/other_gcc.cmake ..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    相关资源
    最近更新 更多