Search code examples
c++clangclang++llvm-clang

Clang does not generate profraw file when linking manually


I am trying out the profiling functionality of clang using llvm-cov and llvm-profdata. I have everything setup with CMake, but it doesn't generate the default.profraw as expected. I'v tried the steps manually and discovered that clang does not generate the default.profraw file in case I split the steps between generating the object files and compiling the executable.

For example, The following works:

$ clang++ -g -O0 -fprofile-instr-generate -fcoverage-mapping -std=gnu++2a binoperator.cpp main.cpp
$ ./a.out
38
Done...
$ ls -al default.profraw
-rw-rw-r--. 1 marten marten 224 May 13 13:59 default.profraw

The following doesn't work (this is roughly what CMake tries to do):

$ clang++ -g -O0 -fprofile-instr-generate -fcoverage-mapping -std=gnu++2a -o binoperator.cpp.o -c binoperator.cpp
$ clang++ -g -O0 -fprofile-instr-generate -fcoverage-mapping -std=gnu++2a -o main.cpp.o -c main.cpp
$ clang++ -o a.out binoperator.cpp.o main.cpp.o 
$ ./a.out
38
Done...
$ ls -al default.profraw
ls: cannot access 'default.profraw': No such file or directory

Why? What is the difference? How can I make the second case work?

With kind regards,

Marten


Additional info:

  • main.cpp
#include "binoperator.h"

#include <iostream>

int main()
{
    BinOperator bo;

    int result = bo.add(5, 33);

    std::cout << result << std::endl;
    std::cout << "Done..." << std::endl;

    return 0;
}
  • binoperator.h

#ifndef BINOPERATOR_H
#define BINOPERATOR_H

class BinOperator
{
public:
    int add(int a, int b) const;
};

#endif
  • binoperator.cpp
#include "binoperator.h"

int BinOperator::add(int a, int b) const
{
    return (a + b);
}
$ clang --version
clang version 8.0.0 (Fedora 8.0.0-1.fc30)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Solution

  • I've found out that in the second case, the -fprofile-instr-generate -fcoverage-mapping options should also be specified in the linking call to clang++:

    $ clang++ -O0 -fprofile-instr-generate -fcoverage-mapping binoperator.cpp.o main.cpp.o -o a.out
    

    In CMake, this can be done with target_link_options().