Beginning Programming All-in-One For Dummies. Wallace WangЧитать онлайн книгу.
which is a C++ compiler. Unfortunately, the CodeWarrior compiler ran only on the PowerPC processors, which were used in older Mac computers. When Apple switched to Intel processors, Microsoft had to dump the CodeWarrior compiler and use a different compiler called Xcode.
Because CodeWarrior and Xcode are both C++ compilers, Microsoft could theoretically compile the same C++ program under both CodeWarrior and Xcode with no problems. Realistically, Microsoft had to rewrite major portions of their C++ programs just to get them to run under the Xcode compiler. The moral of the story is that switching compilers is rarely an easy decision, so it’s important to choose the “right” compiler from the start.
At one time, the CodeWarrior compiler was considered the “right” compiler to use for creating Mac programs. What made CodeWarrior suddenly turn into the “wrong” compiler was when Apple switched from PowerPC processors to Intel processors. Everyone who had used the CodeWarrior compiler had to switch to the Xcode compiler. Bottom line: What may seem like the “right” compiler today could later turn out to be the “wrong” compiler through no fault of your own or the compiler company.When choosing a compiler, you have to consider your needs, the compiler company’s reputation, and the compiler’s technical features.
Defining your needs for a compiler
The most important choice for a compiler centers solely on what you need. Follow these steps:
1 Decide which programming language you want to use.If you want to write C++ programs, you need a C++ compiler. If you want to write C# programs, you need a C# compiler. Many compilers can work with multiple languages, such as C and C++.
2 Decide which operating system you want to use.If you want to write C++ programs for macOS, your choices immediately narrow to the small list of C++ compilers that run under macOS.
3 Choose a compiler that has the best chance of being around years from now.Most companies prefer using compilers from brand-name companies, like Apple or Microsoft. Even compilers from big-name companies are no guarantee against obsolescence. Microsoft has stopped supporting its compilers over the years, such as Microsoft Pascal and Visual Basic 6. If you used either of these compilers to write a program, you had to change compilers when Microsoft stopped developing them.Many people are choosing open-source compilers. Open source simply means that the source code to the compiler is available freely to anyone. Not only does this mean that open-source compilers are free (compared to the hundreds of dollars you can pay for a brand-name compiler), but it also guarantees that the compiler can’t become obsolete due to lack of support. If you use a compiler from a company that goes out of business, you’re forced to port (transfer) your program to another compiler, which means having to rewrite the program to run under a different compiler.
Because anyone can examine and modify the source code to an open-source compiler, anyone can make changes to the compiler to improve it. One of the most popular open-source compilers is GCC (https://gcc.gnu.org
), which stands for GNU Compiler Collection.
Originally, GCC only compiled C source code, but later versions of GCC compile several different languages, including Ada, C, C++, Java, and Objective-C, with more programming languages being supported every year. Even better, the GCC compiler also runs on a variety of operating systems, such as Linux and Windows, so if you write a program using the GCC compiler, you can recompile your program to run under another operating system with minimal changes (ideally).
The GCC compiler actually consists of two parts:
The front end of the compiler translates source code into an intermediate format:To write C++ programs, you must use the C++ front end of the GCC compiler.To write Ada programs, use the Ada front end of the GCC compiler.By creating front ends for different languages, programmers can make the GCC compiler compile more programming languages.
The back end of the compiler finishes translating the intermediate code into actual machine code.
Evaluating the technical features of a compiler
After you choose a particular programming language and pick which operating systems you want your programs to run on, your list of compiler choices is likely narrowed to one or two choices. Given two compilers that both meet your needs, you can pick the “best” compiler by examining their technical features.
The technical features of a compiler are meaningless if
The compiler stops being developed and supported.
The compiler can’t run under the operating system or processor you need in the future.
A particular technical feature is something you don’t need or care about.
Supported language standards
No two compilers are alike, even those that compile the same programming language, such as C++. The problem is that every programming language has an official “standard,” but the standard for most programming languages is usually far behind what people in the real world are actually using. (By the time an official standards committee agrees on the features of a given programming language, programmers have already created new features that eventually become standards in future versions of that language.)
As a result, most compilers support a given language standard plus additional features that programmers have developed. Therefore, every compiler actually works with a different dialect of a programming language. So, C++ programs that run under the Microsoft Visual Studio compiler may or may not run the same when compiled under the GCC compiler, even though both compilers claim to support the “standard” C++ programming language.
Language standards are nice but generally useless when comparing compilers. What’s more important is whether a particular compiler offers the specific features you need or want, regardless of whatever particular standard it may follow.
Code generation and optimization
Every compiler converts source code into machine language, but some compilers can translate source code into more efficient machine language commands than other compilers. As a result, it’s possible to compile the same C++ program under two different C++ compilers and create identically working programs that consist of different machine language instructions.
The goal of every compiler is to create a program that takes up as little memory and disk space as possible while running as fast as possible. Usually, compilers make a trade-off. To make a program run faster, the executable file may take up a large amount of disk space or require a lot of memory. If the compiler can reduce the size of your program and the amount of memory it needs to run, it may create a slow program.
To help you tweak your program for the best balance of speed, size, and memory requirements, many compilers offer optimization settings. By fiddling with these optimization settings, you can tell the compiler how to speed up or shrink your program, as shown in Figure 4-1.