2

I found that in one of our projects we use compile command like the following:

g++ -ansi -std=c++0x ...

Is it right way to use simultaneously both flags -ansi and -std=c++0x?

I read man for gcc and it looks like we have to choose only one flag. What do you think?

zubactik
  • 1,297
  • 3
  • 19
  • 34
  • Well, what happens if you do that? – filmor Oct 15 '14 at 07:23
  • @filmor , Nothing) I mean that we get compilation log without any issues and also binary file works quite fine. But only fact that we use both flags looks strange for me. – zubactik Oct 15 '14 at 07:27

4 Answers4

1

GCC documentation says

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

So, this is

g++ -ansi -std=c++0x ...

equivalent to

g++ -std=c++98 -std=c++0x ...

When you compile it with

g++ -ansi -std=c++0x ...

I believe, it's equivalent to not specifying -ansi at all, due to the command line parsing which usually takes the last argument for a given parameter. This is speculation though. But I tested with a simple program:

#include<iostream>

constexpr int value() {return 42;}

int main(void)
{
std::cout<<value()<<std::endl;
}

This program can't compile in C++98 as constexpr was introduced in C++11. But it compiles fine with both:

g++ -ansi -std=c++11 test.cpp

and

g++ -ansi -std=c++98 -std=c++11 t.cpp

But fails with both:

g++ -ansi -std=c++11 -ansi test.cpp

and

g++ -ansi -std=c++11 -std=c++98 t.cpp

Giving the errors:

 error: ‘constexpr’ does not name a type
 note: C++11 ‘constexpr’ only available with -std=c++11 or -std=gnu++11

This suggests whatever the specified as the last -std is only one effective and you might as well remove the -ansi.

P.P
  • 117,907
  • 20
  • 175
  • 238
0

If you use -std=c++0x (which became -std=c++11 since gcc 4.7), using -ansi is superfluous. See also here

Community
  • 1
  • 1
Claudio
  • 10,614
  • 4
  • 31
  • 71
0

The options are mutually exclusive, since they both set the standard (or dialect) of the language which the compiler should use for the compilation. Quoting GCC docs:

-ansi

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

So you're basically doing this:

gcc -std=c++98 -std=c++0x ...

I believe later options trump earlier options on the GCC command line, so the net effect is compiling in C++0x mode.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
0

From GCC: standards/ C++ language.

GCC implements the majority of C++98 (export is a notable exception) and most of the changes in C++03. To select this standard in GCC, use one of the options -ansi, -std=c++98, or -std=c++03;

If I understand it correctly, -ansi, -std=c++98, and -std=c++03 can be used interchangeably. Specifically, it doesn't make sense to use -ansi and -std=c++11 simultaneously.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294