简体   繁体   English

g ++中的C ++ 11语言支持,没有`-std = c ++ 11`的库破坏功能

[英]C++11 language support in g++ without the library-breaking features of `-std=c++11`

Is there a way to tell g++ to enable the new language features of C++11 without any breaking changes to the standard C++ library due to ABI modifications? 有没有一种方法可以告诉g++启用C ++ 11的新语言功能,而又不会由于ABI修改而对标准C ++库造成任何重大改变?

Adding the -std=c++11 compilation flag tells g++ to enable both the language and the library features, but object files created this way cannot be safely linked with those that used a different -std= setting. 添加-std=c++11编译标志会告诉g++启用语言和库功能,但是以此方式创建的目标文件不能与使用不同-std=设置的文件安全地链接。 I'd like to be able to use language enhancements like rvalue references, move constructors (for my own classes), and the auto keyword in code that's linked against C++03 libraries. 我希望能够使用语言增强功能,例如右值引用,移动构造函数(针对我自己的类)以及与C ++ 03库链接的代码中的auto关键字。

EDIT: 编辑:

I'm interested in having g++ enable its C++11 language features, but I want it to parse, compile, and link against the old C++03 libraries. 我对让g++启用其C ++ 11 语言功能感兴趣,但我希望它能够对旧的C ++ 03库进行解析,编译和链接。 I don't want it to use the C++11 version of the standard library. 我不希望它使用标准库的C ++ 11版本。 This means that in my own code, I'll be able to use auto , range foreach constructs, rvalue references, etc., but I won't be able to use the new C++11 features in the standard C++ library like std::move or rvalue-ref enhancements to the STL containers. 这意味着在我自己的代码中,我将能够使用auto ,range foreach构造,rvalue引用等,但是我将无法使用标准C ++库(如std::move中的C ++ 11新功能。对STL容器进行::: std::move或rvalue-ref增强。 The reason for not wanting the C++11 version of the standard library is that the layout of various objects has changed, so it's invalid to link two object files that expect different versions of the standard library into the same binary. 不需要标准库的C ++ 11版本的原因是各种对象的布局已更改,因此将期望标准库版本不同的两个对象文件链接到同一二进制文件是无效的。

No, you can't enable C++11 language features without C++11 library features (not without editing the libstdc++ headers to remove all the C++11 parts.) 不,没有C ++ 11库功能,就不能启用C ++ 11语言功能(如果没有编辑libstdc ++标头以删除所有C ++ 11部分,则不能启用)。

But there aren't many incompatible symbols (as long as you don't use 4.7.0 or 4.7.1 which had an incompatible std::list , reverted for 4.7.2) so you probably only need to worry about the erase() members of the RB-tree containers. 但是不存在不兼容的符号(只要您不使用4.7.0或4.7.1带有不兼容的std::list ,将其还原为4.7.2),因此您可能只需要担心erase() RB树容器的成员。 You could ensure the C++11 version of the symbol is defined in your main executable, so that version of the symbol will be used by all code that needs it. 您可以确保在主要可执行文件中定义了该符号的C ++ 11版本,以便该符号版本将被所有需要该符号的代码使用。 Code in other libraries expecting the C++03 versions will ignore the return value, code expecting the C++11 versions will be able to use the return value. 期望C ++ 03版本的其他库中的代码将忽略返回值,而期望C ++ 11版本的代码将能够使用该返回值。

As far as I know GCC 4.7 and up has ABI modifications. 据我所知,GCC 4.7及更高版本具有ABI修改。 You can try versions below that. 您可以尝试以下版本。

It may also help to use -std=gnu++11 instead of -std=c++11 . 使用-std=gnu++11代替-std=c++11可能也有帮助。 Why that exactly is I don't know but it worked for me. 我到底不知道为什么会这样,但是对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM