关于编译Boost库时出现typedef unused的warning的解决办法
之前在写代码后编译boost库时发现一个很神奇的现象,当我用g++默认的头文件寻址方式(即gcc后面不指定-I,从系统库中寻址),boost库的头文件就不会报warning,而当我用-I来指定boost库头文件的路径时,则会出现大量的warning。
打个比方
#include <iostream>
#include <boost/polygon/interval_concept.hpp>
using namespace std;
int main(void)
{
int a, b;
cout << "Hello World!" << endl;
return 0;
}
该C++程序直接用g++编译,不指定-I头文件寻址,
[[email protected] Desktop]$ g++ -g -Wall Test.cpp -o Test
[[email protected] Desktop]$ ./Test
Hello World!
没有任何问题。但如果我们把boost库从系统库(及/usr/include)中copy出来,然后再用-I选项来指定优先寻址路径,则会出现typedef unused的warning。
[[email protected] Desktop]$ g++ -g -Wall -I/home/reol/Desktop Test.cpp -o Test
In file included from Test.cpp:2:0:
/home/reol/Desktop/boost/polygon/interval_concept.hpp: In function ‘typename boost::enable_if<typename boost::polygon::gtl_and<boost::polygon::y_i_scale_down, typename boost::polygon::is_mutable_interval_concept<typename boost::polygon::geometry_concept<T>::type>::type>::type, IntervalType>::type& boost::polygon::scale_down(IntervalType&, typename boost::polygon::interval_coordinate_type<IntervalType>::type)’:
/home/reol/Desktop/boost/polygon/interval_concept.hpp:389:65: warning: typedef ‘Unit’ locally defined but not used [-Wunused-local-typedefs]
typedef typename interval_coordinate_type<IntervalType>::type Unit;
warning虽然不影响我们的编译结果,但在大工程中并不美观,如果我们把安装包发给客户,客户make一下发现了一大堆warning,肯定会觉得这个程序可靠性堪忧。
为了了解这个问题的本质,我解包了boost库的rpm安装包,发现boost库的安装包中有许多用来解决typedef unused的patch补丁。也就是说这些warning是boost库在开发过程中留下的问题,是boost库本身的缺陷而不是我们的使用不当。
那为什么我们不指定-I时就没有warning了呢?我去查阅gcc的手册之后发现了这么一段内容:
Add the directory dir to the head of the list of directories to be searched for header files. This can be used to override a system header file, substituting your own version, since these directories are searched before the system header file directories. However, you should not use this option to add directories that contain vendor-supplied system header files (use ‘-isystem’ for that). If you use more than one ‘-I’ option, the directories are scanned in left-to-right order; the standard system directories come after.
If a standard system include directory, or a directory specified with ‘-isystem’, is also specified with ‘-I’, the ‘-I’ option is ignored. The directory is still searched but as a system directory at its normal position in the system include chain. This is to ensure that GCC’s procedure to fix buggy system headers and the ordering for the include_next directive are not inadvertently changed. If you really need to change the search order for system directories, use the ‘-nstdinc’ and/or ‘-isystem’ options.
以上文字解释了GCC的-I命令的执行原理,以及造成warning问题的原因。简单的来讲就是GCC在编译系统库时会自动修复一些bug,而用-I指定的第三方库则不行,解决的办法就是把-I改为-isystem。
[[email protected] Desktop]$ g++ -g -Wall -isystem/home/reol/Desktop Test.cpp -o Test