Home > Backend Development > C++ > How Do I Choose the Right C Standard When Compiling with g ?

How Do I Choose the Right C Standard When Compiling with g ?

Mary-Kate Olsen
Release: 2024-11-12 07:55:02
Original
599 people have browsed it

How Do I Choose the Right C   Standard When Compiling with g  ?

Choosing the C Standard in g Compilations

Compiling C code with g requires consideration of the C standard version to avoid potential errors. By default, g uses a specific standard version, but it can be overridden using command-line arguments.

Default Standard Version

The default standard version used by g can be determined by executing the command:

g++ -dM -E -x c++ /dev/null | grep -F __cplusplus
Copy after login

This command outputs a macro that defines the __cplusplus preprocessor macro, indicating the version of the C standard being used. For example:

#define __cplusplus 199711L  // Represents C++98
Copy after login

Overriding the Default Version

To override the default standard version, use the -std= argument followed by the desired version. For instance, to use C 17, the command would be:

g++ -std=c++17 example.cpp
Copy after login

Recommendation

Using the -std= argument is recommended for several reasons:

  • Ensures compatibility with the intended version of C .
  • Prevents unexpected errors due to using an outdated or unsupported standard.
  • Facilitates collaboration across different systems and toolchains with varying default standards.

References

  • [g Options](https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Options.html)
  • [g Standard Version Query](https://gcc.gnu.org/onlinedocs/gcc/cpp-options.html#cpp-options_002dM)

The above is the detailed content of How Do I Choose the Right C Standard When Compiling with g ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template