Article | Talk | Edit | History  

CE - Intro to programming/Intro and Setting up the tools

From the World Wide Wiki

We are what we pretend to be, so we must be careful what we pretend to be.

Jump to: navigation, search

Contents

Introduction to Computer Programming

So before we learn about programming, we need to learn what programming is. Computer's can do some pretty amazing things, and they can do it really fast. At present, however, they cannot think independently, and by this I mean they cannot come up with things to do on their own. Everything a computer does it does because somebody at some point, directly or indirectly, told it to. And that's basically what programming is, it's a way of creating a set of instructions for the computer to follow.

Languages

Of course, computer's don't speak the same language as us, so in order to tell it what to do, we need to learn how to present the instructions in a way it can understand. So learning to program is largely about learning to speak the computer's language. But what exactly is a language? A language is just a set of rules that define allowed grammar and syntax for communication, as well as set of allowed words (a vocabulary), or rules for creating words. Human languages are generally pretty slack with the rules, and have tons of allowed words. That's because humans are actually sort of smart, and we're generally pretty good at understanding messages based on context and experience. This allows us to convey the same information in a multitude of different ways without causing much confusion.

Computer's aren't really so smart. They're methodical, and fast, and don't make too many mistakes, but they're really just not as smart as people are (not yet, anyway). Therefore, computer languages are much more strict than human languages. There are relatively few rules for syntax and grammar, but they are strictly enforced. Likewise, there are relatively few recognized words in computer languages (on the other hand, the rules for creating new words are pretty simple, and we do this pretty frequently).

Translators

The great thing about computers is that, even though they may not be real smart themselves, they do things that are pretty smart. Like if you had a really obedient 6 year old (a 6 year old is still smarter than a computer) and gave him a list of very precise very explicit steps to follow, he could solve, say, a differential equation. As long as you told him explicitly how to do each step. It's the same with computer, they are very obedient (though it may not always seem like it when you start programming and your code keeps crashing). One thing this allows us to do (besides solving differential equations), is to create programs that are a little better at understanding languages than the computer itself. These programs can act as translators between us and our computers. Now, most of these programs still aren't any where's near as "smart" as us, so the language is still much stricter and simpler than we're used to, but they are definitely much more flexible and "programmer friendly" than a computer's native language.

Programming languages

Eventually, any good programmer should learn at least the basics of how to speak directly to the computer, without a translator. And later in the curriculum, we will, in quite a bit of depth, actually. But for now, we will instead learn to speak to a translator. The particular translator we'll be talking to speaks a language known as C++, and so our task will be to learn this language. In later courses, we'll also be learning a language called Java. These are called programming languages, because they're the languages you use to program. Your first programming language is generally the hardest one to learn (which is actually true with foreign languages, too), because a big part of the work is learning the general programming concepts which apply to practically all languages. Once you've got those down, learning a new language is just a matter of learning the handful of different rules and vocab words.

The Build Process

So this is how programming works. We use a plain old text editor—like MS Notepad on windows, or vim or emacs on Linux—to write our code according to the rules of the programming language we're using. We then save the code to one or more files, called source files (because they contain source code). With our source files saved, we need to tell the translator to translate them. There are many different kinds of translators out there, and often times we will need to use a handful of translators, one after the other, to get something the computer can understand. But however it happens, the translator(s) will create another file, which contains the fully translated version of our code[1]. This is called an executable file. When we want to run the program, we direct the computer to the executable, and the computer reads it and does what it says to do (executes it)[2]. The whole process of going from source files to an executable file is called the build process. The exact steps involved, and tools (programs) required depend on the programming language and also on how you want the program to be executed. There are also tools that do the same thing as other tools. These are just alternatives you can choose from, each having it's own particular strengths and weaknesses compared to others.

Our build process

As I said, we'll be learning C++ in this chapter. Further, we're going to compile our code to an executable that's only intended to run our the specific computer we're each using (we'll learn more about portability later). To this end, there's a particular build process we're going to use, and particular tools I've chosen for use to start with. Remember, there are alternative options to these tools which I encourage you to play with once you're comfortable with programming. I've chosen the one's I have because I'm familiar with them myself, and they're free to obtain and use.

  1. The first step, as usual, is to write our code.
  2. Next, we need to compile it. For this, we use a compiler which is our first "translator".
  3. The compiler creates object files for each source file. This is a preliminary translation of our code.
  4. Next we have to link our code. This is done by the linker. Essentially, this will combine all of the object files into a single executable file. The reason for the linker step is that code in one source file may need to reference code in another source file. When the compiler compiles each source file, it's going to recognize that this referenced code doesn't actually exist in the current file, and it's going to make a note of it. The linker's job is to make sure all these dependencies are satisfied, and arrange the code together so all the references are in the right place.
  5. Lastly, we execute the program

Our tools

I'm going to attempt to keep this open to both Windows and Linux users. Mac users, I think you've got a Linux kernel buried inside the heart of your OS, so I think if you pull up a command window, you can follow along with the Linux users. Linux is really the preferred environment for programming in C++ (despite what Microsoft may say), and there are more tools (and more free tools) available for it than for Windows, so if things get rough, Windows users may have to fight their way through. There's lots of resources online for this, and if you have any questions, as always, contact me. Though it might be a better idea, if you're serious about becoming a hardcore programmer, to just get yourself running on Linux. The top resource for this is just http://www.linux.org/, but you can try my article: "Migrate to linux".

Another option for windows users is to use Cygwin, which is a free[3] Linux emulator for windows. The program basically allows you to open a Linux command window under windows. It should come with all the tools we need here, but I'm guessing the programs you build in cygwin can only be run in cygwin. That's fine for now though. You can get it from cygwin.com.

Source editor

You can take your pick of plain text editors for this. By plain text I mean there is no built in formatting, each character is just a single ASCII byte. On Microsfot Windows, you can just use the built in program called "notepad" (instructions for opening Notepad:[4]). If you have another preferred editor, feel free, as long as it's plain text. If you don't have a preference, I recommend Crimson Editor, which is freely (as in beer) available here. It does syntax highlighting and is quite a bit less stupid than Notepad.

For Linux popular plain text editors are vim (or just vi), emacs, joe, and pico. I personally prefer vim, but any should do fine. The commands for each program should just be the name of the program. If you've never used one of these before, it'll probably take a little while to get used to. If you're using a desktop environment like KDE and Gnome, there are desktop plain text editors available, too (like KWrite on KDE) but I would recommend sticking it out in the command window with one of the above mentioned programs. Once you get over the initial shock of working in a curses[5] environment, and overcome the slight learning curve for these programs, I think you'll find they're much more powerful than any desktop tool. As you get more into programming, you'll also find that being write there on the command line is a big bonus. Tutorials abound online for all of these programs. Sorry Windows users, I don't know of any similar command line editors for your OS. (Though if you google it, you might have some luck. I know there's a windows "version" of VIM available, but it's still not on the command line, and I'm not sure how fully featured it is).

For both operating systems, there are probably some editors designed specifically for editing C++, that you might want to search for. Just be aware, what we don't want to use right now is an Integrated Development Environment, or IDE. These are programs that have all the tools needed for the build process built in, along with an editor. These can be really useful, but I don't think it's terribly appropriate for first learning to program. For one thing, it obscures a lot of the build process, which is one of the key reasons we're using C++ in the first place. It will also just further complicate the experience because it will add one more thing you need to learn, and IDEs are notoriously complex. I'd rather focus on learning the language and the basics of the build process for now. Last note for Linux users: a lot of the "plain" text editors, notably vim and emacs, are so incredibly powerful that once you learn to use them properly, they can actually do a lot of the same stuff as one of those fancy IDEs, right from the editor. But that's for later.

Compiler

For windows, we'll be using the free 32-bit C++ compiler from Borland, bcc32. I'm using the bcc55 package, which is version 5.5 of this compiler. There might be a newer version when you get it. You can download from "codegear" here. You'll have to register, but it's free. Just click on the "Compiler" link a little ways down from the top. This will take you to a login form. Assuming you don't already have an account, click the link at the bottom of the box "Create a new account" Note that the Borland's forms tend to take a really long time, so be patient, and don't hit any buttons twice. Once you've got your account created, and you're logged in after following the "Compiler" link on codegear, It'll probably present you with a short "C++ compiler survery". You have to fill it out to register, so just do it. Or switch to Linux. Hit submit and wait for it to process. When it does, you should be brought to a page where you can actually download it. Don't worry that it says "Trial" it's full featured and there's no restrictions on it. You can download from HTTP or FTP. Note that the HTTP option uses a Java applet to do the download. If you're into this, go ahead, but the FTP option is just a plain old FTP download.

Download and run the program, and hopefully it'll take care of everything. I can't say for sure, because I'm having trouble running the install program now, but hopefully it will work for you (don't worry, I've already got the compiler installed from before). If all goes well with the install, you just need to make sure the path to the "bin" folder is on your path. To do this, open the control panel, then "system" from the CP. Go to the advanced tab, and click the button "Environment Variables". In the bottom box, look for a variable (left hand column) called "Path". Double click the row to edit it. A new window should appear. In the bottom "Variable value" text field, go to the very end of the text in the field, and type a semi colon (this is used to separate different paths in the list. Now type the path to the bin folder that just go installed. It should be in the top level of the install directory. So if you installed to C:\Borland\BC55, then type "C:\Borland\BC55\bin". If there are any spaces in the path, use quotes around the whole path. Now just OK out of all those things. This makes sure that from the command line, we can just type the name of the compiler, bcc32, and windows will be able to find it.

For Linux, we're going to be using the g++ compiler, which is the C++ version of the GCC tool (which, I believe, is the GNU C Compiler, C being a predecessor language to C++). Every instance of Linux I've ever seen has g++ installed on it. To find out, just type g++ at the command line, and hit enter. It should complain that no input files were given, and exit. If the command wasn't found and you have root access, you can su into root and try again (don't use the -C option, this still uses the calling user's search path). (If you were able to get it as root, you should probably use which to find where it is, and change permissions and or search paths so normal users can access it. You don't want to program as root, too much can go wrong). If you still can't find it or don't have root access, you're going to have to talk to your admin. If you are the admin, you're on your own, sorry. I've never had to install g++ before. On fedora, you can check yum or pirut to try to find it. Whatever your system, try whatever installation utility you use, see if it can find it for you.

Linker

Both programs, bcc32 and g++, will, by default, automatically compile and link your source files all in one go. I think they actually delegate to a separate linker program to do this, but as long you don't have some weird setup, then they should each know where to find the correct program, and we don't need a linker.

Execution

Execution is handled by the operating system. On windows, you're generated executable file should have a .exe extension. From a desktop environment, like My Computer or Windows Explorer, you can just open this file to run the program. (To open, double click it, or select it and enter, or other variations, depending on your set up. It's just like any other program). On linux, your output file might have a .out extension, or it might not have any extension, or it might have some other extension, it doesn't matter. To run it, just type ./ followed immediately by the name of the file, and hit enter.


Notes and references

  1. Some translators, called interpreters, may not actually create an output file, they do the translation in real time, reading in part of the source file, translating it, and telling computer what to do right away.
  2. How exactly we direct the computer to the file, and how exactly it "reads" it depends largely on the operating system. We'll get into this much later in the curriculum, probably around the time we talk about computer architecture.
  3. Free as in beer, not sure about freedom
  4. Open the start menu, and then the programs (or "all programs" menu. Find the "Accessories" folder, and you should see an item labeled "Notepad". Just click that.
  5. That's the coding library used to create most of these text-based environments.