I am in need to debug a flow hence compiling its code using cmake / mingw64 C++ compiler. I understand that if we compile with -fprofile-arcs -ftest-coverage for each source code file - I would get debug flow execution details thereby helping me to understand & fix the flow. So I added the below CMAKE options in my CMakelists.txt but I get mentioned error then:
SET(GCC_COVERAGE_COMPILE_FLAGS "-fprofile-arcs -ftest-coverage")
SET(GCC_COVERAGE_LINK_FLAGS "-fprofile-arcs -fprofile-generate -lgcov --coverage")
add_definitions(${GCC_COVERAGE_COMPILE_FLAGS})
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" )
SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}" )
Error during build - linking:
[76/150] Linking CXX executable test.exe
FAILED: test.exe
cmd.exe /C "cd . && C:\tools\mingw64\bin\c++.exe -DGSL_THROW_ON_CONTRACT_VIOLATION -Wall -ftrack-macro-expansion=0 -Werror -std=gnu++14 -D _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING -Og -g3 test.cpp.obj testhelp.cpp.obj -o test.exe -Wl,--out-implib,test.dll.a -Wl,--major-image-version,0,--minor-image-version,0 ext/googletest/build/googlemock/libgmock_main.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
test/case/TestArgs.cpp.obj: In function `_GLOBAL__sub_I_65535_0__ZN2cc5shell30TestCommandArguments_Size_Test10test_info_E':
:(.data+0x2ee0): undefined reference to `__gcov_merge_add'
In function `_GLOBAL__sub_I_65535_0__ZN2cc5shell58TestArgumentCompletion_arg_root_2_subgroups_empty_arg_Test10test_info_E':
: undefined reference to `__gcov_init'
: undefined reference to `__gcov_merge_add'
I tried various options to resolve it - but it is not helping?
Linker option -lgcov
is actually linking with "gcov" library, so it is better to mark it so:
link_libraries(gcov)
Difference between CMAKE_EXE_LINKER_FLAGS
variable and link_libraries
/target_link_libraries
commands is that the variable adds linker flags to the link command line before the object files, but the commands add linker flags after the object files. This difference is crucial in symbols resolving, when incorrect order of libraries and object files gives undefined reference
error.