g ++ -std = c ++ 0x和兼容性 [英] g++ -std=c++0x and compatibility

查看:162
本文介绍了g ++ -std = c ++ 0x和兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用g ++ 4.4在linux上编译共享库。如果我可以在库中使用某些C ++ 11功能,但是我无法更新编译器的版本,也不需要为我的库的用户使用任何特殊的编译器开关。



我有两个问题,我无法找到一个明确的答案。


  1. 如果我编译共享库与-std = c ++ 0x或-std = g ++ 0x,我保证使用我的库的程序不需要这些开关(假设我在头文件中没有c ++ 0x功能)?它看起来很有效,但我不希望在后面注册一些微妙的问题。

  2. 4.4是相当不完整的。由于标准库的大部分内容仅包含头文件,并且gnu的头文件通常是完整版本的ifdefs,所以我认为可能有一种方法可以使用libstdc ++中至少包含头文件的更新版本。不过,我不能使用不同的.so。我敢肯定,我可以把它们混在一起,但是能够正确地做到这一点吗? 谢谢。

    解决方案


    1。如果我使用-std = c ++ 0x或-std = g ++ 0x编译共享库,我是否保证使用我的库的程序不需要这些开关(前提是我没有c ++ 0x功能头文件)?它似乎有效,但我不希望在注册时遇到细微的问题。


    支持C ++ 11在GCC 4.x版本中仍然是实验性的(它不再是GCC 5以后的实验)。尽管我们试图让事情顺利进行,但答案是否定的,您通常不能保证在任何情况下都能正常工作。由于使用 -std = c ++ 0x 导致了许多ABI更改,这些更改可能会导致混合使用C ++ 03代码和C ++ 11代码的程序出现问题,有关更多详细信息,请参阅 http://gcc.gnu.org/wiki/Cxx11AbiCompatibility 。如果您的图书馆没有导出该页面上描述的任何符号,那么您应该没问题。


    2。 g ++ 4.4中的C ++ 11标准库相当不完整。由于标准库的大部分内容仅包含头文件,并且gnu的头文件通常是完整版本的ifdefs,所以我认为可能有一种方法可以使用libstdc ++中至少包含头文件的更新版本。不过,我不能使用不同的.so。我确信我可以一起把它们混为一谈,但能否正确地做这样的事情?

    不,绝对没有任何可能的工作机会。来自更高版本的头文件使用4.4不支持的功能,即使您可以使用它们,您也需要使用更新的 libstdc ++。so 。只是没有。



    标题是 完整版本 #ifdefs ,几乎只有当你使用 -std = c ++ 0x 时,你会发现检查 __ GXX_EXPERIMENTAL_CXX0X __ >但这并不意味着你的4.4版本支持lambdas,非静态数据成员初始值设定项,正确的右值引用语义,默认/删除函数等等,稍后头文件可以自由使用。 必须使用libstdc ++头文件与他们自带的相同版本的GCC。



    总之,如果你想要适当的C ++ 11支持需要使用更新的编译器。



    如果您不能使用更新的编译器,您将无法获得适当的C ++ 11支持。


    I'm using g++ 4.4 to compile a shared library on linux. I would like to use some C++11 features if I can in the library, but I cannot update the version of the compiler or require any special compiler switches for users of my library.

    I have two questions and I'm having trouble finding a definitive answer.

    1. If I compile a shared library with -std=c++0x or -std=g++0x, am I guaranteed that a program that uses my library doesn't need those switches (provided I have no c++0x features in the header files)? It seems to work, but I don't want to be signing up for subtle problems down the road.

    2. The standard library for C++11 in g++ 4.4 is quite incomplete. Since much of the standard library is header-only and gnu's header files are generally full of version ifdefs, I would think that there may be a way to use a more recent version of at least the header files in libstdc++. I can't use a different .so for it, though. I'm sure I can kludge this together, but is it possible to do something like this correctly?

    Thanks.

    解决方案

    1. If I compile a shared library with -std=c++0x or -std=g++0x, am I guaranteed that a program that uses my library doesn't need those switches (provided I have no c++0x features in the header files)? It seems to work, but I don't want to be signing up for subtle problems down the road.

    C++11 support was still experimental in GCC 4.x releases (it is no longer experimental from GCC 5 onwards). Although we tried to keep things working, the answer is no, you are not generally guaranteed that will work in all cases. There are a number of ABI changes caused by using -std=c++0x that could cause problems for programs that mix C++03 code and C++11 code, see http://gcc.gnu.org/wiki/Cxx11AbiCompatibility for more details. If your library doesn't export any of the symbols described on that page then you should be fine.

    2. The standard library for C++11 in g++ 4.4 is quite incomplete. Since much of the standard library is header-only and gnu's header files are generally full of version ifdefs, I would think that there may be a way to use a more recent version of at least the header files in libstdc++. I can't use a different .so for it, though. I'm sure I can kludge this together, but is it possible to do something like this correctly?

    No, there is absolutely no chance whatsoever that will work. The headers from later versions use features not supported by 4.4, and even if you could use them you'd need to use the newer libstdc++.so. Just no.

    The headers are not full of version #ifdefs, almost the only ones you'll find are checks for __GXX_EXPERIMENTAL_CXX0X__ which is defined by G++ when you use -std=c++0x but that doesn't mean your 4.4 version supports lambdas, non-static data member initializers, proper rvalue reference semantics, default/deleted functions etc. that later headers make liberal use of. You must use libstdc++ headers with the same version of GCC they came with.

    In short, if you want proper C++11 support you need to use a newer compiler.

    If you can't use a newer compiler you can't get proper C++11 support.

    这篇关于g ++ -std = c ++ 0x和兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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