ENCM 335 Fall 2018: Command-Line C Programming on Macos
Total Page:16
File Type:pdf, Size:1020Kb
page 1 of 4 ENCM 335 Fall 2018: Command-line C programming on macOS Steve Norman Department of Electrical & Computer Engineering University of Calgary September 2018 Introduction This document is intended to help students who would like to do ENCM 335 C pro- gramming on an Apple Mac laptop or desktop computer. A note about software versions The information in this document was prepared using the latest versions of macOS Sierra version 10.12.6 and Xcode 9.2, the latest version of Xcode available for that version of macOS. Things should work for you if you have other but fairly recent versions of macOS and Xcode. If you have versions that are several years old, though you may experience difficulties that I'm not able to help with. Essential Software There are several key applications needed to do command-line C programming on a Mac. Web browser I assume you all know how to use a web browser to find course documents, so I won't say anything more about web browsers here. Finder Finder is the tool built in to macOS for browsing folders and files. It really helps if you're fluent with its features. Terminal You'll need Terminal to enter commands and look at output of commands. To start it up, look in Utilities, which is a folder within the Applications folder. It probably makes sense to drag the icon for Terminal to the macOS Dock, so that you can launch it quickly. macOS Terminal runs the same bash shell that runs in Cygwin Terminal, so commands like cd, ls, mkdir, and so on, are all available on your Mac. Command-line C programming on macOS page 2 of 4 Geany (or some other programmer's text editor) This year I've recommended Geany for my section of ENCM 335, because it's free, it works well for editing C and Python, it's reasonably easy to learn, and it's available for Microsoft Windows, macOS, and Linux. Geany for macOS is easy to download and install. Start here: https://www.geany.org/Download/Releases If you have already been using another programmer's text editor on your Mac, and you like it, there is no need to switch to Geany. Xcode Xcode is the application Apple supplies to developers who want to create software for macOS or iOS. Xcode can be found using the macOS App Store. Xcode is free, but it's huge! Depending on the speed of your internet connection, it may take over and hour to download and install it. It also occupies several GB of disk space, so make sure your hard drive isn't close to full when you attempt to set it up. After downloading Xcode, you'll need to launch it at least once, to complete the installation. After that, though, you won't need to launch it to do ENCM 335 assignments. Instead, you'll use the clang command in Terminal to build executable files—one of the effects of installing Xcode is to make the clang command available. An example programming session Figure1 shows part of a Mac screen for a student working on Exercise B of Lab 2 in ENCM 339 in Fall 2017. (There will be a similar exercise in Lab 2 in ENCM 335 in Fall 2018.) I'll refer to this screenshot repeatedly to explain the key steps in building and running C programs using Terminal. Files and folders The screenshot shows that the student has made a folder called ENCM339 on their desktop. (Desktop is one reasonable place to put your files for this course, but choices, such as Documents, are reasonable too.) Within ENCM339 are Lab1 and Lab2 folders, and within the Lab2 folder there are folders for some of the Lab 2 exercises. Notice that in the Terminal window the student has used two cd commands to get to the folder that has the files for Exercise B. The first cd command was cd ~ /Desktop/ENCM339 That made the thiscourse folder the working directory. The second cd command was cd Lab2/exB That moved two levels down from the ENCM339 to the folder with the Exercise B files. Obviously, those commands are just examples. You'll need to use variations of those commands, appropriate to the names and organization of folders you've chosen to use on your Mac. Command-line C programming on macOS page 3 of 4 Figure 1: Screenshot of command-line C programming with a Mac. Three windows are visible: a Finder window for browing folder and files, a Terminal window for entering commands, and a Geany window for editing code. If you zoom in on the image, the text in the windows should be quite legible! Command-line C programming on macOS page 4 of 4 Editing text You can see in the screenshot that the file lab2exB-partI.c is open in Geany. For this particular exercise, no editing of the .c file is needed, but Geany is convenient not just for writing C code, but also for reading it. Building and running an executable The command to build an executable from C code is clang.(gcc will also work, but on a current Mac, gcc is just an alias for clang, and doesn't really invoke the C compiler from the GCC project.) (If you're interested in learning more about the two main open-source compiler projects, here are some web links: https://clang.llvm.org https://gcc.gnu.org It's great for developers that these two ongoing projects exist!) You can see in the screenshoot that the command to build and executable for Part I of Exercise B was clang -Wall lab2exB-partI.c This created an executable with the default name of a.out (not a.exe, which is the default with gcc on Cygwin). The command to run the executable was ./a.out It's possible to choose a non-default name for an executable with the -o option of clang. This command would have generated an executable called partI ... clang -Wall lab2exB-partI.c -o partI The command to run the executable would be ./partI Note that for macOS, unlike Microsoft Windows, names of executable files normally do not have include a .exe extension..