Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用"coverage"模式,链接bin文件时出现错误 hidden symbol `__gcov_init' #2708

Closed
fengqianliuli opened this issue Aug 22, 2022 · 2 comments
Labels
Milestone

Comments

@fengqianliuli
Copy link

Xmake 版本

2.6.9

操作系统版本和架构

Linux version 5.4.0-121-generic (buildd@lcy02-amd64-013) (gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)) #137-Ubuntu SMP Wed Jun 15 13:33:07 UTC 2022

描述问题

  • 首先我的项目使用xmake是正常编译的
  1. 添加coverage模式,在最外层xmake.lua添加 add_rules("mode.coverage")
  2. 编译,xmake f -m coverage;xmake
  3. xmake执行时报错,另外虽然bin文件链接出错,但是build/.objs目录下仍然会生成 .gcno文件
    [ 90%]: linking.coverage persistency_test
    error: /usr/bin/ld: build/linux/x86_64/coverage/persistency_test: hidden symbol `__gcov_init' in /usr/lib/gcc/x86_64-linux-gnu/9/libgcov.a(_gcov.o) is referenced by DSO
    /usr/bin/ld: final link failed: bad value
    collect2: error: ld returned 1 exit status

期待的结果

正常编译生成unittest可执行程序

工程配置

-- define project
set_project("persistency")

-- set common config
add_rules("mode.asan", "mode.coverage")
set_languages("c99", "cxx17")
add_includedirs("./", "$(projectdir)/../common/core/include")

if is_mode("debug") then
  set_symbols("debug")
  set_optimize("none")
  add_cxflags("-Wall", "-Werror", "-Wunused-parameter")
end

if is_mode("release") then
  set_symbols("debug")
  set_optimize("faster")
  set_strip("all")
  add_cxflags("-DNDEBUG", "-Wall", "-Werror", "-Wunused-parameter")
end

if is_mode("asan") then
  add_ldflags("-fsanitize=address", {force = true})
end

-- includes sub-projects
includes("src", "test")



### 附加信息和错误日志

➜  per git:(develop) ✗ xmake -vD          
checking for gcc ... /usr/bin/gcc
checking for the c++ compiler (cxx) ... gcc
checking for the c++ compiler (cxx) ... gcc
checking for the c++ compiler (cxx) ... gcc
checking for the c++ compiler (cxx) ... gcc
checking for the c++ compiler (cxx) ... gcc
checking for the c++ compiler (cxx) ... gcc
checking for the c++ compiler (cxx) ... gcc
checking for the c++ compiler (cxx) ... gcc
checking for the c++ compiler (cxx) ... gcc
checking for the c++ compiler (cxx) ... gcc
checking for the c++ compiler (cxx) ... gcc
checking for the c++ compiler (cxx) ... gcc
checking for /usr/bin/gcc ... ok
checking for flags (-fPIC) ... ok
> gcc "-fPIC"
checking for flags (-fvisibility-inlines-hidden) ... ok
> gcc "-fvisibility-inlines-hidden"
checking for flags (-O0) ... ok
> gcc "-O0"
checking for flags (--coverage) ... ok
> gcc "--coverage"
[ 20%]: ccache compiling.coverage test/file_storage_test.cpp
/usr/bin/gcc -c -m64 -g -Wall -O0 -std=c++17 -I. -I/home/haotian.shi/boyan/per/../common/core/include -I/home/haotian.shi/boyan/out/tmprootfs/usr/include -Itest -I/home/haotian.shi/boyan/per/../third_party --coverage -o build/.objs/persistency_test/linux/x86_64/coverage/test/file_storage_test.cpp.o test/file_storage_test.cpp
[ 20%]: ccache compiling.coverage src/file_storage/read_write_accessor_with_filelock.cpp
/usr/bin/gcc -c -m64 -fPIC -g -O0 -std=c++17 -I. -I/home/haotian.shi/boyan/per/../common/core/include -Isrc -I/home/haotian.shi/boyan/per/../third_party -I/home/haotian.shi/boyan/per/../third_party/boost --coverage -o build/.objs/persistency/linux/x86_64/coverage/src/file_storage/read_write_accessor_with_filelock.cpp.o src/file_storage/read_write_accessor_with_filelock.cpp
[ 20%]: ccache compiling.coverage src/file_storage/read_accessor_with_filelock.cpp
/usr/bin/gcc -c -m64 -fPIC -g -O0 -std=c++17 -I. -I/home/haotian.shi/boyan/per/../common/core/include -Isrc -I/home/haotian.shi/boyan/per/../third_party -I/home/haotian.shi/boyan/per/../third_party/boost --coverage -o build/.objs/persistency/linux/x86_64/coverage/src/file_storage/read_accessor_with_filelock.cpp.o src/file_storage/read_accessor_with_filelock.cpp
[ 20%]: ccache compiling.coverage src/key_value_storage/kvs_json_impl.cpp
/usr/bin/gcc -c -m64 -fPIC -g -O0 -std=c++17 -I. -I/home/haotian.shi/boyan/per/../common/core/include -Isrc -I/home/haotian.shi/boyan/per/../third_party -I/home/haotian.shi/boyan/per/../third_party/boost --coverage -o build/.objs/persistency/linux/x86_64/coverage/src/key_value_storage/kvs_json_impl.cpp.o src/key_value_storage/kvs_json_impl.cpp
[ 20%]: ccache compiling.coverage src/file_storage/read_accessor_impl.cpp
/usr/bin/gcc -c -m64 -fPIC -g -O0 -std=c++17 -I. -I/home/haotian.shi/boyan/per/../common/core/include -Isrc -I/home/haotian.shi/boyan/per/../third_party -I/home/haotian.shi/boyan/per/../third_party/boost --coverage -o build/.objs/persistency/linux/x86_64/coverage/src/file_storage/read_accessor_impl.cpp.o src/file_storage/read_accessor_impl.cpp
[ 20%]: ccache compiling.coverage src/file_storage/file_storage_impl.cpp
/usr/bin/gcc -c -m64 -fPIC -g -O0 -std=c++17 -I. -I/home/haotian.shi/boyan/per/../common/core/include -Isrc -I/home/haotian.shi/boyan/per/../third_party -I/home/haotian.shi/boyan/per/../third_party/boost --coverage -o build/.objs/persistency/linux/x86_64/coverage/src/file_storage/file_storage_impl.cpp.o src/file_storage/file_storage_impl.cpp
[ 20%]: ccache compiling.coverage test/json_key_value_test.cpp
/usr/bin/gcc -c -m64 -g -Wall -O0 -std=c++17 -I. -I/home/haotian.shi/boyan/per/../common/core/include -I/home/haotian.shi/boyan/out/tmprootfs/usr/include -Itest -I/home/haotian.shi/boyan/per/../third_party --coverage -o build/.objs/persistency_test/linux/x86_64/coverage/test/json_key_value_test.cpp.o test/json_key_value_test.cpp
[ 20%]: ccache compiling.coverage test/main.cpp
/usr/bin/gcc -c -m64 -g -Wall -O0 -std=c++17 -I. -I/home/haotian.shi/boyan/per/../common/core/include -I/home/haotian.shi/boyan/out/tmprootfs/usr/include -Itest -I/home/haotian.shi/boyan/per/../third_party --coverage -o build/.objs/persistency_test/linux/x86_64/coverage/test/main.cpp.o test/main.cpp
[ 20%]: ccache compiling.coverage test/sqlite_key_value_test.cpp
/usr/bin/gcc -c -m64 -g -Wall -O0 -std=c++17 -I. -I/home/haotian.shi/boyan/per/../common/core/include -I/home/haotian.shi/boyan/out/tmprootfs/usr/include -Itest -I/home/haotian.shi/boyan/per/../third_party --coverage -o build/.objs/persistency_test/linux/x86_64/coverage/test/sqlite_key_value_test.cpp.o test/sqlite_key_value_test.cpp
[ 20%]: ccache compiling.coverage src/key_value_storage/key_value_storage.cpp
/usr/bin/gcc -c -m64 -fPIC -g -O0 -std=c++17 -I. -I/home/haotian.shi/boyan/per/../common/core/include -Isrc -I/home/haotian.shi/boyan/per/../third_party -I/home/haotian.shi/boyan/per/../third_party/boost --coverage -o build/.objs/persistency/linux/x86_64/coverage/src/key_value_storage/key_value_storage.cpp.o src/key_value_storage/key_value_storage.cpp
[ 20%]: ccache compiling.coverage src/key_value_storage/kvs_sql_impl.cpp
/usr/bin/gcc -c -m64 -fPIC -g -O0 -std=c++17 -I. -I/home/haotian.shi/boyan/per/../common/core/include -Isrc -I/home/haotian.shi/boyan/per/../third_party -I/home/haotian.shi/boyan/per/../third_party/boost --coverage -o build/.objs/persistency/linux/x86_64/coverage/src/key_value_storage/kvs_sql_impl.cpp.o src/key_value_storage/kvs_sql_impl.cpp
[ 20%]: ccache compiling.coverage src/file_storage/read_write_accessor_impl.cpp
/usr/bin/gcc -c -m64 -fPIC -g -O0 -std=c++17 -I. -I/home/haotian.shi/boyan/per/../common/core/include -Isrc -I/home/haotian.shi/boyan/per/../third_party -I/home/haotian.shi/boyan/per/../third_party/boost --coverage -o build/.objs/persistency/linux/x86_64/coverage/src/file_storage/read_write_accessor_impl.cpp.o src/file_storage/read_write_accessor_impl.cpp
checking for flags (-MMD -MF) ... ok
> gcc "-MMD" "-MF" "/dev/null"
checking for flags (-fdiagnostics-color=always) ... ok
> gcc "-fdiagnostics-color=always"
checking for g++ ... /usr/bin/g++
checking for the shared library linker (sh) ... g++
checking for /usr/bin/g++ ... ok
checking for flags (-fPIC) ... ok
> g++ "-fPIC" "-shared"
[ 80%]: linking.coverage libpersistency.so
/usr/bin/g++ -o build/linux/x86_64/coverage/libpersistency.so build/.objs/persistency/linux/x86_64/coverage/src/file_storage/read_write_accessor_impl.cpp.o build/.objs/persistency/linux/x86_64/coverage/src/file_storage/read_write_accessor_with_filelock.cpp.o build/.objs/persistency/linux/x86_64/coverage/src/file_storage/file_storage_impl.cpp.o build/.objs/persistency/linux/x86_64/coverage/src/file_storage/read_accessor_with_filelock.cpp.o build/.objs/persistency/linux/x86_64/coverage/src/file_storage/read_accessor_impl.cpp.o build/.objs/persistency/linux/x86_64/coverage/src/key_value_storage/key_value_storage.cpp.o build/.objs/persistency/linux/x86_64/coverage/src/key_value_storage/kvs_sql_impl.cpp.o build/.objs/persistency/linux/x86_64/coverage/src/key_value_storage/kvs_json_impl.cpp.o -shared -fPIC -m64 -L/home/haotian.shi/boyan/out/tmprootfs/usr/lib -lsqlite -ldl
checking for g++ ... /usr/bin/g++
checking for the linker (ld) ... g++
checking for flags (-fPIC) ... ok
> g++ "-fPIC"
checking for flags (-Wl,-rpath=@loader_path) ... ok
> g++ "-Wl,-rpath=@loader_path" "-m64" "-m64"
checking for flags (--coverage) ... ok
> g++ "--coverage" "-m64" "-m64"
[ 90%]: linking.coverage persistency_test
/usr/bin/g++ -o build/linux/x86_64/coverage/persistency_test build/.objs/persistency_test/linux/x86_64/coverage/test/file_storage_test.cpp.o build/.objs/persistency_test/linux/x86_64/coverage/test/json_key_value_test.cpp.o build/.objs/persistency_test/linux/x86_64/coverage/test/sqlite_key_value_test.cpp.o build/.objs/persistency_test/linux/x86_64/coverage/test/main.cpp.o -m64 -L/home/haotian.shi/boyan/out/tmprootfs/usr/lib -Lbuild/linux/x86_64/coverage -Wl,-rpath=$ORIGIN -lgtest -lsqlite -lpersistency -lpthread -ldl -fprofile-arcs -ftest-coverage --coverage
error: @programdir/modules/private/async/runjobs.lua:256: @programdir/actions/build/kinds/binary.lua:74: @programdir/core/sandbox/modules/os.lua:257: /usr/bin/ld: build/linux/x86_64/coverage/persistency_test: hidden symbol `__gcov_init' in /usr/lib/gcc/x86_64-linux-gnu/9/libgcov.a(_gcov.o) is referenced by DSO
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status

stack traceback:
    [C]: in function 'error'
    [@programdir/core/base/os.lua:872]:
    [@programdir/core/sandbox/modules/os.lua:257]: in function 'runv'
    [@programdir/modules/core/tools/gcc.lua:374]:
    [C]: in function 'xpcall'
    [@programdir/core/base/utils.lua:280]:
    [@programdir/actions/build/kinds/binary.lua:74]: in function 'callback'
    [@programdir/modules/core/project/depend.lua:189]: in function 'on_changed'
    [@programdir/actions/build/kinds/binary.lua:55]: in function '_do_link_target'
    [@programdir/actions/build/kinds/binary.lua:102]:
    [@programdir/actions/build/kinds/binary.lua:129]: in function '_link_target'
    [@programdir/actions/build/kinds/binary.lua:157]: in function 'jobfunc'
    [@programdir/modules/private/async/runjobs.lua:232]:
    [C]: in function 'xpcall'
    [@programdir/core/base/utils.lua:280]: in function 'trycall'
    [@programdir/core/sandbox/modules/try.lua:121]: in function 'try'
    [@programdir/modules/private/async/runjobs.lua:218]: in function 'cotask'
    [@programdir/core/base/scheduler.lua:388]:

stack traceback:
        [C]: in function 'error'
        @programdir/core/base/os.lua:872: in function 'base/os.raiselevel'
        (...tail calls...)
        @programdir/modules/private/async/runjobs.lua:256: in field 'catch'
        @programdir/core/sandbox/modules/try.lua:127: in global 'try'
        @programdir/modules/private/async/runjobs.lua:218: in upvalue 'cotask'
        @programdir/core/base/scheduler.lua:388: in function <@programdir/core/base/scheduler.lua:385>
@fengqianliuli
Copy link
Author

已解决,需要在动态库链接时,添加gcov的链接

@waruqi
Copy link
Member

waruqi commented Aug 23, 2022

修复了,你更新到 dev 再试试,不需要额外的 -lgcov,这个不通用,对 clang 不适用。。

主要是因为 mode.coverage rule 仅仅对 ldflags 加了 --coverage flag,对 so 库忘记加了,shflags 上也加上 --coverage 就行了,它会自动 link 上对应的 -lgcov

@waruqi waruqi added this to the v2.7.1 milestone Aug 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants