$29
Welcome!
Welcome to your frst COMP 3522 lab. Today’s lab is all about seting up your tools, and using them to generate and share a simple Hello World application. You will install CLion, git, g++, and create your frst C++ project.
You’ll also learn how to use Github classroom, commit and push your code. Let’s get started!
• CLion setup
Please complete the following:
1. Start by signing up for a free renewable one-year JetBrains student license at htps://www.jetbrains.com/student/ so you can download and use any of the JetBrains desktop products.
2. Install the JetBrains C++ IDE called CLion (2023.1.5) on your machine htps://www.jetbrains.com/clion/download/ .
◦ Install with the following options:
3. Ensure git is installed on your laptop. If it isn’t, install it now from htps://git-scm.com/downloads. Continue installing with all default setings using the installer
4. Sign up for a free GitHub Student Developer pack at htps://education.GitHub.com/pack so you can store unlimited private repositories on GitHub.
5. We will compile using g++ this term. If you are using Windows, you will need to install Cygwin (htps://www.cygwin.com/ ) or MinGW (htps://www.mingw-w64.org/ ).
6. If you’re having issues installing Cygwin on PC, try following this Youtube video: htps://youtu.be/IVNLaGkvDw0 . Make sure to install the following packages:
• gcc-g++
•
• make
•
• gdb
•
7. If you are using a Mac, the g++ command invokes the LLVM compiler (if you don’t believe me, open a Terminal and enter the command g++ –version (that’s a double dash)). The easiest way to fx this is to install g++ using Homebrew, and then invoke g++ using the command g++-13. Be aware the g++ version may be newer on your machine:
8. Ensure CLion uses the correct g++ compiler in Setings/Preferences by selecting Build, Execution, Deployment | Toolchains and seting C++ Compiler.
9. If Cygwin isn’t working for you, try using MinGW. Just ensure the C++ Compiler is using g++
Getting the Personal Access Token
You may need to get a Personal Access Token before cloning the repo
1. To log in with a github personal access token, open CLion and go to Setings > Version Control > Github. If you’re already logged in, press the – buton to log out
2. Select the + icon, then select “Log in with Token…”
3. Select “Generate” on the window that pops up
4. A web page should open up mentioning, “New personal access token”. Change the token expiration to something past the end of the term
5. Scroll down to the botom and press “Generate Token”
6. Copy the generated token value from the website and paste it into CLion and select “Add Account”. You should now be able to clone from your repo
Getting the Course’s sample code:
1. We’ll be using Github Classroom this term to track your lab/assignment submissions. For now, let’s use it to clone all of the course’s sample code. Go to this link:
htps://classroom.github.com/a/guzoC6DD . And Accept the assignment:
2. Refresh the following page and click on the repository link that’s provided
Clone from repo to Clion
3. Next you will clone the repository. Click the green Code buton and copy the repo url
4. Return to CLion and create a new project, or resume an existing project. Find VCS option at the top of the screen and select Get from Version Control
Alternatively, if you don’t see the VCS option, you may see Git. In that case select the Clone… option
5. Paste the repo link into the URL feld of the window that appears and select Clone at the botom right.
6. The following popups may or may not appear. Select Trust Project popup
7. Check the box in the top left asking about reloading CMake projects. And select OK at the botom
Getting the Lab 1 code:
8. Go to this link: htps://classroom.github.com/a/YLH3Sjn2 and Accept this
assignment:
9. Follow the steps similar to points 2 - 7 to clone the lab 1 repo.
10. Examine the project fles that are created. Note there is a fle called main.cpp. It contains the main method which prints a message.
11. CLion uses cmake, which is a build tool similar to ant. Recall that ant uses a fle called build.xml. cmake uses a fle called CMakeLists.txt. Take a close look at the fle. Can you guess what each line represents?
a. There is a very good CLion cmake tutorial here: https://www.jetbrains.com/help/clion/quick-cmake-tutorial.html
12. Ensure the following line is in your CMakeLists.txt in CLion, this adds helpful warning messages for you:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic")
Commit and Push
13. Modify the "Lab 1, Hello, World" output printed to the screen, and commit your changes.
a. Reminder, commiting saves changes to your local machine. Pushing saves changes to the online repo. Make sure to commit and push when you’re making large changes and making your fnal submission to be marked.
14. Select Git from the top menu and select Commit to commit your changes
15. Enter a commit message, then select Commit at the botom right of the popup to fnalize the commit.
16. Now that the code is commited to your local machine, push the commited code to your online
github repo. Select Git and Push from the top menu. Then select Push in the popup that appears
17. A reminder to commit and push your code regularly.
a. Protip – you can commit and push at the same time by entering the commit menu, then selecting the down arrow and selecting Commit and Push
Let’s start programming!
18. Fill out your name and student number at the top of main.cpp
19. Implement a function called gcd. The gcd function accepts two positive integers and returns the greatest common denominator of the two integers. Your function prototype and comment should look like this:
20. Ensure you create a .hpp fle, not .h. Clion creates .h fles by default. The following steps will set this up.
a. Right click your lab1 folder and select New -> C/C++ Source File
b. Enter gcd as the name of the fle. Press the wrench icon in the botom right
c. Ensure source extension is cpp and header extension is hpp. Select OK to confrm your changes
d. Ensure both Create an associated header and Add to targets checkbox is selected. Then select OK to fnish creating the gcd fles.
i. Create an associated header – automatically creates a .hpp header fle along with the .cpp source fle
ii. Add to targets - adds the new .hpp and .cpp fle to the CMakeLists.txt fle.
This is required for the new fles to be compiled as part of your project
21. Write the gcd function prototype in the gcd.hpp header fle
22. Write the gcd function’s complete implementation in the gcd.cpp fle
23. Write code in main.cpp to call your gcd function to show it works. Make sure to import the gcd function into main.cpp by adding #include gcd.hpp at the top of main.cpp
24. Ensure you commit and push your new function. Prove it works by invoking it in the main function.
25. Do NOT submit this Lab via D2L/The Learning Hub, submit by pushing it to your GitHub repo via CLion
26. That’s it, you’re fnished lab 1!
• Grading
This lab and all future labs will be marked out of 10. For full marks this week, you must:
1. (2 points) Install or have already installed current versions of CLion, g++, and git
2. (3 point) Successfully confgure CLion to work with g++, GitHub, and GitHub Classroom
3. (2 points) Commit and push to GitHub after each non-trivial change to your code
4. (3 points) Implement and test the gcd function described above (remember to create a separate header fle for the function prototype).
Test cases:
Try out these two numbers, along with your own custom test cases:
Find the GCD of: 5764375, 46875
Troubleshooting:
If you’re a mac user and geting an error, when running any code, similar to the following:
The C compiler
"/usr/bin/cc"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /Users/a1/Cluster_analys/cmake-build-debug/CMakeFiles/CMakeTmp
It may be because you haven’t installed the xcode developer tools before installing brew and gcc. In that case:
• Please open the Terminal, and run the following command: xcode-select --install
• After that in CLion please select `Tools | CMake | Reset Cache and Reload Project`
credit:
htps://intellij-support.jetbrains.com/hc/en-us/community/posts/360010551220-Can-t-compile-si
mple-test-program-on-Mac
Credit to Jayden, if you need access to the g++ executable and can’t fnd the location, here’s how to do it:
“I believe I've found the location of the actual g++ executable fle
1. Go to Macintosh HD directory
2. Press cmd + shift + .(dot) (simultaneously) and it will reveal hidden fles + folders
3. Go into /usr/bin, where you should fnd both the gcc & g++ executable fles”