c++clangbuild-time

'clang -ftime-trace' does not produce a JSON file


Clang supports the -ftime-trace flag since version 9 which allows to analyze compilation times by producing a JSON file that can be read by Google Chrome. Unfortunately, Clang fails to output a JSON file for me, even for the simplest program.

Minimal example: I have a main.cpp file

#include <iostream>

int main(){
    std::cout << "test" << std::endl;
}

Using Clang 13 (on WSL with Ubuntu 20.04) and compiling it with clang++ -ftime-trace main.cpp produces the executable a.out, but no JSON file. What am I doing wrong?


Solution

  • The -ftime-trace flag produces JSON files for each object file and places them next to each object file. It does not profile the linking stage.

    Running clang++ -ftime-trace main.cpp produces a temporary object file in the /tmp/ directory and then runs the linker to form the complete executable a.out in your working directory. Thus, if you look into the /tmp/ directory, you can actually find your JSON file there.

    Simply specify the -c flag, i.e., clang++ -ftime-trace -c main.cpp, to skip the linker and produce an object file main.o along the JSON file main.json in your working directory. You can also provide a different name for these files using the -o flag.