-
Notifications
You must be signed in to change notification settings - Fork 1
Build on any OS with Clang and VS Code
Clang is a compiler that runs on and generates code for Windows, Linux, and macOS. Clang is a drop-in replacement for each platform "native" compiler (Microsoft Visual C++ on Windows, GCC on Linux, on Apple devices, it is the native compiler) because it provides a compatible command-line interface and emits compatible code. For example, you can mix Windows libraries built with MSVC and Clang and use the linker of either toolchain to produce the final binary. Using the same compiler on every platform is convenient because you get the same tools (sanitizers, profilers), error messages, quality of generated code, bugs, etc.
VS Code is a cross-platform editor developed by Microsoft. VS Code is heavily customizable, depending on official or community-developed extensions for most of its functionality. You get the extensibility of something like Vim together with a graphical interface that comes close to full-blown IDEs. Language support in VS Code is generally implemented using language servers that provide features like code completion, go to definition, renaming, etc. to the editor. For C/C++, a very popular language server is clangd, which of course integrates seamlessly with Clang.
- A Clang/LLVM toolchain: Open a PowerShell and enter
winget install "LLVM"
- Visual Studio Build Tools (Download)
- Ninja: Open a PowerShell and enter
winget install "Ninja-build.Ninja"
- VS Code (Download)
- Xcode (Apple's fork of Clang called Apple Clang comes with Xcode)
- VS Code (Download)
- A Clang/LLVM toolchain (Guide for Ubuntu)
- VS Code (Download)
- Clone the Git repository into a directory of your choice.
- Open the project in VS Code:
- On macOS and Linux: Start VS Code, go to
File > Open Folder...
, and select the SLProject root directory - On Windows: Open a Developer PowerShell so Clang can find your MSVC toolchain and enter
code <Your SLProject root directory>
- On macOS and Linux: Start VS Code, go to
- Install and enable the following extensions:
- Open the command palette (
Ctrl + Shift + P
) and enterCMake: Scan for Kits
- In the command palette, enter
CMake: Select a Kit
and select the Clang/LLVM toolchain:- On Windows, select
Clang-cl <version> x86_64-pc-windows-msvc
- Make sure you select
Clang-cl
and notClang
! - Example:
Clang-cl 18.1.6 x86_64-pc-windows-msvc
- Make sure you select
- On macOS, select
Clang <version> <architecture>-apple-darwin<version>
- Example:
Clang 15.0.0 arm64-apple-darwin-23.4.0
- Example:
- On Linux, select
Clang <version> <architecture>-unknown-linux-gnu
- Example:
Clang 17.0.0 x86_64-unknown-linux-gnu
- Example:
- On Windows, select
- Open your VS Code settings, switch to
Workspace
, and go toExtensions > clangd
- Add the following clangd argument (
Clangd: Arguments
):--compile-commands-dir=build
- Run the command
CMake: Configure
- Restart clangd by opening the command palette and entering
clangd: Restart language server
- Open the command palette and enter
CMake: Set Debug Target
- Select
app-demo
to run the SLProject demo application - Run the command
CMake: Debug
to build and launch the application
Most CMake configuration options are also available in the CMake view. To open the CMake view, click on the CMake icon in the Activity Bar on the left-hand side of the VS Code window. Here, you can configure:
- The kit (toolchain)
- The build type (Debug/Release)
- The target to build
- The target to launch/debug
Additionally, there are buttons in the Status Bar at the bottom to build, debug, and launch the selected target.
- Activate your Emscripten environment
- In a terminal, run:
emcmake cmake -Bbuild-emscripten -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .
- Open your VS Code settings, switch to
Workspace
, and go toExtensions > clangd
- Change the argument to:
--compile-commands-dir=build-emscripten
- clangd requires some additional configuration to work with Emscripten. Run the script
scripts/configure_clangd_emscripten.py
with the path to the build directory:- On Windows:
python .\scripts\configure_clangd_emscripten.py build-emscripten
- On macOS and Linux:
python3 ./scripts/configure_clangd_emscripten.py build-emscripten
- On Windows:
- Restart clangd by opening the command palette and entering
clangd: Restart language server
To revert back to your normal development environment, set the clangd argument in your VS Code settings back to --compile-commands-dir=build
, delete the file .clangd
, and restart the language server.