zig icon indicating copy to clipboard operation
zig copied to clipboard

zig cc/c++: lld-link errors when using `--coverage`

Open lachlanm-git opened this issue 1 year ago • 3 comments

Zig Version

0.11.0 edit: also tried "0.12.0-dev.1297+a9e66ed73" (Windows x86_64) with the zig cc repo steps - same issue

Steps to Reproduce and Observed Behavior

Observed Behaviour lld-linker errors.

LLD Link... lld-link: error: undefined symbol: llvm_gcda_start_file >>> referenced by \zig\o...\main.obj:(__llvm_gcov_writeout)

lld-link: error: undefined symbol: llvm_gcda_emit_function >>> referenced by \zig\o...\main.obj:(__llvm_gcov_writeout)

lld-link: error: undefined symbol: llvm_gcda_emit_arcs >>> referenced by \zig\o...\main.obj:(__llvm_gcov_writeout)

lld-link: error: undefined symbol: llvm_gcda_summary_info >>> referenced by \zig\o...\main.obj:(__llvm_gcov_writeout)

lld-link: error: undefined symbol: llvm_gcda_end_file >>> referenced by \zig\o...\main.obj:(__llvm_gcov_writeout)

lld-link: error: undefined symbol: llvm_gcov_init >>> referenced by \zig\o...\main.obj:(__llvm_gcov_init)

Steps to reproduce run zig cc main.c --coverage

main.c

int main(void) {
    return 0;
}

I believe --coverage may be equivalent to -fprofile-arcs -ftest-coverage.

Expected Behavior

no linker errors

lachlanm-git avatar Oct 27 '23 05:10 lachlanm-git

For background, I am playing with CppUTest and building with Zig. (WiP here: https://github.com/lachlanm-git/cpputest-zig-0.11.0 with build.zig)

I was looking at adding coverage reporting but ran into this issue. Interestingly (I think), when I run zig build on my repo, I see 'zig-cache/tmp/*.gcno` files generated but get the link errors.

lachlanm-git avatar Oct 27 '23 05:10 lachlanm-git

Additionally, it would be useful to add linker options in build.zig.

We are missing something like exe.linkAdditionalOptions("--coverage");

When trying to generate coverage data the linker fails because there are symbols missing, which are included when linking with the option --coverage.

In other words, we need to both compile and link with --coverage.

exe.addCSourceFiles(&[_][]const u8{
    "file.c"
}, &[_][]const u8{
    "--coverage"
});
exe.linkAdditionalOptions("--coverage");

djboni avatar Nov 30 '23 23:11 djboni

Additionally, it would be useful to add linker options in build.zig.

We are missing something like exe.linkAdditionalOptions("--coverage");

When trying to generate coverage data the linker fails because there are symbols missing, which are included when linking with the option --coverage.

In other words, we need to both compile and link with --coverage.

exe.addCSourceFiles(&[_][]const u8{
    "file.c"
}, &[_][]const u8{
    "--coverage"
});
exe.linkAdditionalOptions("--coverage");

I believe you can do "-Wl,<opt1,opt2,..>" to provide flags to the linker. Possibly, a build API would be nice.

Regarding the initial issue, I've tried "-Wl,--coverage", with various permutations with no success. It is interesting that I don't observe an error/warning using build.zig (v0.11.0) but if I do the following, I get a linker error. Maybe a difference between zig cc entry and normal zig.

zig cc .\main.c --coverage "-Wl,--coverage" error: unsupported linker arg: --coverage

lachlanm-git avatar Dec 23 '23 00:12 lachlanm-git

I've done some more digging and I don't think this is an issue with Zig. I think this is a Clang issue that requires linking against clang_rt.profile-<ARCH>.lib[1,2].

Perhaps this ticket should be closed if that does indeed seem to be the case...

[1] - https://marco-c.github.io/2018/01/09/code-coverage-with-clang-on-windows.html [2] - https://bugs.llvm.org/show_bug.cgi?id=40877

lachlanm-git avatar Dec 28 '23 09:12 lachlanm-git

Ok, I've built a clang_rt.profile lib and linked against it. Confirmed, this removes the linker error and I can get the .gcda files.

I'll close this issue as I think it is out of scope of the Zig project to additionally build and package this runtime

lachlanm-git avatar Dec 29 '23 10:12 lachlanm-git