COMP 166 - Lab 0 - Development Tools

Learning Outcomes and Introduction

In this lab assignment you will learn about:

Task 1: Software Setup

  1. Go to code.visualstudio.com in your web browser and download an installation package.
  2. Install the downloaded program as appropriate for your system.

    If you are on Windows, open the installer and follow the prompts to complete the installation.

    If you are on a Mac:

    1. Open the downloaded ZIP file to unpack it.
    2. Move the Visual Studio Code program to your Applications folder.
  3. Launch Visual Studio Code.
  4. Open the Extensions panel by clicking on the extensions icon in the left-hand toolbar, or choosing View > Extensions from the main menu.
  5. Install the C/C++ extension from Microsoft.

    screenshot of C/C++ extension banner, with arrow pointing to install button
  6. Install the Live Share extension from Microsoft.

    screenshot of Live Share extension banner, with arrow pointing to install button
  7. Install compilation tools for your system.

    If you are using Windows:
    1. Go to the Mingw-w64 download page. The download will begin automatically after a few seconds (you may need to grant the website permission to do so).
    2. Open the downloaded installer and follow the prompts to install the recommended version.
    3. When the installation is complete, open an Explorer window and find the directory where Mingw-w64 was installed. Navigate to C:\Program Files (x86)\mingw-w64. Inside of that directory will be a single folder that will be named something like "i686-8.1.0-posix-dwarf-rt_v6-rev0", but it may be slightly different on your system. Drill down into this folder, and into mingw32\bin inside of that.
    4. Copy the full path of the Mingw-w64 "bin" directory. You can click on the downward chevron in the Explorer location bar (left of search and refresh) to get a text representation that you can copy to the clipboard with ctrl+c.
    5. Use the start menu search box to find and open the "Edit environment variables for your account" tool (it can also be found in Windows Settings).
    6. Select the Path variable and then click the Edit… button. A new editor dialog will open.
    7. Click the New button and paste in the path to the Mingw-w64 folder that you copied earlier, followed by "\bin".
    8. Click the OK button to save your changes.
    If you are using MacOS:
    1. Open a terminal panel by choosing Terminal > New Terminal from the main menu, or pressing ctrl+`
    2. Type xcode-select --install and press enter.

      If you've already done some programming on your system before, you may see an error message. That's fine, it means you already have the tools and are ready to go!

    3. A dialog will appear asking if you would like to install the command line developer tools.

      screenshot of dialog from running xcode-select

      Choose the "Install" button and wait for the download and installation to complete.

  8. Back in VSCode, bring your focus back to the terminal panel by clicking anywhere inside it or pressing ctrl+` (the ` key is usually found to the left of the 1 key).
  9. Type gcc and press enter.

    If you see a "command not found" or "'gcc' is not recognized" error, ask for help!

    If you see "error: no input files", everything is working correctly!

Task 2: Hello World!

  1. Create a comp-166 folder somewhere you'll be able to find it later.
  2. Download the hello_world.c source code below, and move it into this folder.

    Download hello_world.c

    Don't worry about understanding all of this code for now; we'll be learning about each piece later!

  3. Open your folder in VSCode (File > Open…)
  4. Open the Explorer panel by clicking on the Explorer icon in the left-hand toolbar, or choosing View > Explorer from the main menu.
  5. Open hello_world.c in an editor tab by clicking on its name in the Explorer panel.
  6. Admire the pretty syntax coloring! If it's not working for you, check that you've finished everything in Task 1 or ask for help.
  7. The red squiggly underlines in our code (and red text in the Explorer pane) are indicating that this source file has an error. We cannot compile or run our code until all errors are fixed (yellow underlines will appear for warnings that indicate potential problems but won't prevent your code from compiling and running). Hover your mouse over the underlined code or open the Problems panel (View > Problems in the main menu) to find out what's wrong.

    What does this error mean!?

    The error messages you get from the compiler can be quite cryptic, especially when you're just starting to program. They tend to be fairly short and use a lot of specialized jargon.

    Do not depair! You will learn what a lot of this jargon means throughout this course, and as you get more experienced, just knowing where the error occurs can often be enough for you to spot the problem.

    In the meantime, the most common errors you will see when you are just beginning will be:

    expected …
    syntax error before …
    Something is wrong with your syntax. Check for missing semi-colons and braces.
    identifier … is undefined
    You probably misspelled something, or forgot to use quotes. This can also happen if you've forgotten to define a variable before using it.
    undefined symbols … linker command failed
    You've either misspelled a function name (case matters!) or forgot to #include the header where it is defined.

    If you're stumped, ask for help during a lab session or post a question on the course forum!

  8. Fix the error by removing the line that says "Oops!" (line 4), and save your changes.
  9. Open the terminal panel (ctrl+`), type gcc hello_world.c, and press enter

    If all went well, there should be no errors or warnings reported, and a new a.out file will appear in the Explorer panel (or a.exe if you're using Windows). If not, ask for help!

  10. If you're using Windows, type ./a.exe in the terminal and press enter. On MacOS, type ./a.out instead.

    You should see the words "Hello world!" appear just below your command. Congratulations, you've just compiled and run your first C program!

  11. Extend the hello_world program to also output:

    • Your full name
    • The reason you're taking this course
    • Two or three things you hope to learn in this class
  12. Compile and run your code each time you make a change to make sure it still works as expected.

Task 3: Keeping it Organized

That comp-166 folder is going to get messy quickly if we just dump everything in there for every lab. Let's make some sub-folders to keep it organized, and setup VSCode to do some of the grunt work for us.

  1. Create a folder inside of comp-166 named like LastnameFirstname_lab0 and move your source code into it.
  2. Create additional folders for future labs 1 through 6, following the same naming pattern.
  3. Now that your code is in a folder, it's a little more tedious to compile it from the terminal. You'd have to type:

    gcc LastnameFirstname_lab0/hello_world.c

    Fortunately, VSCode can do this for us!

    Choose Terminal > Configure Default Build Task… from the main menu (and press enter if asked to choose which tool to use).

  4. You should now be looking at a tasks.json file that was generated for you. We'll need to modify this file slightly so that VSCode doesn't output the program in the same folder as the source. We'll add a little more error checking at the same time.

    There should be section of this file that currently looks like this:

    
    					"args": [
    						"-g",
    						"${file}",
    						"-o",
    						"${fileDirname}/${fileBasenameNoExtension}"
    					],
    				

    Change that section to read:

    
    "args": [
    	"-Wall",
    	"-Wextra",
    	"-Wpedantic",
    	"-std=c99",
    	"-g",
    	"${file}",
    	"-o",
    	"${fileBasenameNoExtension}"
    ],
    				

    and save your changes.

  5. Switch back to your hello_world.c file and test that the build works by choosing Terminal > Run Build Task… from the main menu.

    Notice that a new file (or two) has appeared in the Explorer pane. With this configuration, VSCode will create an executable with the same name as your source file, instead of the default "a.out" we got in Task 2.

  6. Open the terminal and test that you can run your program. Eg:

    ./hello_world

    Note that VSCode's "Run Without Debugging" doesn't work properly for C code, so you'll have to run your program manually from the terminal each time. We'll setup and learn about debugging in Lab-1.

Task 4: ZIP and Submit

Create a ZIP archive of your lab-0 folder, including the folder itself. Make sure that it includes your hello_world.c source code and does not include any compiled programs.

To create a ZIP file on MacOS:

  1. Open the Finder application
  2. Navigate to your comp-166 folder
  3. Right-click (or control-click) your lab-0 sub-folder
  4. Choose Compress "…" from the context menu

To create a ZIP file on Windows:

  1. Open a File Explorer window
  2. Navigate to your comp-166 folder
  3. Right-click your lab-0 sub-folder
  4. Choose Send to > Compressed (zipped) folder from the context menu
  5. Type a name for the new ZIP file and press enter to accept

Submit your ZIP file to the Lab-0 assignment on D2L.

All future labs will be submitted in the same way, but will not include a separate task and instructions for this process!

Submission and Grading

Completing all tasks in this lab should result in a hello_world.c program inside of a Lab-0 folder named like LastnameFirstname_lab0. Create a ZIP archive containing this entire Lab-0 folder and upload it to the D2L assignment. Do not include compiled binaries in your submission.

NOTE: This assignment is to be done individually. You can help one another with problems and questions, but in the end everyone must write and submit their own code.

Criteria Marks
Submitted code to D2L with correct packaging 1
Total 1