DM560, Introduction to Programming in C++

Sheet 1

Task 1

  1. In the course we will work on Linux/GNU operating system.
    Review this tutorial on Linux/GNU.
    More on command-line tools from CodeAcademy.
    More on remote access.

  2. The easiest way to obtain a development environment is to use the one prepared and running on the SDU eScience Cloud:
    Coder C++.
    Guidelines.

  3. If you want to use your local machine you need two elements:
    a text editor and a compiler

Task 2

Make sure you can edit, compile and execute your helloworld.ccp program from a Unix shell. A shell is a user interface for access to an operating system’s services. Commonly, it refers to a command-line interface (CLI) as opposed to a graphic user interface (GUI). A Unix shell is a command-line interpreter that provides a traditional Unix-like command line user interface. It is available under these names/programs:

The Command Prompt in Windows is a shell but based on DOS rather than Unix. We will use the Bash shell. Your Windows file system is located at /mnt/c in the Bash shell environment. If you want to use Windows tools to edit your files (for example with Notepad++ or atom), then you must work in the Windows directories. For example:

mkdir /mnt/c/DM560 
cd DM560 

If you really want to know where your Linux files are from Windows, read this.

Compiler

Make sure you have the right version of g++:

g++ --version 

If you do not have g++ installed you will be suggested to install it.

For the GNU compiler we will need a version number above 5.4 in this course. Explore the many other parameters of the compilers with g++ --help and man g++.

If you have lost your Windows Subsystem for Linux (WSL) password follow these instructions.

Editing

Try vi filename it works from shell.

  • i to insert text
  • esc to exit insert mode
  • :x to save and quit
  • :q to quit without saving
  • :q! to quit without saving the modifications done.

Building

1) Compiling:

g++ -c helloworld.cpp 

check that the file helloworld.o has been created. This a binary file. You can decode its content with objdump -d helloworld.o. The file still misses the specification of the system libraries. We include these specifications in the linking process.

2) Linking:

g++ -o helloworld helloworld.o

The flag -o outfile specifies the name of the executable file to create. This generates an executable file: helloworld. It is also in binary format and you can decode it with objdump -d helloworld. Compare the output of objdump with the output at the Compiler Explorer page (check that the machine and the compiler match with yours in the right window).

Note: with only one source file it is possible to carry out both compilation and linking at once via:

   g++ -o helloworld helloworld.cpp

Execution

./helloworld

Task 3

After you get the helloworld program to work, make a few mistakes to see how the tools respond; for example:

  • Forget the header
  • Forget to terminate the string
  • Misspell return (e.g., retrun)
  • Forget a semicolon
  • Forget { or }

Modify then the program to output multiple lines of text.