gcc编译器报错集锦


在这里插入图片描述

概述

在进行Linux编程时,使用gcc或者arm-linux-gcc交叉编译器时,偶尔会出现一些奇奇怪怪的问题,有些问题甚至遇到多次,但由于之前没有记录导致需要重新解决,所以就在想着,将一些不常见的编译问题给记录下来,供后续开发参考,于是有了这篇文章,可能现在记录得还不够多,但随着时间积累,应该会积累不少,下次遇到同样问题,可以有迹可循。

文章主要记录一下不常见的错误,供后续开发参考:
在这里插入图片描述


all warnings being treated as errors

日期:2023-05-25 16:31:41
报错:cc1: all warnings being treated as errors
编译条件:

  • Ubuntu 18.04,
  • 编译器:aarch64-mix210-linux-gcc
  • 编译代码:usbip源码,从Linux内核(3.10)复制出来的

错误原因:

  • 使用snprintf函数,将最多31个字节写入大小介于0和255之间的区域,这本来只是一个 warning
  • 生成Makefile时,加了-Werror,将warning当成错误处理

解决方案:

  • 重新解压源码,使用grep "Werror" -rnw ./找到 -Werror 的地方,把它删掉,再执行./autogen.sh
  • sed 's/-Wall -Werror/-Wall/g' ./configure.ac

错误打印:

usbip_host_driver.c: In function 'usbip_host_export_device':
usbip_host_driver.c:359:45: error: '%s' directive output may be truncated writing up to 31 bytes into a region of size between 0 and 255 [-Werror=format-truncation=]
  snprintf(attr_path, sizeof(attr_path), "%s/%s:%d.%d/%s",
                                             ^~
usbip_host_driver.c:359:2: note: 'snprintf' output 7 or more bytes (assuming 293) into a destination of size 256
  snprintf(attr_path, sizeof(attr_path), "%s/%s:%d.%d/%s",
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    edev->udev.path, edev->udev.busid,
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    edev->udev.bConfigurationValue, 0, attr_name);
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
Makefile:442: recipe for target 'libusbip_la-usbip_host_driver.lo' failed
make[2]: *** [libusbip_la-usbip_host_driver.lo] Error 1

运行时缺少动态库,且路径为相对路径./…/…/common/finger…

日期:2023-05-25 16:57:12
报错:编译没出错,运行时缺少动态库,且路径为相对路径./…/…/common/finger…
编译条件:

  • Ubuntu 18.04,
  • 编译器:aarch64-mix210-linux-gcc,使用arm-hisiv100nptl-linux没出现此情况
  • Makefile:CFLAGS += ./…/…/common/finger/lib_himix200/libzaz_himix200_linux.so,使用相对路径的库,但其他库也使用相对路径却不会。

错误原因分析:

  • 运行aarch64-mix210-linux-objdump -p CK6I_UPPER | grep NEEDED查看依赖动态库,也是相对路径:
    NEEDED libiconv.so.2
    NEEDED ./…/…/common/finger/lib_himix200/libzaz_himix200_linux.so

  • 猜测是链接时使用相对路径的原因,具体原因不清楚,尝试去掉

解决方案:

  • 改Makefile,-L ./../../common/finger/lib_himix200/ -lzaz_himix200_linux

错误打印:

# ./a.out
./a.out: error while loading shared libraries: ./../../common/finger/lib_himix200/libzaz_himix200_linux.so: cannot open shared object file: No such file or directory

海思 aarch64-mix210-linux-g++ 编译器,list对象多层使用段错误

日期:2023-09-11 19:58:31
报错:编译没报错,运行段错误
编译条件:

  • Ubuntu 18.04,
  • 编译器:aarch64-mix210-linux-g++
  • 编译代码:UPPER

错误原因分析:

  • 同样代码使用 arm-hisiv100nptl-linux- 、arm-hisiv300-linux- 编译器没有问题,换了编译器就出问题;
    应该是编译器对list的对象处理不同。从错误打印看,是释放对象时deallocate出错。

解决方案:

  • 把返回对象,改成返回引用,使其不需要重新创建销毁list对象。

错误代码:

typedef struct seatplan_device_info
{
	//省略无关字段...
	VECTOR_SEATPLAN_SIGNAL_INFO vtsignal;
}SEATPLAN_DEVICE_INFO, *PTR_SEATPLAN_DEVICE_INFO;
typedef vector<PTR_SEATPLAN_DEVICE_INFO> 	VECTOR_SEATPLAN_DEVICE_INFO;
typedef struct seatplan_info
{
	//省略无关字段...
	VECTOR_SEATPLAN_DEVICE_INFO vtdevice;
}SEATPLAN_INFO, *PTR_SEATPLAN_INFO;
typedef list<PTR_SEATPLAN_INFO> 		LIST_PSEATPLAN_INFO;

LIST_PSEATPLAN_INFO seatPlanList = GetSeatPlanListObj();
LIST_PSEATPLAN_INFO::iterator itor;
for(itor = seatPlanList.begin(); itor != seatPlanList.end() ; itor++)
{
	// 省略无关代码
	VECTOR_SEATPLAN_DEVICE_INFO::iterator iter;
	for(iter = pstInfo->vtdevice.begin() ; iter != pstInfo->vtdevice.end() ; iter++)
	{
		PTR_SEATPLAN_DEVICE_INFO pstDevInfo = *iter;
		const HI_CHAR *p = pstDevInfo->szshow_chn;;
		HI_U8 u8ShowChn[MAX_KVM_AV_CHN_NUM] = {0};
		for(int i = 0 ; i < MAX_KVM_AV_CHN_NUM ; i++)
		{
			sscanf(p ,"%d[^,]" , (char*)&u8ShowChn[i]);
			p = strchr(p , ',');
			if(!p)
				break;
			p++;
		}
	}
}

上述代码在arm-hisiv100nptl-linux- 、arm-hisiv300-linux- 编译器编译后运行正常, aarch64-mix210-linux-g++ 编译后会出错,gdb的报错打印如下:

Program received signal SIGSEGV, Segmentation fault.
[Switching to LWP 5194]
0x0000007ff737a3f4 in free () from /lib64/libc.so.6
(gdb) bt
#0  0x0000007ff737a3f4 in free () from /lib64/libc.so.6
#1  0x00000000004a885c in deallocate (this=0x7f69ffa380, __p=<optimized out>) at /opt/linux/x86-arm/aarch64-mix210-linux/aarch64-linux-gnu/include/c++/7.3.0/ext/new_allocator.h:125
#2  deallocate (__a=..., __n=1, __p=<optimized out>) at /opt/linux/x86-arm/aarch64-mix210-linux/aarch64-linux-gnu/include/c++/7.3.0/bits/alloc_traits.h:462
#3  _M_put_node (this=0x7f69ffa380, __p=<optimized out>) at /opt/linux/x86-arm/aarch64-mix210-linux/aarch64-linux-gnu/include/c++/7.3.0/bits/stl_list.h:387
#4  std::__cxx11::_List_base<seatplan_info*, std::allocator<seatplan_info*> >::_M_clear (this=this@entry=0x7f69ffa380)
    at /opt/linux/x86-arm/aarch64-mix210-linux/aarch64-linux-gnu/include/c++/7.3.0/bits/list.tcc:80
#5  0x00000000004a8cb0 in ~_List_base (this=0x7f69ffa380, __in_chrg=<optimized out>) at /opt/linux/x86-arm/aarch64-mix210-linux/aarch64-linux-gnu/include/c++/7.3.0/bits/stl_list.h:442
#6  ~list (this=0x7f69ffa380, __in_chrg=<optimized out>) at /opt/linux/x86-arm/aarch64-mix210-linux/aarch64-linux-gnu/include/c++/7.3.0/bits/stl_list.h:733
#7  CGuiSer::PlanManage (pmsg=pmsg@entry=0x7f800d9010) at ../guiServer/guiSer.cpp:961
#8  0x00000000004ad224 in CGuiSer::MsgRecvProc (procId=0, recvBuf=0x7f800d9010, recvLen=28, sendBuf=0x7f80058010, sendLen=0x7f69ffa904) at ../guiServer/guiSer.cpp:170
#9  0x0000000000568648 in SysCtrlMsgRecvProc (arg=<optimized out>) at sys_ctrl_server.c:127
#10 0x0000007ff7d508f0 in ?? () from /lib64/libpthread.so.0
#11 0x0000007ff73d386c in ?? () from /lib64/libc.so.6
(gdb) 

最后,将代码中获取对象的语句改为获取引用,运行就正常了。看gdb的报错打印,应该是释放对象出错,具体的原因不清楚,记录一下,供参考。

LIST_PSEATPLAN_INFO seatPlanList = GetSeatPlanListObj();
改为
LIST_PSEATPLAN_INFO& seatPlanList = GetSeatPlanListObj();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wkd_007

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值