使用 glibc 2.7 编译包含路径失败

Compilation with glibc 2.7 include path fails

我已经在我的 SLES 12.3 g++ 7.3 上编译到 /FaF 目录中,并且 glibc 2.27 安装到 /FaF/glibc 目录。

g++ -c testAbs.cpp -I /FaF/glibc/include 编译这个非常简单的程序非常失败:

#include <cstdlib>
#include <cmath>
int main( int argc, char * argv [] )
{
    if ( argc != 2 )
    {
        return 1;
    }
    return std::abs(std::atoi(argv[1]));
}

See the long error list。下面我插入了前 15 行 - 总共有超过 300 行错误消息。

g++ -c testAbs.cpp编译它工作正常。在这种情况下,它使用系统 glibc 2.22 include files.

我发现它一定与 g++ 7.3 安装有关。使用系统 g++ 4.8.5 可以正常编译 /usr/bin/g++-4.8 -c testAbs.cpp -I /FaF/glibc/include

使用 glibc 2.27 包含目录的路径用 g++ 7.3 编译它有什么可怕的错误?

我搞砸了什么?

In file included from /usr/include/math.h:48:0,
                 from /FaF/include/c++/7.3/cmath:45,
                 from testAbs.cpp:11:
/FaF/glibc/include/bits/mathdef.h:19:3: error: #error "Never use <bits/mathdef.h> directly; include <complex.h> instead"
 # error "Never use <bits/mathdef.h> directly; include <complex.h> instead"
   ^~~~~
In file included from /FaF/include/c++/7.3/cstdlib:75:0,
                 from testAbs.cpp:10:
/usr/include/stdlib.h:95:1: error: ‘__BEGIN_NAMESPACE_STD’ does not name a type; did you mean ‘__BEGIN_DECLS’?
 __BEGIN_NAMESPACE_STD
 ^~~~~~~~~~~~~~~~~~~~~
 __BEGIN_DECLS
/usr/include/stdlib.h:101:5: error: ‘div_t’ does not name a type; did you mean ‘size_t’?
   } div_t;
     ^~~~~

问题中的示例是我的包含文件的最小化版本。这些都是项目中需要的包含文件:

#include <algorithm>
#include <atomic>
#include <chrono>
#include <cmath>
#include <condition_variable>
#include <cstdlib>
#include <forward_list>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <mutex>
#include <regex>
#include <set>
#include <string>
#include <thread>
#include <vector>
#include <execinfo.h>
#include <gnu/libc-version.h>
#include <libgen.h>
#include <locale.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/poll.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/sysinfo.h>
#include <unistd.h>
#include <mysql/my_global.h>
#include <mysql/mysql.h>

首先,它无法按此顺序编译,我无法使用 cmath 包含 - 由于问题中描述的错误。

我可以使用这些 g++ 开关修复它:

-nostdinc
-I /FaF/curl/include
-I /usr/include/mysql
-I /FaF/lib64/gcc/x86_64-suse-linux/7.3.0/include-fixed
-I /FaF/include/c++/7.3
-I /FaF/include/c++/7.3/x86_64-suse-linux/
-I /FaF/lib64/gcc/x86_64-suse-linux/7.3.0/include
-I /FaF/glibc/include/
-I /usr/include

g++ -xc++ -E -v - 帮助我找出需要哪些包含路径。

顺序很重要

-I /usr/include 必须排在最后!