How to add GProf flag in Bazel build

Lately I’ve decided to profile my C++ program (built with Bazel) with GProf. However, not until some googling did I realize that I need to add the “-pg” flag the following way to make the program generate the file gmon.out file:

bazel build — copt=”-pg” — cxxopt=”-pg” — linkopt=“-pg” {your target}

That’s because according to [2], the -pg flag needs to be passed at both complication and linking time, and Bazel does not pass options specified by — copt/ — cxxopt to linking. So one would need to pass -pg through the linker option — linkopt.


[1] Bazel documentation:

[2]GProf FAQ:

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.