在一个新的linux环境下编译程序,编译.a的时候正常;编译.so和app的时候异常,报了一堆 undefined reference to `__cyg_profile_func。。。 错误,检查代码的编译工具链和依赖库路径,未发现异常;
因为使用的makefile,在别的linux环境下验证过是正常的,怀疑是芯片的编译工具链差异导致;
最终排查到是 CFLAGS 里面的 “-finstrument-functions” 编译选项不支持,去掉就好了;
undefined reference to `__cyg_profile_func_enter'
undefined reference to `__cyg_profile_func_exit'
排查思路
1:先建立一个最简单的.c文件,用指令去编译
#include <stdio.h>
void main()
{
int a = 0;
if (0 == a)
printf("ok ok ok");
}
/home/ec200a/ec200a_linux/owtoolchain/linux64/bin/arm-openwrt-linux-gcc test_app.c -o test_app
如果单个编译指令支持的话,说明编译器没问题
2:makefile 的编译选项修改
主要查看 CFLAGS 和 CPPFLAGS 的编译参数,没什么好的办法,就是一个个注释编译,看看是哪里的问题,我这边排查到 “-finstrument-functions” 这个参数不支持,去掉就正常了
CFLAGS = -DLINUX -Wno-multichar -fPIC -O2 -fexpensive-optimizations -frename-registers -fomit-frame-pointer
CPPFLAGS = -DLINUX -Wno-multichar -fPIC -fexceptions -fpermissive -w -O2 -fexpensive-optimizations -frename-registers -fomit-frame-pointer
# DEBUG_FLAGS = -fexceptions -finstrument-functions -funwind-tables -g -rdynamic -O0
DEBUG_FLAGS = -fexceptions -funwind-tables -g -rdynamic -O0
CFLAGS += $(RELEASE_FLAGS)
CPPFLAGS += $(RELEASE_FLAGS)
扩展知识
编译时如果为gcc加上“-finstrument-functions”选项,那在每个函数的入口和出口处会各增加一个额外的hook函数的调用;示例如下
static void func_test(v)
{
/* your code... */
}
那通过-finstrument-functions选项编译后,这个函数的定义就变成了:
static void func_test(v)
{
__cyg_profile_func_enter(this_fn, call_site);
/* your code... */
__cyg_profile_func_exit(this_fn, call_site);
}
文章讲述了在新Linux环境下编译程序时遇到的undefinedreferenceto__cyg_profile_func错误,经过排查发现是由于CFLAGS中的-finstrument-functions编译选项不被支持。作者通过简化.c文件测试和逐步排除编译选项解决了问题。
809

被折叠的 条评论
为什么被折叠?



