在GCC 7.3中禁用C ++ 11


11

似乎在Ubuntu 18.04中,默认附带的默认出厂g ++(版本7.3.0)在C ++ 11兼容模式下运行。我的旧代码中出现了一些与C ++ 11不兼容的错误。我安装了g ++-6(版本6.4.0),并且程序可以正常编译。是否可以在g ++-7中禁用C ++ 11模式?


2
实际上,GCC 6的默认模式已经是C ++ 14。GCC 7增加了对C ++ 17的支持;仅禁用此C ++ 17内容可能就足够了。无需回到C ++ 11。回到C ++ 98确实是很大的过度杀伤力(杀伤力强吗?)
MSalters

5
您在不兼容的旧程序中做了什么?您可能已经能够执行由于错误而无法执行的操作。在这种情况下,使用c ++ 98不会带来任何好处。
WHN

9
@snb:或者他们使用std::auto_ptr,或者auto以其原始伪装使用了,或者他们现在进行了非法变窄的转换,或者他们使用了export,或或或
Lightness Races in Orbit


2
@LightnessRacesinOrbit在C ++ 11之前,大多数情况都是不好的做法。
WHN

Answers:


20

尝试添加-std=gnu++98,这可能是gcc 6.4.0的默认设置。

例:

g++ -std=gnu++98 hello.cpp -o hello

5
如果您需要与旧GCC的编译代码的兼容性ABI,你会想-D_GLIBCXX_USE_CXX11_ABI=0,以及
Tavian巴恩斯

2
或者-std=gnu++03是C ++ 11之前的最后一个标准。还值得一提的是,-std=c++03它将禁用某些与严格的ISO C ++不兼容的GNU扩展。
彼得·科德斯

1
在Godbolt编译器浏览器上,C ++ 14是g ++ 6.3 godbolt.org/g/x2xPCS的默认设置。C ++ 98是g ++ 5.5及更早版本的默认设置。我检查了值__cplusplus如何确定编译器使用的C ++标准版本?
彼得·科德斯

1

man g++可以在不同方言之间进行选择:

Options Controlling C Dialect
   The following options control the dialect of C (or languages derived
   from C, such as C++, Objective-C and Objective-C++) that the compiler
   accepts:

  -ansi
       In C mode, this is equivalent to -std=c90. In C++ mode, it is
       equivalent to -std=c++98.

       This turns off certain features of GCC that are incompatible with
       ISO C90 (when compiling C code), or of standard C++ (when compiling
       C++ code), such as the "asm" and "typeof" keywords, and predefined
       macros such as "unix" and "vax" that identify the type of system
       you are using.  It also enables the undesirable and rarely used ISO
       trigraph feature.  For the C compiler, it disables recognition of
       C++ style // comments as well as the "inline" keyword.

  -std=
       Determine the language standard.   This option is currently only
       supported when compiling C or C++.

       The compiler can accept several base standards, such as c90 or
       c++98, and GNU dialects of those standards, such as gnu90 or
       gnu++98.  When a base standard is specified, the compiler accepts
       all programs following that standard plus those using GNU
       extensions that do not contradict it.  For example, -std=c90 turns
       off certain features of GCC that are incompatible with ISO C90,
       such as the "asm" and "typeof" keywords, but not other GNU
       extensions that do not have a meaning in ISO C90, such as omitting
       the middle term of a "?:" expression. On the other hand, when a GNU
       dialect of a standard is specified, all features supported by the
       compiler are enabled, even when those features change the meaning
       of the base standard.  As a result, some strict-conforming programs
       may be rejected.  The particular standard is used by -Wpedantic to
       identify which features are GNU extensions given that version of
       the standard. For example -std=gnu90 -Wpedantic warns about C++
       style // comments, while -std=gnu99 -Wpedantic does not.

       A value for this option must be provided; possible values are

       c90
       c89
       iso9899:1990
           Support all ISO C90 programs (certain GNU extensions that
           conflict with ISO C90 are disabled). Same as -ansi for C code.

       iso9899:199409
           ISO C90 as modified in amendment 1.

       c99
       c9x
       iso9899:1999
       iso9899:199x
           ISO C99.  This standard is substantially completely supported,
           modulo bugs and floating-point issues (mainly but not entirely
           relating to optional C99 features from Annexes F and G).  See
           <http://gcc.gnu.org/c99status.html> for more information.  The
           names c9x and iso9899:199x are deprecated.

       c11
       c1x
       iso9899:2011
           ISO C11, the 2011 revision of the ISO C standard.  This
           standard is substantially completely supported, modulo bugs,
           floating-point issues (mainly but not entirely relating to
           optional C11 features from Annexes F and G) and the optional
           Annexes K (Bounds-checking interfaces) and L (Analyzability).
           The name c1x is deprecated.

       gnu90
       gnu89
           GNU dialect of ISO C90 (including some C99 features).

       gnu99
       gnu9x
           GNU dialect of ISO C99.  The name gnu9x is deprecated.

       gnu11
       gnu1x
           GNU dialect of ISO C11.  This is the default for C code.  The
           name gnu1x is deprecated.

       c++98
       c++03
           The 1998 ISO C++ standard plus the 2003 technical corrigendum
           and some additional defect reports. Same as -ansi for C++ code.
       gnu++98
       gnu++03
           GNU dialect of -std=c++98.

       c++11
       c++0x
           The 2011 ISO C++ standard plus amendments.  The name c++0x is
           deprecated.

       gnu++11
       gnu++0x
           GNU dialect of -std=c++11.  The name gnu++0x is deprecated.

       c++14
       c++1y
           The 2014 ISO C++ standard plus amendments.  The name c++1y is
           deprecated.

       gnu++14
       gnu++1y
           GNU dialect of -std=c++14.  This is the default for C++ code.
           The name gnu++1y is deprecated.

       c++1z
           The next revision of the ISO C++ standard, tentatively planned
           for 2017.  Support is highly experimental, and will almost
           certainly change in incompatible ways in future releases.

       gnu++1z
           GNU dialect of -std=c++1z.  Support is highly experimental, and
           will almost certainly change in incompatible ways in future
           releases.
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.