CPSC170
Fundamentals of Computer Science II

Lab 0

Introduction to Linux, Emacs, and C++

Linux

Open up the command terminal. We will review a few commands that will help you navigate the file system. More commands can be found in the links provided below. Type the command

pwd

It should print the current directory (folder) you're working in. Now try typing

ls

It prints the files and folders in the current directory.

The cd command is used to change the current directory. If there is a folder in the current directory, you can move to that folder by typing cd followed by the name of the folder. For example, if there is a folder called MyDir in the current directory, then type

cd MyDir

This changes your current directory to MyDir. Use pwd to verify the change. You can move to the parent of the current directory by typing

cd ..

To make a new directory called NewDir type the command

mkdir NewDir

This will create a new directory in the current directory called NewDir. You can learn more about these commands by looking up their manual pages. For example, to look up the manual for ls, type the following command

man ls

You can learn more about the command line using the following link:

The Linux command line for beginners

Emacs

Emacs will be our text editor. A tutorial is provided in the link below. We will now write our first program. Use the previous commands to move to the directory you'd like to work in. Type the following command

emacs hello.cpp

Now type in the following program.

#include <iostream>

int main() {
   std::cout << "Hello, world!" << std::endl;
   return 0;
}

Save your program with the command C-x (keep the control key pressed then press the x key) followed by C-s. Exit Emacs by typing C-x followed by C-c. Now we can compile the program using the command g++ hello.cpp. If you typed in the program correctly this command should produce a file called a.out. This is an executable file that can be executed by the following command.

./a.out

Once executed, the string "Hello, world!" should be printed on the terminal.
Try removing a semicolon from your program and recompiling. What happens?
Try changing the name main() to mainn() What happens?
Try writing the whole program with the exception of #include <iostream> on a single line. What happens?
Experiment with other changes, see what breaks the code.

A Guided Tour of Emacs

C++

The following link provides a great resource for learning C++. Most of the topics we will cover are also covered on the cplusplus website. I encourage you to read ahead of the class. This way you can ask questions about what you don't understand, this helps you retain what you've learned better. We will not cover all aspects of C++, the goal is for us to cover enough for you to be able to pick up new things on your own.

cplusplus.com: Tutorials