G ++和clang ++在构建共享库时与标准库不兼容? [英] G++ and clang++ incompatibility with standard library when building shared libraries?

查看:1806
本文介绍了G ++和clang ++在构建共享库时与标准库不兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个文件clang.cpp包含:

If I have a file clang.cpp containing:

#include <map>
void myfunc() {
    std::map<int, int> mymap;
    const int x = 20;
    myfoo[x] = 42;
}

和main.cpp包含:

and main.cpp containing:

void myfunc();
int main() { myfunc(); }

编译 clang ++ -g clang.cpp -shared -fPIC -o libclang .so -stdlib = libstdc ++ -std = c ++ 11 clang ++ -g main.cpp -L -Wl。, - rpath =。 -lclang -lstdc ++ -o a.out -stdlib = libstc ++ -std = c ++ 11 将运行得很好。

添加gcc.cpp包含:

However, if I add gcc.cpp containing:

#include <tuple>
template std::pair<int const, int>::pair(std::piecewise_construct_t, std::tuple<int const&>, std::tuple<>);

然后使用 g ++ -g gcc.cp -shared -fPIC -o libgcc.so ,并将链接命令更改为 clang ++ -g main.cpp -L -Wl。, - rpath =。 -lgcc -lclang -stdlib = libstdc ++ -std = c ++ 11 -o a.out ,则运行 ./ a.out 错误。

then also compile that to a shared library using g++ -g gcc.cp -shared -fPIC -o libgcc.so and change the linking command to clang++ -g main.cpp -L -Wl.,-rpath=. -lgcc -lclang -stdlib=libstdc++ -std=c++11 -o a.out, then running ./a.out will segment fault.

我不知道该怎么做,因为clang和gcc在使用相同的c ++标准库时应该是ABI兼容的。我的版本是3.6.2的clang,5.2.1的gcc,与ubuntu一起提供。

I don't know what to make of this, since clang and gcc are supposed to be ABI compatible when using the same c++ standard library. My versions are 3.6.2 for clang, 5.2.1 for gcc, as shipped with ubuntu.

推荐答案

给定

int f(std::tuple<const int &> t){
  return std::get<0>(t);
}

Clang生成

f(std::tuple<int const&>):                    # @f(std::tuple<int const&>)
        movl    (%rdi), %eax
        retq

而GCC产生

f(std::tuple<int const&>):
        movq    (%rdi), %rax
        movl    (%rax), %eax
        ret

换句话说,Clang期望元组本身通过寄存器传递,而GCC期望地址在寄存器中传递(并且元组在堆栈上传递)。

In other words, Clang is expecting the tuple itself to be passed by register, while GCC is expecting the address to be passed in register (and the tuple passed on the stack).

混合搭配,你会得到有趣的结果。

Mix and match and you get "fun" results.

这篇关于G ++和clang ++在构建共享库时与标准库不兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆