Chapter 1
Installing the Required Bits and Pieces

In this chapter, you’ll learn about C and C++ IDEs and compilers, and I’ll walk you through the process of installing them on your favorite computer. Be warned, however, that the projects in chapters 3 and 4 will be running on a Raspberry Pi Zero, which is a Linux-based system.

If you already have a Pi Zero or another Linux machine, I recommend you set it up now. The basic programs that we’ll go through in chapters 1 and 2 will run on any system, but if you’re already familiar with Linux and the Pi, so much the better.

If you’d like to get started learning with Linux but don’t have a computer on which to install it, I suggest you check out VirtualBox (http://www.virtualbox.org) from Oracle. VirtualBox is free and lets you install almost any client operating system (Windows, Linux, and a few others) onto a virtual computer running inside your host machine (Windows, Mac, or Linux). Since everything is free, you can download VirtualBox for your current computer, download a free Linux version (Ubuntu is probably the most popular, but you have many to choose from), start up your new virtual computer, and follow along with this book.

That being said, however, you can indeed learn to program in C on any machine, be it Mac, Linux, or Windows, and I’ll show you how to get started on any of those.

To program in C on any computer, you’ll need at least two things: an editor, or IDE, and a compiler. Let’s look at the compiler first.

The Compiler

The compiler is the piece of software that converts the code you write to language that the computer can understand. This end result is a collection of 1s and 0s called assembly language, machine code, or object code. There are different compilers for different systems and different programming languages, but when it comes to C, the most common compiler by far is gcc (Gnu Compiler Collection). It comes preinstalled on most Linux distributions, and versions exist for both Windows and Mac.

Linux

To make sure you have gcc installed, type gcc --version in your terminal. You should be greeted with a message similar to this:

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609

(This may vary based on your distribution, of course.) If by chance gcc is not installed, you should be able to install it from the standard Linux package repositories; try sudo apt-get install gcc or sudo yum install gcc.

Mac

To use gcc on the Mac, you’ll need to install it, since it’s not included by default. This means you’ll have to download and install the developer’s command-line tools. Luckily, on newer versions of the OS—El Capitan, Yosemite, and newer—you no longer need to download and install Xcode (Apple’s bulky software developer package) first. Instead, you can download the command-line tools separately.

To do this, open a terminal and enter

xcode-select --install

You’ll get a software update pop-up window that asks you to confirm that you wish to install the tools. Click Install and agree to the terms of service; then wait for the 130 MB download to finish. The installer should finish on its own; when it does, type

gcc --version 

into your terminal window and you should see something like this:

gcc (GCC) 4.8.0 20130113 (experimental)

You now have gcc installed on your Mac!

Windows

gcc does work on Windows machines, though you’ll need to install it and a Linux-like environment for it. Alternatively, you can download the Visual Studio Community package (see the next section) and you’ll have access to gcc as part of its available add-ons. This is my recommended course of action, since installing and using the GNU-like environments for Windows, Cygwin, and MinGW is not a trivial process.

The IDE

When it comes to writing your code, the IDE (integrated development environment) is where you’re going to have the most choices available to you. As a matter of fact, it’s kind of a dirty little secret among programmers that you don’t need to use a fancy development environment like Microsoft’s Visual Studio or Eclipse at all. You can write perfectly good, functional C code with any standard text editor, like emacs, vim, Sublime Text, or even Windows’ Notepad or the Mac’s TextEdit.

That being said, however, it’s definitely to your advantage to use a development environment of some sort, especially when you’re learning. The basic functions that come with an IDE, such as automatically adding closing parentheses and brackets ({ }), can be immeasurably helpful. It’s a feature that you’ll get used to pretty quickly.

If you’d prefer to use a full-featured IDE, which is probably a good idea for beginners, many options—both free and otherwise—are available. Many people like NetBeans (http://netbeans.org), and Eclipse’s C/C++ extension (http://eclipse.org/cdt) is also popular. If you’re on a Mac or Windows, you can get the community edition of Microsoft’s Visual Studio IDE for free (http://visualstudio.com/downloads).

NetBeans is a free IDE that has extensions available for many different languages, including Java, HTML, PHP, and C/C++. It’s supported on all three major platforms. When you visit the download page, choose your OS first at the top left of the page, and then choose the C/C++ bundle appropriate to your platform (32- or 64-bit).

Eclipse is also popular, though many (myself included) find its C/C++ interface sort of clunky. Like NetBeans, it’s available for all three major operating systems, and it’s also free. Visit their download page; download the .zip, .dmg, or .tar file appropriate for your platform; and follow the installation instructions.

If you’re using a Mac or a Windows box, I recommend getting the free Visual Studio Community edition from Microsoft. Be warned: If you install all the available packages (such as Android and iOS development on the Mac), it’s a hefty download. However, once it’s installed, it does give you an all-in-one application for all your coding projects.

If you’re confident in your coding skills, you can use any text editor, and as I mentioned, some have add-ons and extensions that make programming more accessible. Sublime Text (www.sublimetext.com/3) is my usual choice; it has automatic keyword coloring and parentheses- and brace-closing built in. I’m also an emacs user (and I know that by saying that I’m inviting hate mail from vi users!), and I use it when I do programming remotely, such as during an SSH session on a remote device. Notepad++ (http://notepad-plus-plus.org) is also a popular choice for a text editor development environment.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.94.190