Configure Clangd

Rico Ruotong Jia
1 min readJan 7, 2024

--

In 2003, Chris Lattner from University of Illinois, Urbana Champaign started the LLVM (low level virtual machine) project. LLVM is a tool chain and a set of compilers that takes in any programming language, and outputs machine code in any architecture.

Front-end

  • Source code -> lexical analysis, AST -> IR code

Uses a lexer to :

  • break down the code into tokens: keyword, operators, identifiers
  • build an AST of the tokens in the parser
  • generate IR: each node’s class has a method called codegen, that’s for LLVM Intermediate Representation

Middle Layer

  • IR Transformations: applying transformations such as inlining functions, eliminating dead code, simpligying expresions.
  • Analyze what code can be optimized, such as data flow, control flow analyses. Then, carry out these optimizations.

Back End

  • Select instruction set, allocate registers to variables, generate machine code (X86) object code from IR based on architecture

Install clang-format so we can format cpp files

sudo apt-get update
sudo apt-get install clang-format
# overwrite the cpp file
clang-format -i <your_file>.cpp -style=google

Enable clangd for vscode, so goto can be enabled

sudo apt-get install clangd-10
  1. CMake must be at least 3.5
  2. set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

This way, a compile_commands.json will be generated. Place compile_commands.json at the project root folder, and reload vscode window

Do we need to change compile_commands.json ? Not if you have:

  • Changing included files
  • Adding or removing files from build
  • Modifying compilation flags

--

--

Rico Ruotong Jia
Rico Ruotong Jia

Written by Rico Ruotong Jia

A singer, a gym goer, a robot enthusiast, a human being. Northwestern University MSc. Robotics

No responses yet