How to Write C++ Programs in Linux

For most of you, the easiest way to write C++ programs from your home computer will be to connect to one of the Linux computers in the CS network and use the tools there to write, compile, and run your programs. To do that, just follow a few easy steps:

  1. Connect to a CS machine

    First you'll need to figure out the name of a computer to connect to. You won't need to know where the computer is physically located - you won't ever need to actually sit at it - but you'll need to know what its name is so you can establish a connection to it. To find a name, go to stuff.cs.uchicago.edu on the web, which contains a search form with which you can find a CS machine. You can specify any criteria you like; the only thing that's important here is that you specify "Linux" under "Operating Systems." Click "Search" and you'll get a list of computers. Pick one of them and remember its name.

    Now you'll need to connect to it, and to do that you'll need to download an SSH client if you don't already have one. You can use PuTTY for Windows or Nifty Telnet for Macintosh. Whichever you use, set it up to connect to the computer you picked out and then establish a connection. If everything is set up correctly, your SSH client ought to pop up a window that will ask you for your username and password. Give it your username and CS password and it ought to give you a command prompt. You're now connected and you can get started programming.

  2. Edit your program files

    There are a number of ways you can edit a file in Linux. There are several different editors available for your use, each with its own benefits and drawbacks.

  3. Transferring files from your computer to a Linux machine

    If you'd rather, you can actually edit files on your own Windows or Macintosh computer and transfer them to the Linux machine to compile them. Even if you have a C++ compiler on your own computer, you should compile your program on Linux just to make sure it works on that platform because that's the platform on which we'll be testing your homework submissions. To transfer programs from your home computer to a Linux computer, you've got several options. First, the SSH program you're using may have a method for transferring files built in - consult your program's help files and you may find something. If that doesn't work, you can download a program such as WinSCP for Windows or Nifty Telnet for Macintosh.

  4. Compile your programs

    To make your program, you'll need to run a compiler on its source code. On Linux, you can just type

    g++ -Wall -g -o myprogram myprogram.cc

    where myprogram is the name you want your final program to have and myprogram.cc is the name of your source code file.

    That is sufficient for now, but you'll find as your progams get larger that you'll want to split them up into different files. Once you do that, you'll want to have a tool that automates the process of compiling the various pieces of your program and linking them together. To do that, there is a special program called make, and it's a good idea to learn how to use it as soon as possible. For this program, just make a file named Makefile with the following contents:


    myprogram: myprogram.cc
         g++ -Wall -g -o myprogram myprogram.cc

    (Note that the indentation on the second line must be a tab character, not spaces.) Make works by dependency checking: if make wants to build the thing on the left of the colon on the first line, it first makes sure it has all the things on the right, recursively making them if they don't exist or aren't up-to-date and you've provided a rule in your makefile for making them. Once it has everything it needs, it executes the listed command, which hopefully will produce a file named whatever the name on the left of the colon is.

    Once you've got a makefile, you can just type make at the command prompt and everything will get built. If there are errors, you'll need to fix them before you run your program. (If you're using emacs on Linux, you can do all this from within emacs by typing escape, then x, and then typing the word compile and hitting enter. If there are error reports, they will show up in a special window; you can click on them individually to go to the lines that have errors.)

  5. Run your program

    Once you've compiled your program, you ought to be able to run it just by typing

    ./myprogram

    where myprogram is the name of your program. Congratulations! You've just run your first C++ program on Linux!

    A couple words of caution: first, if you name your program a name that happens to already be a Unix program and you don't type ./ in front of your program's name, you'll get the already-installed program rather than your own. I've seen people waste hours trying to figure out why their program didn't do anything because they named it test and when they typed test at the command prompt they got Unix's standard program test instead. (I'm a little weird; I discovered this problem when I named a program ed.) Just make sure to type ./ and you'll be sure you're getting your program.

    Second, unlike Scheme programs, if you compile a C++ program on a computer running Linux on an x86 processor, the resulting program will only run on Linux x86 processors. If you want your program to run on SunOS, MacOS, or Windows, or you want it to run on a different processor family, you have to recompile it for that platform.

    That's all! Have fun programming!