解决make遇到Arguments too long
升级到了Ubuntu20.04的时候报错 Arguments too long, 最终通过降级make版本解决,接下来看看解决:
- 问题
Ubuntu20.04上make编译生成so的时候报错:
make[1]:execvp:/bin/sh:Arguments too long
对应makefile中的报错位置,仅仅是生成so的时候报错,伪代码如下
${build_tool} -shared -fpic -o "$@" ${OBJ_FILE} ${LDFLAGS}
然而如果不通过make执行,将后面的参数打印出来后,直接用交叉编译链执行是可以的,所以怀疑方向是在make
- 分析:
(1)查看系统命令缓冲区大小,通过命令
xargs --show-limits
执行结果:
limit.png同时我也将这个库的合包段参数打出来看,确实很多 ,达到了 262 822(超过了系统 命令缓冲区的大小: 131072)
到这,也知道为什么报错了,因为执行的时候命令参数超过系统环境长度了,解决思路:
1)降低命令长度:最直接的方式肯定是降低长度,不过这个并不是最长久的,因为在这个架构下的代码去降低路径来缩减长度,无非是当前编译过了,在研发不断增长,到了某一天,就又会遇到超长
2)改编译配置:因为升级了ubuntu版本才出现编译不过,接下来查看make相关官方文献
(2)进行一系列查询,到了ubuntu官方去查询,在make的changelog中又有找到类似问题的修复说明:
ubuntu_make_changelog.png进一步在4.1的新版本4.1.9中的diff里找到有对应的修复4.1.9-diff,但4.2.1的版本里却没看到相关补丁内容,看起来是没有合到4.2.1中,那接下来尝试回退版本试试
- 更新make版本实验验证
搭建虚拟机,匹配到Ubuntu 20.04,先用安装make V4.2.1版本进行编译复现问题,再降级到补丁的make版本进行编译观察是否解决
(1)make V4.2.1版本安装:装虚拟机后再 apt安装make即可
new-make-version.png确认安装后Make版本一致,均为4.2.1,接下来进行make编译
new-make-exe.png编译结果一致
(2)补丁版本降级安装:
下载补丁版本,地址
https://mirrors.tscak.com/ubuntu/pool/main/m/make-dfsg/
下载对应的deb,我的镜像安装包:
ubuntu-20.04.1-desktop-amd64.iso
通过:
uname -a
也看得到
root@ubuntu:~/make-4.1.9# uname -a Linux ubuntu 5.15.0-60-generic #66~20.04.1-Ubuntu SMP Wed Jan 25 09:41:30 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
所以下载:make_4.1-9.1ubuntu1_amd64.deb
下载到了直接编译好的deb,然后进行安装(中间遇到两次报错也记录了出来,也可以直接看最后一次正确安装)
root@ubuntu:~/make-4.1.9# sudo apt install make_4.1-9.1ubuntu1_amd64.deb
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package make_4.1-9.1ubuntu1_amd64.deb
报错:
Unable to locate package make_4.1-9.1ubuntu1_amd64.deb
注意:(1)一定找对目录,到在deb文件目录 (2)要安装的文件需要真的在这个文件夹下,然后以相对路径去执行,即加上 ./
root@ubuntu:~/make-4.1.9# sudo apt install -y ./make_4.1-9.1ubuntu1_amd64.deb
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'make' instead of './make_4.1-9.1ubuntu1_amd64.deb'
Suggested packages:
make-doc
The following packages will be DOWNGRADED:
make
0 upgraded, 0 newly installed, 1 downgraded, 0 to remove and 238 not upgraded.
E: Packages were downgraded and -y was used without --allow-downgrades.
报错:
Packages were downgraded and -y was used without --allow-downgrades.
正确安装:由于 包已经下在本地了,所以不需要-y, 去掉后重新装一次:
root@ubuntu:~/make-4.1.9# sudo apt install ./make_4.1-9.1ubuntu1_amd64.deb
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'make' instead of './make_4.1-9.1ubuntu1_amd64.deb'
Suggested packages:
make-doc
The following packages will be DOWNGRADED:
make
0 upgraded, 0 newly installed, 1 downgraded, 0 to remove and 238 not upgraded.
Need to get 0 B/154 kB of archives.
After this operation, 12.3 kB disk space will be freed.
Do you want to continue? [Y/n] y
Get:1 /root/make-4.1.9/make_4.1-9.1ubuntu1_amd64.deb make amd64 4.1-9.1ubuntu1 [154 kB]
dpkg: warning: downgrading make from 4.2.1-1.2 to 4.1-9.1ubuntu1
(Reading database ... 238557 files and directories currently installed.)
Preparing to unpack .../make_4.1-9.1ubuntu1_amd64.deb ...
Unpacking make (4.1-9.1ubuntu1) over (4.2.1-1.2) ...
Setting up make (4.1-9.1ubuntu1) ... Processing triggers for man-db (2.9.1-1) ...
N: Download is performed unsandboxed as root as file '/root/make-4.1.9/make_4.1-9.1ubuntu1_amd64.deb' couldn't be accessed by user '_apt'. - pkgAcquire::Run (13: Permission denied)
root@ubuntu:~/make-4.1.9# make --version
GNU Make 4.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
root@ubuntu:~/make-4.1.9#
这里就能看到,原本是 4.2.1-1.2 降级到了 4.1-9。接下来再进行make,就能通过了