Private Computer — macOS
To program on a Mac, you need Apple's Command Line Tools. They contain the compiler and are much smaller than the full Xcode development environment.
1. Installing the compiler
-
Open the Terminal (type
Terminalinto Spotlight search). -
Run the following command:
xcode-select --install -
In the window that appears, click
Installand accept the license agreement. The download may take a few minutes.
If you have already installed Xcode, the compiler is already available. The terminal then reports that the Command Line Tools are already installed.
On macOS, the g++ command does not run the GCC compiler but Apple's compiler Clang. Clang is compatible with GCC: it understands the same commands and options (e.g. g++ --std=c++20 main.cpp) and compiles standard C++ the same way. This makes no difference for the bridge course and the lecture, so you can use g++ everywhere.
2. Checking the compiler
Enter the following command in the terminal:
g++ --version
An output should appear that contains Apple clang version and a version number. If an error message appears instead, or you are asked again to install the Command Line Tools, the installation was not completed. In that case, repeat step 1.
3. Installing Visual Studio Code
Download and install Visual Studio Code using the installer for Mac.
When you work with Visual Studio Code for the first time, pop-ups may appear asking you to grant Visual Studio Code access to files. These prompts are normal and must be confirmed.
Setting up Visual Studio Code is described in the main guide.
FAQ and problems
Below you will find problems that frequently occur on macOS. If you cannot find a solution to your problem here, please also check the general FAQ.
Compiling with BitmapPlusPlus.hpp does not work
Problem: The first compiler error is 'constexpr' does not name a type.
Solution: g++ uses an old C++ standard. You therefore need to specify the standard when compiling: g++ --std=c++20 main.cpp. You can also add this option to .vscode/tasks.json; this has to be done again for every new program. On older systems, the C++ standard from 2011 is also sufficient for BitmapPlusPlus.hpp, i.e. g++ --std=c++11 main.cpp.
Program cannot read input
Problem: On macOS, a program started with the default configuration cannot read input from the command line.
Solution: Two files control building and running C++ files in Visual Studio Code. They are located in the project folder under .vscode.
The file tasks.json controls the compilation process. The task defined here is required for running the program.
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${fileDirname}/*.cpp",
"-o",
"${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
The file launch.json controls how the program is run:
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and run active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask": "C/C++: g++ build active file"
}
]
}
The important thing here is that the attribute externalConsole has the value true, so that the program is opened in an external console. This external console then also allows input and output via the terminal.
On macOS, the file launch.json is often not created automatically in the .vscode folder. To set externalConsole to true, you therefore have to create this file first. To do so, click on the gear icon to the right of the Run/Debug button you use to start your program and – as when running the program for the first time – select the option with g++ build and debug active file (or clang++ build and debug active file) in its name. Afterwards, there is a launch.json file in your .vscode folder that you can edit.