COMP6771
Advanced C++ Programming
Week 1.2
C++ Environment
Author: Hayden Smith
-
Prepare yourself for the content in this course by:
- Getting familiar with the basics of Gitlab
- Getting familiar with the basics of the C++ environment
- Building our first program
- Testing our first program
Why?
Gitlab
- All of the coding in this course comes from Gitlab.
- If you aren't familiar with Gitlab, we have prepared "lab0" for you.
- It's important you're familiar with git adding, git committing, git pushing, and accepting merge requests.
- https://gitlab.cse.unsw.edu.au
We can compile and execute this easily.
First programs
#include <iostream>
int main() {
// put "Hello world\n" to the character output
std::cout << "Hello, world!\n";
}
$ g++ -o hello hello.cpp
$ ./hello
We can compile and execute this easily.
First programs
#include <iostream>
#include "age.h"
int main() {
// put "Hello world\n" to the character output
std::cout << getAge() << "\n";
}
int getAge() {
return 5;
}
$ g++ -o age age.cpp
$ ./age
int getAge();
age.c
age.h
We can compile and execute this too.
Declarations in .h files, definitions in .c files
First programs
#include <iostream>
#include "age.h"
int main() {
std::cout << getAge() << "\n";
}
$ g++ -o age age_main.cpp age_lib.cpp
$ ./age
int getAge();
age_main.c
age.h
#include <iostream>
#include "age.h"
int getAge() {
return 5;
}
age_lib.c
- Imagine having thousands of header and cpp files?
-
You have a few options
-
Manually create each library and make sure you link all the dependencies
- You would have to make sure you linked them all in the right order
-
Create one massive binary and give it all the headers and cpp files
- Extremely slow
- Hard to build just parts of the code (eg. To run tests on one file)
-
Makefiles
- Unwieldy at large scale (hard to read and hard to write)
- Any better options?
-
Manually create each library and make sure you link all the dependencies
The problem with classic compiling
-
The solution to this chaos is to use build systems.
-
With these systems, you simply have to declare files and relationships between them, and the build system will figure out what to run for you.
-
In COMP6771 we will be using CMake for compilation in conjunction with VScode for editing.
Managing larger projects
In COMP6771 we will be using CMake for compilation in conjunction with VScode for editing. We will be using C++20
Managing larger projects
Let's follow instructions in SETUP.md of tut01 to setup our environment. We can find tut01 on Gitlab via Webcms3.
The rest of this lecture will be a demo of the basic setup.
Managing larger projects
Catch2 is just one particular framework you can use to test with C++. More information on it can be found here.
Catch2
- Test API, not implentation
- Don't make tests brittle
- If your code changes, your tests should change minimally
- Make tests simple
- It should be obvious what went wrong
- Don't put if statements or loops in your tests
- Any complex code should be put in a well-named function
Principles of testing
Feedback
COMP6771 21T2 - 1.2 - Environment Setup
By haydensmith
COMP6771 21T2 - 1.2 - Environment Setup
- 696