g++ with -fprofile-dir flag has a bug (backslash instead of forward-slash issue)

John Selbie jselbie@gmail.com
Sun Apr 12 07:27:49 GMT 2020


TLDR: With gcc/g++ 9.2.0 and 9.30 on Cygwin, when you use
-fprofile-generate and -fprofile-dir together, the target path for the
.gcda file is corrupted with a backslash instead of having a forward slash
used.

Here's a sample run where profile guided optimization is getting enabled
for a simple little C++ program:

    jselbie@IRONMAIDEN ~/bench
    $ g++ bench.cpp -o bench -fprofile-generate
-fprofile-dir=/home/jselbie/bench/profile -O3

    jselbie@IRONMAIDEN ~/bench
    $ ./bench.exe

profiling:/home/jselbie/bench/profile/#home#jselbie#bench\bench.gcda:Skip

That "profiling...Skip" line is an error indicating that the profiling
library can't save the gcda file. If you look closely,there's backslash
between the encoded path and the filename (bench.gcda).

Doing a strings search on the binary reveals the same thing:

    jselbie@IRONMAIDEN ~/bench
    $ strings bench.exe | grep jselbie
    /home/jselbie/bench/profile/#home#jselbie#bench\bench.gcda

If I switch back to Linux and do the same steps, it not only works, but you
can see there is no backslash embedded into the binary either

    jselbie@IRONMAIDEN:~/bench$ g++ bench.cpp -o bench -fprofile-generate
-fprofile-dir=/home/jselbie/bench/profile -O3

    jselbie@IRONMAIDEN:~/bench$ ./bench

    jselbie@IRONMAIDEN:~/bench$ ls profile/
    bench.gcda

    jselbie@IRONMAIDEN:~/bench$ strings bench | grep jselbie
    /home/jselbie/bench/profile/bench.gcda


The workaround is to just skip the -fprofile-dir flag altogether.

Seems like there is a bug in Cygwin's build of gcc/g++ that puts the wrong
path separator into the executable

Can this be fixed?  Or a place where I can file a bug?

jrs


More information about the Cygwin mailing list