将 gcc/g++ 仪器功能与 MinGW 一起使用?

Posted

技术标签:

【中文标题】将 gcc/g++ 仪器功能与 MinGW 一起使用?【英文标题】:Using gcc/g++ instrument functions with MinGW? 【发布时间】:2010-08-01 08:56:30 【问题描述】:

我尝试将 gcc 仪器功能与 MinGW 的 g++ 编译器一起使用,但我 总是遇到链接器问题。是否可以将仪器功能与 MinGW/MSYS?

链接器失败输出:

$ g++ instrumentFunctions.cpp -o iftest -finstrument-functions >> iflog.txt
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x4e): undefined reference to `__cyg_prof
ile_func_enter'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x158): undefined reference to `__cyg_pro
file_func_exit'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x179): undefined reference to `__cyg_pro
file_func_enter'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x18c): undefined reference to `__cyg_pro
file_func_exit'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x1a7): undefined reference to `__cyg_pro
file_func_enter'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x1bf): undefined reference to `__cyg_pro
file_func_exit'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x1db): undefined reference to `__cyg_pro
file_func_enter'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x1f3): undefined reference to `__cyg_pro
file_func_exit'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x22f): undefined reference to `__cyg_pro
file_func_enter'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x27a): undefined reference to `__cyg_pro
file_func_exit'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x29b): undefined reference to `__cyg_pro
file_func_enter'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x2e4): undefined reference to `__cyg_pro
file_func_exit'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x2ff): undefined reference to `__cyg_pro
file_func_enter'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x326): undefined reference to `__cyg_pro
file_func_exit'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x341): undefined reference to `__cyg_pro
file_func_enter'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x368): undefined reference to `__cyg_pro
file_func_exit'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text$printf[_printf]+0x16): undefined referenc
e to `__cyg_profile_func_enter'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text$printf[_printf]+0x43): undefined referenc
e to `__cyg_profile_func_exit'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text$_ZSt3minIjERKT_S2_S2_[unsigned int const&
 std::min<unsigned int>(unsigned int const&, unsigned int const&)]+0x15): undefined reference to `__cyg_profile_func_ent
er'
C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text$_ZSt3minIjERKT_S2_S2_[unsigned int const&
 std::min<unsigned int>(unsigned int const&, unsigned int const&)]+0x42): undefined reference to `__cyg_profile_func_exi
t'
collect2: ld returned 1 exit status

我使用的g++命令:

g++ iftest.cpp -o iftest -finstruction-functions

我的测试源列表:

#include <stdio.h>

  void __cyg_profile_func_enter( void *, void * ) __attribute__ ((no_instrument_function));

  void __cyg_profile_func_enter(void *func, void *callsite)
  
    printf("%p\n", (int)func);
  


  void func_c( void )
  
    return;
  

  void func_b( void )
  
    func_c();

    return;
  

  void func_a( void )
  
    func_b();
    return;
  


  int main()
  
    func_a();
    func_c();
  

【问题讨论】:

【参考方案1】:

您需要为 __cyg* 函数提供“C”链接。在 C++ 中定义的函数通常会得到一个不能从非 C++ 编写的库中使用的重命名。您可以通过使用 `extern "C" 块来要求编译器为函数提供 C 将为其命名的名称。以下应该可以正常编译。

#include <stdio.h>

extern "C" 
  void __cyg_profile_func_enter( void *, void * ) __attribute__ ((no_instrument_function));

  void __cyg_profile_func_enter(void *func, void *callsite)
  
    printf("enter %p\n", func);
  
  void __cyg_profile_func_exit( void *, void * ) __attribute__ ((no_instrument_function));

  void __cyg_profile_func_exit(void *func, void *callsite)
  
    printf("exit %p\n", func);
  


  void func_c( void )
  
    return;
  

  void func_b( void )
  
    func_c();

    return;
  

  void func_a( void )
  
    func_b();
    return;
  


  int main()
  
    func_a();
    func_c();
  

【讨论】:

以上是关于将 gcc/g++ 仪器功能与 MinGW 一起使用?的主要内容,如果未能解决你的问题,请参考以下文章

mingw和gcc的关系?mingw具有gcc的全部核心编译功能吗

Windows下 gcc/g++的安装与配置

如何使用 mingw64 gcc/g++ 比较两个图像?

MinGW:如何在 Windows 上将 GCC/G++ 升级到版本 5?

更新 ubuntu 10.4 后无法将 errno.h 与 gcc/g++ 一起使用

msvc MinGW gcc g++关系