0

I am trying to debug a flow and for which I am trying to make use of -finstrument-functions compiler option. I have successfully written up below method and created trace.o object file:

void __cyg_profile_func_enter(void* this_fn, void* call_site)
__attribute__((no_instrument_function));
void __cyg_profile_func_exit(void* this_fn, void* call_site)
__attribute__((no_instrument_function));

It works well with sample code and I get function address logged in file from where I can extract file:function name.

In our project code there around 1500 files so I have added the below code in my CMakeLists.txt file:

SET(GCC_COVERAGE_COMPILE_FLAGS "-finstrument-functions")
add_definitions(${GCC_COVERAGE_COMPILE_FLAGS})

The above config ensures that all my source code files are compiled with -finstrument-functions compiler flag.

To ensure that I link trace.o file I am trying the below method:

SET(OBJS
  /c/work/trace.o #path to trace.o in my system
)

SET_SOURCE_FILES_PROPERTIES(
  ${OBJS}
  PROPERTIES
  EXTERNAL_OBJECT true
  GENERATED true
)

But during compilation of my project code I get below error:

    Test.cpp.obj: In function `cc::cppx::cMethod(int, int*, int&)':
    Test.cpp:16: undefined reference to `__cyg_profile_func_enter'
    Test.cpp:17: undefined reference to `__cyg_profile_func_exit'
    Test.cpp.obj: In function `__gthread_active_p':
    C:/tools/mingw64/x86_64-w64-mingw32/include/c++/x86_64-w64-mingw32/bits/gthr-default.h:304: undefined reference to `__cyg_profile_func_enter'
    C:/tools/mingw64/x86_64-w64-mingw32/include/c++/x86_64-w64-mingw32/bits/gthr-default.h:304: undefined reference to `__cyg_profile_func_exit'
    Test.cpp.obj: In function `__exchange_and_add_single':
    C:/tools/mingw64/x86_64-w64-mingw32/include/c++/ext/atomicity.h:66: undefined reference to `__cyg_profile_func_enter'
    C:/tools/mingw64/x86_64-w64-mingw32/include/c++/ext/atomicity.h:68: undefined reference to `__cyg_profile_func_exit'
    Test.cpp.obj: In function `forward<cc::cppx::TestInvoke_lambda_with_args_byvalue_ptr_ref_Test::TestBody()::<lambda(int, int*, int&)>&>':
    C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/move.h:76: undefined reference to `__cyg_profile_func_enter'
    C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/move.h:76: undefined reference to `__cyg_profile_func_exit'
    Test.cpp.obj: In function `operator()':
    Test.cpp:54: undefined reference to `__cyg_profile_func_enter'
    Test.cpp:56: undefined reference to `__cyg_profile_func_exit'
    Test.cpp.obj: In function `__exchange_and_add':
    C:/tools/mingw64/x86_64-w64-mingw32/include/c++/ext/atomicity.h:49: undefined reference to `__cyg_profile_func_enter'
    C:/tools/mingw64/x86_64-w64-mingw32/include/c++/ext/atomicity.h:49: undefined reference to `__cyg_profile_func_exit'
    Test.cpp.obj: In function `__tcf_0':

I am trying hard to find ways how to achieve my objective but it is not sorting the issue - is there a way the same can be setup?

Programmer
  • 8,303
  • 23
  • 78
  • 162
  • Please don't use [`add_definitions`](https://cmake.org/cmake/help/latest/command/add_definitions.html) to add general compiler flags. First [check that flags are supported](https://cmake.org/cmake/help/latest/module/CheckCXXCompilerFlag.html), then use e.g. [`target_compile_options`](https://cmake.org/cmake/help/latest/command/target_compile_options.html) to add general options. – Some programmer dude Jan 11 '18 at 14:54
  • As for your problem, are you missing linking with an object file or a library? Where do you link with this `trace.o` file? – Some programmer dude Jan 11 '18 at 14:54
  • Thanks - I am new to CMAKE - can you please provide the option you are mentioning? – Programmer Jan 11 '18 at 15:15
  • You want to "combine" the manually created object file with executable, and the referenced question explains how to do that. It seems you have followed approach in the [second answer](https://stackoverflow.com/a/38610428/3440745), but its code lacks some parts. You may follow the other answer with `target_link_libraries` approach. If you want to link your object file with *all* futher executables, use `link_libraries` instead. – Tsyvarev Jan 11 '18 at 20:01

0 Answers0