C++ Insights is a command line tool that shows how high-level C++ code is transformed by the compiler into a more detailed, low-level form by revealing things like implicit constructors, type deductions, range-based loops, lambda expansions, and other. This tutorial explains how to install C++ Insights on Ubuntu 24.04.
Prepare environment
C++ Insights relies on standard headers like stddef.h
, which are provided by compilers such as GCC or Clang. Make sure a C++ compiler is installed, as it supplies the internal headers required for parsing.
sudo apt install -y g++
Install C++ Insights
Download the latest C++ Insights archive from GitHub repository:
wget -qO insights.tar.gz https://github.com/andreasfertig/cppinsights/releases/latest/download/insights-ubuntu-amd64.tar.gz
Extract the binary to /usr/local/bin
so it's available system-wide:
sudo tar xf insights.tar.gz -C /usr/local/bin insights
Verify that C++ Insights was installed successfully by viewing the installed version:
insights --version
Remove the downloaded archive:
rm -rf insights.tar.gz
Testing C++ Insights
Assume the following C++ code is saved in a file called main.cpp
:
#include <iostream>
#include <vector>
int main() {
const std::vector values = {1, 2, 3};
for (const int val : values) {
std::cout << val << std::endl;
}
return 0;
}
Run C++ Insights on the source file with the following command:
insights main.cpp -- -std=c++17 -I/usr/lib/gcc/x86_64-linux-gnu/13/include
--
- separates C++ Insights options from compiler options.-std=c++17
- specifies that the code should be interpreted using the C++17 standard.-I/usr/lib/gcc/x86_64-linux-gnu/13/include
- adds the path to internal GCC headers (likestddef.h
), which are required for proper parsing of standard library components. You can specify multiple-I
options to add additional directories, making sure all necessary headers are located during the analysis.
Output:
#include <iostream>
#include <vector>
int main()
{
std::vector<int, std::allocator<int> > v = std::vector<int, std::allocator<int> >{std::initializer_list<int>{1, 2, 3}, std::allocator<int>()};
{
std::vector<int, std::allocator<int> > & __range1 = v;
__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > __begin1 = __range1.begin();
__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > __end1 = __range1.end();
for(; __gnu_cxx::operator!=(__begin1, __end1); __begin1.operator++()) {
int x = __begin1.operator*();
std::operator<<(std::cout.operator<<(x), "\n");
}
}
return 0;
}
It reveals how the compiler transforms the code behind the scenes.
Uninstall C++ Insights
To uninstall C++ Insights, just remove the related file:
sudo rm -rf /usr/local/bin/insights
Leave a Comment
Cancel reply