Beginning Programming All-in-One For Dummies. Wallace WangЧитать онлайн книгу.
rel="nofollow" href="#ulink_69df831b-0692-5dc3-9b37-81a93e0be57e">FIGURE 4-1: Compiler optimization settings let you make your program as small and as fast as possible.
One major feature of a compiler’s code generation capabilities involves speed, which can measure two different features:
How quickly the compiler works in translating your source code to machine code: In the old days, compilers could take hours or days to compile a simple program. Nowadays, compilers often work in minutes or even seconds. Shove in a program that consists of 800,000 lines of code, and in less than a minute, the compiler can create an executable file for you. The faster the compiler works, the less time you waste waiting to run and test your program.
The performance of the machine language code that the compiler creates: Given the same program, one compiler may create a program that runs quickly, whereas a second compiler may create that same program that runs much slower.
Ideally, you want a compiler that both compiles fast and creates programs that run fast.
Target platforms
Most compilers can compile programs only for a specific operating system, such as Linux or Windows. However, what happens if you need to write a program that runs on two or more operating systems?
You could write the program twice with two different compilers — one for each operating system. So, if you wanted to write a C++ program that runs under macOS and Windows, you could compile that program by using Microsoft Visual Studio (for Windows) and then write a similar program to compile by using Xcode (for macOS).
Of course, writing the same program two times for two different compilers on separate operating systems is a tremendous waste of time. As an alternative, some compilers are known as cross-compilers — they can create programs that work on multiple operating systems, such as Linux, macOS, and Windows. Figure 4-2 shows the Xojo cross-compiler, which lets you choose whether to compile a program for Linux, macOS, or Windows.
FIGURE 4-2: A cross-compiler lets you write a program and compile it for multiple operating systems at the click of a mouse.
With a cross-compiler, you can write a program once and compile it to run on multiple operating systems, effectively doubling or tripling your potential market. Without a cross-compiler, you may need to write a program for each compiler, under a different operating system, essentially doubling or tripling your work.
Although the idea of writing a program once and having it run on multiple operating systems may seem appealing, cross-compilers aren’t perfect. Chances are, you’ll have to tweak your program to run under each operating system, but those minor tweaks will be much easier than rewriting huge chunks of your program if you had to use two separate compilers.
Cost
Paying for a compiler doesn’t necessarily mean you’re getting a better compiler. The GCC compiler is free and one of the best compilers available.
Generally, you should only pay for a compiler if it offers a unique feature or programming language that you need or want to use. For learning purposes, you can find plenty of free compilers so you can study different programming languages without spending any money at all.
Windows users will most likely want to consider Microsoft Visual Studio (https://visualstudio.microsoft.com
), which comes in both free and commercial versions. Microsoft also offers a version of Visual Studio for the Mac to create macOS and iOS apps.
To create Mac, iPhone, iPad, Apple Watch, or Apple TV apps, use Apple’s free Xcode compiler (https://developer.apple.com/xcode
), which only runs on a Mac. Another free option from Apple is Swift Playgrounds (www.apple.com/swift/playgrounds
), which runs on a Mac or iPad and lets you write Swift code.
Because Linux is free, most Linux compilers are free, too, including the popular GCC compiler (https://gcc.gnu.org
), although you can buy commercial compilers if necessary.
Finding an Interpreter
Interpreters are commonly used for scripting languages, such as Perl or Python, but they’re rarely used for system programming languages, such as C++. That’s because if you write a program and use an interpreter, you must distribute a copy of your source code with the interpreter. Giving away your source code essentially gives away your program, so most commercial programs use a compiler instead.
However, interpreters can be useful for learning a programming language. By using an interpreter, you can focus solely on learning a programming language without the distraction of creating a complete program.
One popular type of interpreter is the online interpreter, which lets you use any browser to practice typing specific language commands. JDoodle (www.jdoodle.com
) lets you select from dozens of popular and obscure programming languages such as C++ and Swift, along with less common languages like Forth and Smalltalk.
If you’re interested in Flutter, Google’s cross-platform tool for creating Windows, Android, macOS, iOS, and Fuchsia apps, you can practice writing Dart code on the Flutter website (https://flutter.dev
), as shown in Figure 4-3.
FIGURE 4-3: The Flutter website lets you type and run code using any browser.
Nearly every web browser comes with a JavaScript interpreter. Web designers use JavaScript for creating interactive web pages, verifying information typed on a web page (such as a username and password), or opening pop-up windows that display advertisements.
Although JavaScript interpreters can be found in any web browser, you may have to download and install interpreters for other programming languages. Some popular programming languages for running programs on web servers (those computers responsible for displaying and retrieving information from web pages, such as shopping data) include
Perl (www.perl.com
)
PHP (www.php.net
)
Python (www.python.org
)
Ruby (www.ruby-lang.org
)
Not only do the preceding four languages have free interpreters that you can copy, but their interpreters also run on different operating systems. That means a Perl or Ruby program written on a Windows computer should run identically if it’s copied and run on a Linux or Mac computer.