I'm trying to get include-what-you-use working with my codebase for the first time.
My CentOS6 system has its default of g++ 4.4.7 installed. I use g++ 4.9.3 for my C++11 project. Those four choices are inflexible.
I effectively build the cmake database for my project like this:
%> 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
When I compile my project, I get output like this:
...
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
...
Great! I don't need deque.h
in that file, so the tool is somewhat working!
The warnings are discouraging though... Of course stddef.h
is found by my actual compilation because it is actually defined below gcc-4.9.3 at:
/path/to/gcc-4.9.3/lib/gcc/x86_64-unknown-linux-gnu/4.9.3/include/stddef.h
Q: How can I change my ${CMAKE_CXX_INCLUDE_WHAT_YOU_USE}
variable so that the tool looks below gcc-4.9.3
instead of the default of gcc-4.4.7
?
Note that if I ask cmake
to export the JSON compile commands, this entry looks something like:
{
"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"
}
Thanks!
edit:
Per @KamilCuk's suggestion below, adding a sysroot
to the iwyu command seems to make the 4.4.7 warning go away.
%> 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"
It cannot find basics like <map>
or <vector>
now, but the 4.4.7 warning has gone away, which tells me we're on the right track!
Using --sysroot /path/to/gcc-4.9.3/include/c++/4.9.3
doesn't solve that problem. I get many errors like this, for many common files:
Warning: include-what-you-use reported diagnostics:
../dev/whatever/foo.cc:12:10: fatal error: 'sstream' file not found
#include <sstream>
^~~~~~~~~