【问题标题】:Compilation with glibc 2.7 include path fails使用 glibc 2.7 编译包含路径失败
【发布时间】:2018-08-14 22:12:53
【问题描述】:

我已在我的 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

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

我在搞砸什么?

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;
     ^~~~~

【问题讨论】:

  • 首先,这是C++程序,请用g++ -c ...编译。此外,您的 2.27 标头正在获取 /usr/include/stdlib.h 标头,所以我猜您正在以某种奇怪的方式弄乱两个 glibc 系统。
  • 很高兴知道。另一个问题是在运行时使用什么动态链接器等。关心在可执行文件上运行 ldd 吗?它可能会偶然起作用 - 当前 libc 设置中的动态 lnker 可能仅与 2.27 兼容
  • @SeverinPappadeux 我正在使用很多 rpath 指令。没有它,就没有办法让它发挥作用。
  • 好,很高兴听到。

标签: c++ linux compilation glibc


【解决方案1】:

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

#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 必须在列表的最后!

【讨论】:

  • 下单有原因吗?通常这不重要。你有进一步调查吗?
  • @Carsten 是的,我开了一张票,但没有人真正想从 GNU 开发中帮助我。我让它保持原样......
猜你喜欢
  • 1970-01-01
  • 2013-10-04
  • 2015-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
  • 2015-06-24
相关资源
最近更新 更多