c++ programming quick tips
By Vince
c++ is one of my favorite programming languages. It has been around a long time (I used it for my bachelor degree) and is very mature. The libraries and optimized algorithms are great to make software fast and efficient.
The downside is more modern languages provide a wide array of features that make development much quicker. One of the main features is garbage collection, which is a memory management technique that Python and many other languages have built-in. With c++ memory management is handled by you!
My favorite book for getting back up to speed with c++ after a hiatus is called “c++ crash course” by Josh Lospinoso. It runs through a massive chunk of the language in an organized manner.
c++ has an interesting concept (that came from c) using header files. The header file is a way to let the pre-processor/compiler know that it needs to include these functions, variables, or macros and you may be overriding/implementing them at some point. It also allows code re-use from other libraries that you can just include in your software.
One of the main reasons I like c++ is because of its strongly typed nature. This means that you have to be very deliberate in what exactly you need to store. Python on the other hand doesn’t have string typing (or really any typing) which means a variable can be anything. You can switch from a string to a list without any issue. In c++ you have to explicitly declare the variable, the amount of memory it needs, and you cannot change its type. There is even the idea of a constant (const) that tell the compiler that this variable isn’t allowed to change value, so if something tries to do that, throw an error.
Here is a quick bit of code on what that typing looks like (yes printf is an older style without streams to show data)
#include <cstdio>
int main() {
unsigned short a = 0b10101010; //binary
printf("%hu\n", a);
int b = 0123; //octal
printf("%d\n", b);
unsigned long long d = 0xFFFFFFFFFFFFFFFF; //hex
printf("%llu\n", d);
}
There is native support for many types, including:
- integers (int, short, long)
- floating points (float, double)
- characters (char)
- booleans (bool)
With the wide array of standard libraries you can add string support, bitsets, arrays, vectors, the list goes on.
Creating a development environment for macos
I almost exclusively use macos for life. Building a c++ dev environment can be a struggle since there are many components involved. Here is a quick list of things to get it running.
Install homebrew from https://brew.sh
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install gcc with homebrew:
brew install gcc
- This should also install the Xcode command line tools.
- Check for this with:
xcode-select -v
- If not, install that manually with
xcode-select --install
You may need to re-install Xcode command line if there are odd compile issues, to remove it run:
sudo rm -rf /Library/Developer/CommandLineTools
Then install it manually (I always have to do this)
- Check for this with:
- This should also install the Xcode command line tools.
Install your IDE of choice, I prefer VScode since its free and is widely supported
Here is an example c_cpp_properties.json file for VScode. This is what tells VScode the configuration details of your c++ installation and where to find all the library files. This will help with the intelligent highlighting/error detection. The order of include’s matters in this file, so make sure its top to bottom with that libraries you want.
Example c_cpp_properties.json for gcc version 12.2. This file lives in your .vscode directory for the project.
{
"env": {
"myDefaultIncludePath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/",
"/usr/local/Cellar/gcc/12.2.0/include",
"${workspaceFolder}",
"${workspaceFolder}/testing/boost/include",
"${workspaceFolder}/include",
"/usr/local/include"
],
"myCompilerPath": "/usr/local/bin/gcc-12"
},
"configurations": [
{
"name": "Mac",
"includePath": [
"${myDefaultIncludePath}"
],
"defines": [],
"macFrameworkPath": [],
"compilerPath": "/usr/local/bin/gcc-12",
"cStandard": "gnu17",
"cppStandard": "c++20",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
I also use the coderunner extension so with a quick key I can run programs. Here is the cpp string I use for that.
NOTE: Put this all on one line since JSON/VScode won’t like it. Just display like this for easier reading.
"cpp": "cd $dir && /usr/local/bin/g++-12
--std=c++20
-fconcepts
-I /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/
-I /usr/local/Cellar/gcc/12.2.0/include/c++/12
-I /usr/local/include
-I $workspaceRoot/include
-I $dirWithoutTrailingSlash/include
-g $fileName
-o $dirWithoutTrailingSlash/binary/$fileNameWithoutExt
&& $dirWithoutTrailingSlash/binary/$fileNameWithoutExt",
I have a keyboard setting for CMD-R which will run the current file and it works great.
Here is the output from our little program above after we have all this running. I hit CMD-R:
[Running] cd "/Users/vince/Documents/c++book/" && /usr/local/bin/g++-12 --std=c++20 -fconcepts -I /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/ -I /usr/local/Cellar/gcc/12.2.0/include/c++/12 -I /usr/local/include -I /Users/vince/Documents/c++book/include -I "/Users/vince/Documents/c++book"/include -g types.cpp -o "/Users/vince/Documents/c++book"/binary/types && "/Users/vince/Documents/c++book"/binary/types
170
83
18446744073709551615
I have my command runner build the file inside a binary directory, then run it automatically. This is super handy for quick development. On larger scripts/projects you may want to create a task which will build separately. There are also some great build and caching tools like bazel which can be integrated with VScode and get you super quick compilations.
If you would like to use Xcode as an IDE, here is how to install it.
- Install XCode from the app store (Xcode 13.4.1 is current as of this writing).
- Note: this can be painfully slow, so you can download Xcode directly from the developer portal here. Click “view additional downloads” and login, then search for Xcode 13.4.1. Its large at about 10GB.
- Link: https://download.developer.apple.com/Developer_Tools/Xcode_13.4.1/Xcode_13.4.1.xip
- You will need to copy the app to the applications folder, then run it to finish the install
- Once that is installed, verify that the SDK exists for that version from a command line:
ls -al /Library/Developer/CommandLineTools/SDKs/
This is just a quick glimpse into developing with c++, the rewards are definitely worth it once you get settled in!