Saturday 8 January 2022

Installing CLANG on a Raspberry Pi

The following uses an 8GB Raspberry Pi 4.


Update and upgrade your installation

sudo apt update
sudo apt upgrade

Install CLANG

apt-get install clang-format clang-tidy clang-tools clang libc++-dev libc++1 libc++abi-dev libc++abi1 libclang-dev libclang1 liblldb-dev libllvm-ocaml-dev libomp-dev libomp5 lld lldb llvm-dev llvm-runtime llvm python-clang

Note: LLVM suggested apt call includes clangd, however it does not appear on the “current” Raspbian distribution (though I noted my version of CLANG is version 7 so later distributions might contain it).

Check it is in place.

clang++ --version


clang version 7.0.1-8+rpi3+deb10u2 (tags/RELEASE_701/final)
Target: armv6k-unknown-linux-gnueabihf
Thread model: posix
InstalledDir: /usr/bin

Hello World

Create a C++ source file
#include <iostream>
int main(){
std::cout<<"Hello world\n";
return 0;
}

Save the file to a specific folder (I chose ~/Documents/Cplusplus/helloworld )

Change the working folder to its location
cd ~/Documents/Cplusplus/helloworld

Compile:
clang++ -std=c++17 -Wall -pedantic helloworld .cpp -o helloworld
Run
./helloworld


Install Visual Studio Code (Other IDEs are available)

Raspberry Pi has Visual Studio Code in its repository
sudo apt install code

Open Visual Studio Code and install the install the C/C++ extension (C/C++ from Microsoft).

Configuring Visual Studio Code

Other descriptions of configuring Visual Studio include additional editing of files, but the Raspberry Pi repository seems to cover a lot of it.
Open the folder in Visual Studio.
Open the CPP file.

Click on Run and select Start Debugging.
From the list of environments select C++(GDB/LLDB)
Select g++.exe - Build and debug active file.
This creates a new folder named .vscode in the current folder.
Within that folder are two JSON files: tasks.json and launch.json
The file tasks.json contains the arguments and the location of the clang compiler front end.


{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}



Use the above file as a reference.
The launch.json file contains information about what happens when Visual Studio Code starts to run the compilation.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "clang++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"targetArchitecture": "ARM64",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: clang++ build active file",
"miDebuggerPath": "/usr/bin/lldb-mi"
}
]
}



The key element that is not in the default configurations elements is the targetArchitecture:
"targetArchitecture": "ARM64"
Add this to the file as shown above.
This removes the “Warning: Debuggee TargetArchitecture not detected, assuming x86_64.” warning message.


References

https://apt.llvm.org/
https://code.visualstudio.com/docs/setup/raspberry-pi
https://www.raspberrypi.com/news/visual-studio-code-comes-to-raspberry-pi/
https://solarianprogrammer.com/2018/04/22/raspberry-pi-raspbian-install-clang-compile-cpp-17-programs/
https://solarianprogrammer.com/2021/06/11/install-clang-windows-msys2-mingw-w64/
https://johnnn.tech/q/vscode-lldb-on-macos-error-when-starting-debugging-session/
https://code.visualstudio.com/updates/v1_50#_linux-arm-builds