简体   繁体   English

在SOLARIS上使用C ++ Pair初始化C ++ std映射时出错

[英]Error while init of C++ std map with C++ Pair ON SOLARIS

The below code runs OK in Linux but gives compiler error in Solaris. 下面的代码在Linux中可以正常运行,但在Solaris中会给出编译器错误。 I am trying to initialize an std pair and then using it to init a C++ Map. 我试图初始化一个std对,然后使用它来初始化C ++ Map。 It runs perfectly in linux , however Solaris has got an issue with this. 它可以在linux上完美运行,但是Solaris遇到了问题。 Any one has idea what can be done to make it OK in all UNIX variations? 任何人都知道如何在所有UNIX变体中都能使其正常运行?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <map>
#define TRUE 1
#define FALSE 0

using namespace std;


std::pair<std::string, std::string> envVarsData[] =
{
        std::make_pair(std::string("HOME"), std::string("home")),
        std::make_pair(std::string("RETURNED"), std::string("Returned"))
};

size_t iSize = sizeof(envVarsData) / sizeof(envVarsData[0]);
std::map<std::string, std::string> envVarsMap(envVarsData, iSize);

int main()
{

 return 0;
}

The error thrown is as below 引发的错误如下

# CC t1.cpp
"t1.cpp", line 21: Error: Could not find a match for std::map<std::string,std::string>::map(std::pair<std::string, std::string>[2], unsigned) needed in<no tag>.
1 Error(s) detected.
#

To use C++11 features with the Solaris Studio C++ compiler, you must use the -std=c++11 option. 要将C ++ 11功能与Solaris Studio C ++编译器一起使用, 必须使用-std=c++11选项。 Per the What's New in Oracle® Solaris Studio 12.4 guide : 根据Oracle®Solaris Studio 12.4的新增功能》指南

Using C++11 Features 使用C ++ 11功能

In Oracle Solaris Studio 12.4, the C++ compiler supports C++11, a new language and ABI (Application Binary Interface). 在Oracle Solaris Studio 12.4中,C ++编译器支持新语言C ++ 11和ABI(应用程序二进制接口)。

In C++ 11 mode, the CC compiler uses the g++ ABI and a version of the g++ runtime library that is supplied with Oracle Solaris Studio. 在C ++ 11模式下,CC编译器使用g ++ ABI和Oracle Solaris Studio随附的g ++运行时库的一个版本。 For this release, version 4.8.2 of the g++ runtime library is used. 对于此发行版,使用g ++运行时库的4.8.2版。

An ABI describes the low-level details in the generated object code. ABI在生成的目标代码中描述了底层细节。 Modules that use different ABIs cannot successfully be linked together into a program. 使用不同ABI的模块无法成功链接到一个程序中。 This means that you must use C++11 mode on all modules in your program, or none of them. 这意味着您必须在程序中的所有模块上使用C ++ 11模式,或者不使用任何模块。

If you use Oracle Solaris Studio 12.4 C++ as an upgrade to Oracle Solaris Studio 12.3 (C++ 5.12), no changes are needed in scripts or makefiles if you are not using C++11 features. 如果使用Oracle Solaris Studio 12.4 C ++升级到Oracle Solaris Studio 12.3(C ++ 5.12),则在不使用C ++ 11功能的情况下,无需在脚本或生成文件中进行任何更改。 An exception is Rogue Wave Tools.h++ is not available. 一个例外是Rogue Wave Tools.h ++不可用。 For more information about features no longer supported, see Features That Have Been Removed in This Release in Oracle Solaris Studio 12.4: Release Notes 有关不再受支持的功能的更多信息,请参见《 Oracle Solaris Studio 12.4:发行说明》中的本发行版中已删除的功能。

To compile in C++11 mode, add the option –std=c++11 to the CC command line. 要在C ++ 11模式下进行编译,请在CC命令行中添加选项–std = c ++ 11。 The location on the command line is not important. 命令行上的位置并不重要。 The option causes the compiler to recognize language features new in C++11, and to use the C++11 version of the standard library (g++ runtime library that is supplied). 该选项使编译器识别C ++ 11中的新增语言功能,并使用标准库(提供的g ++运行时库)的C ++ 11版本。 Except for the options marked as incompatible with –std=c++11, all other command-line options can be used along with C++11, and have their usual effects. 除了标记为与–std = c ++ 11不兼容的选项外,所有其他命令行选项均可与C ++ 11一起使用,并具有其通常的效果。 The –std=c++11 option must be used consistently on every CC command used in building a library or executable program. 必须在用于构建库或可执行程序的每个CC命令上始终使用–std = c ++ 11选项。

Imagine having a huge installed base of C++ code, upgrading to a new compiler, and having the default language option change from, say, C++03 to C++11 and breaking a lot of already-working code . 想象一下,已经有大量的C ++代码安装基础,升级到新的编译器,并且默认语言选项从例如C ++ 03更改为C ++ 11, 并且破坏了许多已经可以运行的代码

It runs perfectly in linux 它在Linux上完美运行

That's likely because the default language option for g++ is GNU-extended, non-standard C++14 : 这可能是因为g ++的默认语言选项是GNU扩展的非标准C ++ 14

The default, if no C++ language dialect options are given, is -std=gnu++14 . 如果未提供C ++语言方言选项,则默认值为-std=gnu++14

If you want portable code, you can't use non-standard extensions on any platform. 如果您需要可移植的代码,则不能在任何平台上使用非标准扩展。

Any one has idea what can be done to make it OK in all UNIX variations? 任何人都知道如何在所有UNIX变体中都能使其正常运行?

Don't use non-standard compiler extensions on any platform. 不要在任何平台上使用非标准的编译器扩展。

Yes, that means you likely have to go back and change the options on Linux, where if you are using the default g++ options you are compiling with non-standard GNU extensions to C++. 是的,这意味着您可能必须返回并更改Linux上的选项,如果您使用的是默认g++选项,则需要使用非标准的GNU C ++扩展进行编译。

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

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