gcov 符号在哪里?

发布于 2024-07-14 02:01:35 字数 1237 浏览 9 评论 0原文

我正在尝试使用 gcov 编译一个简单的应用程序并收到以下链接错误:

gcc AllTests.o CuTestTest.o CuTest.o -o TestTest
AllTests.o: In function `global constructors keyed to 0_RunAllTests':
/home/p7539c/cutest/AllTests.c:26: undefined reference to `__gcov_init'
AllTests.o:(.data+0x44): undefined reference to `__gcov_merge_add'
CuTestTest.o: In function `global constructors keyed to 0_TestCuStringNew':
/home/p7539c/cutest/CuTestTest.c:30: undefined reference to `__gcov_init'
CuTestTest.o:(.data+0x64): undefined reference to `__gcov_merge_add'
CuTest.o: In function `global constructors keyed to 0_CuStrAlloc':
/home/p7539c/cutest/CuTest.c:379: undefined reference to `__gcov_init'
CuTest.o:(.data+0x184): undefined reference to `__gcov_merge_add'
collect2: ld returned 1 exit status
make: *** [TestTest] Error 1

我似乎找不到丢失符号的位置。 gcov 存在于运行 gcc 版本 4.1.2 的机器上 有

什么想法吗? 谢谢。

编辑时:

将 gcov 与包含一个 .c 文件的应用程序一起使用时,一切似乎都工作正常。 当我有多个 .c 文件(因此有多个 .o 文件)时,我遇到上述问题。

编译步骤如下所示:

cc -fprofile-arcs -ftest-coverage -g   -c -o AllTests.o AllTests.c
cc -fprofile-arcs -ftest-coverage -g   -c -o CuTestTest.o CuTestTest.c
cc -fprofile-arcs -ftest-coverage -g   -c -o CuTest.o CuTest.c

I'm trying to compile a simple app with gcov and getting the following link errors:

gcc AllTests.o CuTestTest.o CuTest.o -o TestTest
AllTests.o: In function `global constructors keyed to 0_RunAllTests':
/home/p7539c/cutest/AllTests.c:26: undefined reference to `__gcov_init'
AllTests.o:(.data+0x44): undefined reference to `__gcov_merge_add'
CuTestTest.o: In function `global constructors keyed to 0_TestCuStringNew':
/home/p7539c/cutest/CuTestTest.c:30: undefined reference to `__gcov_init'
CuTestTest.o:(.data+0x64): undefined reference to `__gcov_merge_add'
CuTest.o: In function `global constructors keyed to 0_CuStrAlloc':
/home/p7539c/cutest/CuTest.c:379: undefined reference to `__gcov_init'
CuTest.o:(.data+0x184): undefined reference to `__gcov_merge_add'
collect2: ld returned 1 exit status
make: *** [TestTest] Error 1

I can't seem to find the location of the missing symbols. gcov is present on the machine running gcc version 4.1.2

Any ideas? Thanks.

On Edit:

Everything seems to work fine when using gcov with an application that consists of one .c file. When I have multiple .c files (hence multiple .o files) I have the above problem.

The compile steps look like the following:

cc -fprofile-arcs -ftest-coverage -g   -c -o AllTests.o AllTests.c
cc -fprofile-arcs -ftest-coverage -g   -c -o CuTestTest.o CuTestTest.c
cc -fprofile-arcs -ftest-coverage -g   -c -o CuTest.o CuTest.c

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

笙痞 2024-07-21 02:01:35

我刚刚花费了大量的时间来调试一个非常相似的错误。 这是我学到的:

  • 编译时必须传递 -fprofile-arcs -ftest-coverage
  • 链接时必须传递-fprofile-arcs
  • 链接时您仍然会遇到奇怪的链接器错误。 它们看起来像这样:

    libname.a(objfile.o):(.ctors+0x0): undefined reference to 'global constructors keyed to long_name_of_file_and_function'

这意味着 gconv 与您的其中一个有问题编译器生成的构造函数(在我的例子中,是复制构造函数)。 检查错误消息中提到的函数,查看它复制构造的对象类型,并查看这些类中是否有任何一个没有复制构造函数。 添加一个,错误就会消失。

编辑:是否优化也会影响这一点。 如果您遇到问题,请尝试打开/关闭优化。

I just spent an incredible amount of time debugging a very similar error. Here's what I learned:

  • You have to pass -fprofile-arcs -ftest-coverage when compiling.
  • You have to pass -fprofile-arcs when linking.
  • You can still get weird linker errors when linking. They'll look like this:

    libname.a(objfile.o):(.ctors+0x0): undefined reference to 'global constructors keyed to long_name_of_file_and_function'

This means that gconv's having problem with one of your compiler-generated constructors (in my case, a copy-constructor). Check the function mentioned in the error message, see what kinds of objects it copy-constructs, and see if any of those classes doesn't have a copy constructor. Add one, and the error will go away.

Edit: Whether or not you optimize can also affect this. Try turning on / switching off optimizations if you're having problems with it.

⊕婉儿 2024-07-21 02:01:35

您要查找的标志是 -lgcov 链接时< /a>. 即,更改

gcc AllTests.o CuTestTest.o CuTest.o -o TestTest

为:

gcc -lgcov AllTests.o CuTestTest.o CuTest.o -o TestTest

The flag you're looking for is -lgcov when linking. That is, change:

gcc AllTests.o CuTestTest.o CuTest.o -o TestTest

to

gcc -lgcov AllTests.o CuTestTest.o CuTest.o -o TestTest
夏天碎花小短裙 2024-07-21 02:01:35

我发现,按照此处的建议,在构建时将 -lgcov 添加到构建行包含使用 -fprofile-arcs -ftest-coverage 构建的 .o 的共享库为我解决了这个问题。 当然,还可以使用 -lgcov 链接可执行文件。 像这样构建共享库:

g++    -shared -o libMyLib.so src_a.o src_b.o src_c.o -lgcov

和像这样的可执行文件:

g++ -o myExec myMain.o -lMyLib -lgcov

将 -lgov 添加到共享库(不仅仅是 exe)的构建中,为我解决了这个额外的错误:

hidden symbol `__gcov_merge_add' in /usr/lib/gcc/x86_64-redhat-linux/4.1.2/libgcov.a(_gcov_merge_add.o) is referenced by DSO
/usr/bin/ld: final link failed: Nonrepresentable section on output

请注意 -lgcov 必须是最后一个链接库。

I found, as suggested here, that adding -lgcov to the build line when building a shared library that contains .o's built with -fprofile-arcs -ftest-coverage solved this for me. And of course linking the executable with -lgcov. Built the shared library like so:

g++    -shared -o libMyLib.so src_a.o src_b.o src_c.o -lgcov

And the executable like so:

g++ -o myExec myMain.o -lMyLib -lgcov

Adding the -lgov to the build of the shared library (not just the exe), solved this additional error for me:

hidden symbol `__gcov_merge_add' in /usr/lib/gcc/x86_64-redhat-linux/4.1.2/libgcov.a(_gcov_merge_add.o) is referenced by DSO
/usr/bin/ld: final link failed: Nonrepresentable section on output

Please note that -lgcov must be the last linked library.

旧话新听 2024-07-21 02:01:35

编译和链接时,您应该能够在命令行上仅指定 --coverage

根据 man gcc

该选项是 -fprofile-arcs -ftest-coverage 的同义词(当
编译)和 -lgcov (链接时)。

You should be able to specify only --coverage on the command line when compiling and linking.

According to man gcc:

The option is a synonym for -fprofile-arcs -ftest-coverage (when
compiling) and -lgcov (when linking).

只为一人 2024-07-21 02:01:35

我使用 gcc -ftest-coverage -fprofile-arcs test.c 尝试了一个简单的测试文件,并且没有出现您所描述的问题。

我怀疑如果在链接时存在 -ftest-coverage 标志,gcc 会引入 gcov 库。 尝试在 gcc 命令行上传递该标志。

I tried a simple test file with gcc -ftest-coverage -fprofile-arcs test.c and had no problems like you describe.

I suspect that gcc brings in the gcov library if the -ftest-coverage flag is there when it is linking. Try passing that flag on your gcc command line.

烟酉 2024-07-21 02:01:35

显然,这个错误消息是在与非 gcc 链接器链接时产生的。 我们在与 ifort 链接时看到此错误(因为我们的代码同时包含 Fortran 和 C 模块)。 切换到与 gcc 链接就成功了。

Perhaps obviously, this exact error message is produced when linking with a non-gcc linker. We seeing this error when linking with ifort (because our code includes both Fortran and C modules). Switching to linking with gcc did the trick.

夜深人未静 2024-07-21 02:01:35

伟大的马克斯·利伯特,
基本上在使用 autoconf 的情况下添加 _LDADD = -lgcov ...

这将解决问题。

Great Max Lybbert,
Basically in case of autoconf usage add _LDADD = -lgcov ...

This will solve the issue.

深海蓝天 2024-07-21 02:01:35

所以我将 -shared 添加到 CFLAGS,现在它似乎可以处理多个文件。 当然,它在一个奇怪的地方取芯,所以我还不知道那是什么。

So I added -shared to the CFLAGS, and now it seems to work with multiple files. Of course, it's coring in a strange place, so I don't know what that's about yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文