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.

References:

[1] Bazel documentation: http://www.bazel.io/versions/master/docs/bazel-user-manual.html

[2]GProf FAQ: http://www.delorie.com/djgpp/v2faq/faq13_5.html

One clap, two clap, three clap, forty?

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