天天看点

gcc limits.h的bug定位看include_next和默认搜索路径

手编的交叉编译工具链经常报一堆宏未定义,例如下面是编译gtest的时候报_POSIX_PATH_MAX宏未定义,有时还会上报SSIZE_MAX等宏未定义:

googletest/src/gtest-filepath.cc: In static member function ‘static testing::internal::FilePath testing::internal::FilePath::GetCurrentDir()’:

googletest/src/gtest-filepath.cc:61:26: error: ‘_POSIX_PATH_MAX’ was not declared in this scope

# define GTEST_PATH_MAX_ _POSIX_PATH_MAX

^

googletest/src/gtest-filepath.cc:111:12: note: in expansion of macro ‘GTEST_PATH_MAX_’

char cwd[GTEST_PATH_MAX_ + 1] = { '\0' };

googletest/src/gtest-filepath.cc:112:25: error: ‘cwd’ was not declared in this scope

char* result = getcwd(cwd, sizeof(cwd));

经查,SSIZE_MAX和_POSIX_PATH_MAX都定义在include/bits/posix1_lim.h,理论上会有include/limits.h文件会包含,而但实际未生效。下面是gtest-filepath.cc里面的头文件包含定义,可以看出对普通linux而已做47行增加了limits.h的包含。

gcc limits.h的bug定位看include_next和默认搜索路径

搜索了一下交叉编译工具链的目录,有7处limits.h的定义,其中/opt/efb/efb_x86_64_gcc-5.5.0_glibc-2.17_linux/include/limits.h和/opt/efb/efb_x86_64_gcc-5.5.0_glibc-2.17_linux/x86_64-linux-gnu/include/limits.h里面还有include_next的用法来二次包含limits.h文件

/opt/efb/efb_x86_64_gcc-5.5.0_glibc-2.17_linux/lib/gcc/x86_64-linux-gnu/5.5.0/include-fixed/limits.h

/opt/efb/efb_x86_64_gcc-5.5.0_glibc-2.17_linux/lib/gcc/x86_64-linux-gnu/5.5.0/install-tools/include/limits.h

/opt/efb/efb_x86_64_gcc-5.5.0_glibc-2.17_linux/include/linux/limits.h

/opt/efb/efb_x86_64_gcc-5.5.0_glibc-2.17_linux/include/limits.h

/opt/efb/efb_x86_64_gcc-5.5.0_glibc-2.17_linux/x86_64-linux-gnu/include/c++/5.5.0/tr1/limits.h

/opt/efb/efb_x86_64_gcc-5.5.0_glibc-2.17_linux/x86_64-linux-gnu/include/linux/limits.h

/opt/efb/efb_x86_64_gcc-5.5.0_glibc-2.17_linux/x86_64-linux-gnu/include/limits.h

下面是/opt/efb/efb_x86_64_gcc-5.5.0_glibc-2.17_linux/include/limits.h和/opt/efb/efb_x86_64_gcc-5.5.0_glibc-2.17_linux/x86_64-linux-gnu/include/limits.h文件里面的摘录,原来123行是没有注释的。

gcc limits.h的bug定位看include_next和默认搜索路径

通过在上面这2个头文件里面打点(增加 #error 编译宏)发现上面这2个头文件实际上默认都没用,但如果修改include-fixed目录下面的limits.h文件的名称,编译过程中才会实际引用到include/limits.h,但include_next都二次搜索又会报找不到limits.h的错误。

从这个现象推断,交叉编译工具链优先包含的时候include-fixed下面的头文件,include_next是对头文件搜索路径的二次搜索。

通过注释掉/opt/efb/efb_x86_64_gcc-5.5.0_glibc-2.17_linux/include/limits.h和/opt/efb/efb_x86_64_gcc-5.5.0_glibc-2.17_linux/x86_64-linux-gnu/include/limits.h里面的include_next定义,并在/opt/efb/efb_x86_64_gcc-5.5.0_glibc-2.17_linux/lib/gcc/x86_64-linux-gnu/5.5.0/include-fixed/limits.h中增加include_next定义之后故障解决。