Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Few questions about C++ compilers : GCC, MSVC, Clang, Comeau etc

I've few questions about C++ compilers

  • Are C++ compilers required to be one-pass compiler? Does the Standard talk about it anywhere?

  • In particular, is GCC one-pass compiler? If it is, then why does it generate the following error twice in this example (though the template argument is different in each error message)?

    error: declaration of ‘adder<T> item’ shadows a parameter
    error: declaration of ‘adder<char [21]> item’ shadows a parameter

A more general question

  • What are the advantages and disadvantages of one-pass compiler and multi-pass compiler?

Useful links:

  • A List of C/C++ compilers (wikipedia)
  • An incomplete list of C++ compilers (Bjarne Stroustrup's site)
like image 596
Nawaz Avatar asked Mar 16 '11 09:03

Nawaz


People also ask

How many compilers are there in C?

There are over 50 compilers for C like ICC by Intel to GNU GCC by GNU Project. The focus of having multiple compilers is to optimize the compiled C code for specific hardware and software environments.

Which compiler is used for C programming?

G++ is the name of the compiler. (Note: G++ also compiles C++ code, but since C is directly compatible with C++, so we can use it.).

Is GCC faster than Clang?

Clang is much faster and uses far less memory than GCC. Clang aims to provide extremely clear and concise diagnostics (error and warning messages), and includes support for expressive diagnostics. GCC's warnings are sometimes acceptable, but are often confusing and it does not support expressive diagnostics.


1 Answers

The standard sets no requirements what so ever with regards to how a compiler is implemented. But what do you mean by "one-pass"? Most compilers today do only read the input file once. They create an in memory representation (often in the form of some sort of parse tree), and may make multiple passes over that. And almost certainly make multiple passes over parts of it. The compiler must make a "pass" over the internal representation of a template each time it is instantiated, for example; there's no way of avoiding that. G++ also makes a "pass" over the template when it is defined, before any instantiation, and reports some errors then. (The standard committee expressedly designed templates to allow a maximum of error detection at the point of definition. This is the motivation behind the requirement for typename in certain places, for example.) Even without templates, a compiler will generally have to make two passes over a class definition if there are functions defined in it.

With regards to the more general question, again, I think you'd have to define exactly what you mean by "one-pass". I don't know of any compiler today which reads the source file several times, but almost all will visit some or all of the nodes in the parse tree more than once. Is this one-pass or multi-pass? The distinction was more significant in the past, when memory wasn't sufficient to maintain much of the source code in an internal representation. Languages like Pascal and, to a lesser degree C, were sometimes designed to be easy to implement with a single pass compiler, since a single pass compiler would be significantly faster. Today, this issue is largely irrelevant, and modern languages, including C++, tend to ignore it; where C++ seems to conform to the needs of a one-pass compiler, it's largely for reasons of C compatibility, and where C compatibility is not an issue (e.g. in a class definition), it often makes order of declaration irrelevant.

like image 155
James Kanze Avatar answered Sep 29 '22 23:09

James Kanze