Command Line Mac Tools


This document shows how to create and download project files for use with the Mac tools from the command line.

Before following these instructions, you should have already downloaded and installed the CLion software (if you haven't, go back and do so).


Section 1) Testing the Mac and Clang

In this section you will create and run a trivial C++ project/program (including editing it, and seeing how syntax errors are reported).

First, we will perform a one-time only setup.

  1. Start a Terminal by clickich the (Mac Terminal icon) on the Dock.

    Perform the following actions in your come directory (mine is Richards-MacBook-Air: ~richardepattis).

    1. Use any editor (vi is automatically available on the Mac; other editors can be downloaded) to update your ~/.bash_profile file to contain the following line at the end (or put it after the other PATH settings in that file; e.g., copy/paste it):
      PATH="/Applications/CLion.app/Contents/bin/cmake/bin":$PATH ; export PATH
    2. Terminate the Terminal and start a new Terminal (which executes the ~/.bash_project file automatically).
    3. Type echo $PATH and ensure the path you added appears.

    Now we will create directories for use in ICS-46, and create a trivialtest project to ensure the required operations work correctly.

    1. Make an empty directory for all ICS-46 Projects (ics46projects) using the mkdir command.
    2. Connect to the ics46projects directory using the cd command.
    3. Make an empty directory for the Trivial Test Project (trivialtest) using the mkdir command.
    4. Connect to the trivialtest project directory using the cd command.
    5. Use any editor to create a main.cpp file that contains the following code (e.g., copy/paste it):
      #include <iostream>
      
      using namespace std;
      
      int main() {
          cout << "Hello, World!" << endl;
          return 0;
      }

    6. Create a CMakeLists.txt file that contains the following code (e.g., copy/paste it):
      cmake_minimum_required(VERSION 3.3.2)
      project(trivialtest)
      
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
      
      set(SOURCE_FILES main.cpp)
      add_executable(trivialtest ${SOURCE_FILES})

    7. List the contents of the ics46project/trivialtest directory storing these two files, by using the ls command.
    8. Create the make files needed for this project, by using the cmake . command (remember, you will do this just the first time you use a project).
    9. List the contents of the ics46project/trivialtest directory storing the new files it creates (and the new CMakeFiles directory), by using the ls command.
    10. Build the trivialtest file needed to execute this program, by using the make command (you will do this each time that you modify a .cpp file in a project).
    11. List the contents of the ics46project/trivialtest directory storing the new file it creates, by using the ls command.
    12. Run the trivialtest executable file, by using the ./trivialtest command.

    The following ~/ics46projects/trivialtest Terminal window shows all these actions (D through O).

  2. Use the finder to navigate to the Macintosh HD/users/yourname/ics46projects/trivialtest directory.

    The following Finder should appear on your screen.

    You can use any combination of Mac OS and Terminal command lines to manipulate the files in this directory: whichever is simplest.

    It would also be useful to make an alias to this ics46projects folder and place it conveniently on the desktop, so you can easliy reach the projects you are working on.

  3. Edit, rebuild, and rerun the main.cpp program,

    The following ~/ics46projects/trivialtest Terminal window shows all these actions.

  4. Create a syntax error (remove the ; at the end of the cout line), and then follow the rebuilding/rerunning steps above The following ~/ics46projects/trivialtest Terminal window shows all these actions.

    The failed results of attempting to rebuild the program appear after the make command. In this case, the Clang compiler accurately indicates the source of the error. I have lengthened the window so as not cause lines to wrap.

We will now continue by learning how to download a project and run it. During the quarter, you will be asked to do this for all programming assignments and many quizzes.


Section 2) Testing Downloadable Projects with the Course Libraries

In this section you will download the course libraries and then download/build/run a project that contains various programs that use these libraries.

  1. If you have not already done so, make an alias to the folder Macintosh HD/users/yourname/ics46projects named named ics46projects and place it on your desktop.

  2. Download and unzip the file template; Copy its three directories (courselib, googletestlib, and test_all_data_types) into the directory that ics46projects aliases; it should already contain the trivialtest directory, created by the operations above.

    Generally, whenever you download projects, you will unzip them and then copy their directories into the directory that ics46projects shortcuts.

    The following ~/ics46projects Terminal window lists these four directories.

  3. Connect to the test_all_data_types directory, by using the cd command. Hint: type only cd te and then press the tab key and Terminal should complete the name of this directory; generally using tab for directory/file name completion can drastically reduce your typing in Terminal.

    List the contents of the ics46project/test_all_data_types directory, by using the ls command. Notice that this project folder contiains various .cpp files and a CMakeLists.txt file

    The following ~/ics46projects/test_all_data_types Terminal window shows all these actions.

  4. Edit the driver.cpp file by uncommenting lines 30-35, so they appear as follows
    #include "driver_set.hpp"
    
    int main() {
      ics::DriverSet d;
      return 0;
    }

  5. Build and run the main.cpp program,

    The following ~/ics46projects/test_all_data_types Terminal window shows all these actions.

    Experiment with this driver by entering commands (and their arguments, when prompted) to better understand the set data type. You can enter the q command to terminate the driver, or you can type command-c to terminate the driver.

    I suggest that you try terminating the driver with a q command; then, rerun the program and terminate it with command-c.

  6. Using the ics46projects alias (you created in step 1), naviagate to the test_all_data_types project folder.

    The following window should appear on your screen.

  7. Double-click the test_all_data_types file to run this program in a Terminal window. The following Terminal window should appear on your screen.

    You can use all commands except the lf command, which is explained below. Then terminate the Terminal window.

  8. Find the input files directory in the test_all_data_types folder and copy all of its .txt data files up one level in the directory (the one that contains the test_all_data_types file).

    Then, run the program from a Terminal window (connnected to the test_all_data_types directory) issue the lf command (load from file); when prompted for the file name, just press the enter key to choose the default (loadset.txt).

    The following ~/ics46projects/test_all_data_types Terminal window shows all these actions.

    Note that because the directory name input files contains a space, it appears in double quotes ("input files") in this and most Terminal commands.

    IMPORTANT: If a program reads data files, the root directory for the specifying the files is the directory in which the executable file appears. It is simplest to copy all the data files into this directory. An alternative would be to leave the files in the input files directory, and enter their names like input files/loadset.txt (note no quotes are needed here: this is not a Terminal command).

    When you double-click an executable file to run it (as you did in Step 7) it does not run the program in the expected directory; so a program run this way cannot correctly read files in that directory!

  9. A typical project folder may have more than one .cpp file with a main function (as test_all_data_types has). Only one of these .cpp files can have its main function uncommented, otherwise the build will fail. When working on project, it is typical to

    1. Comment-out code in one .cpp file.
    2. Uncomment code in another .cpp file.
    3. Rebuild/Rerun the newly uncommented .cpp file.

    In the test_all_data_types project folder, the driver.cpp file has many main functions: we uncommented one (in step 4) to allow us to run the Set driver. If more than one main function is uncommented, attempting to build the code will result in the compiler specifying a redefinition of 'main' error.

    The test_all_data_types project folder also contains many .cpp files (their names all start with test) that each contain one main function (each is a Googletest for one data type). If more than one file contains a main function, attempting to build the code will result in the linker specifying a multiple definition of 'main' error.

    To switch this project from running the Set driver to the Set Googletest

    1. Edit the driver.cpp file and comment-out lines 30-35.
    2. Edit the test_set.cpp file and uncomment all its lines.
      Most program editors have a special command that allows commenting on selected lines to be toggeled.

    Rebuild/Rerun the newly uncommented .cpp file.

    The following ~/ics46projects/test_all_data_types Terminal window shows all these actions.

  10. IMPORTANT:When downloading all other project folders, Mac users must remove the following two lines from their CMakeLists.txt files.
    set(CMAKE_CXX_COMPILER "/cygdrive/c/cygwin64/bin/clang++")
    set(CMAKE_C_COMPILER "/cygdrive/c/cygwin64/bin/clang")

    These two lines are necessary on PCs to find the Clang C and C++ compilers, but they are not necessary on Macs. I want to supply just one download, and it is easier for Mac users to remove these two lines than for PC users to add them.

    For this document, I provided a link to Mac-compatible CMakeLists.txt files, so this operation was unneeded.

We are now at the end of this handout. You will not need to repeat the first two sections during the course, but you will frequently download project folders and use them as demonstrated in section 2.