静态链接 util-linux - 交叉编译(生成动态链接文件)

rwx*_*rwx 4 linux arm

我一直在尝试为arm交叉编译util-linux,但我总是得到动态链接的可执行文件,我不知道为什么会这样。我的目标是静态的。我一直在使用类似步骤的不同工具之前进行交叉编译,并且它一直有效,所以我不知道这次我做错了什么。我使用的是 Ubuntu 16.04。以下是我正在运行的命令:

export CC=arm-linux-gnueabi-gcc
export ac_cs_linux_vers=4
export CFLAGS=-static
export CPPFLAGS=-static
export LDFLAGS=-static

./configure --host=arm-linux LDFLAGS=-static --disable-shared --without-tinfo --without-ncurses --disable-ipv6 --disable-pylibmount --enable-static-programs=fdisk,sfdisk,whereis --prefix=/opt/util-linux/arm --bindir=/opt/util-linux/arm/bin --sbindir=/opt/util-linux/arm/sbin
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我在我能想到的每个地方都指定了静态,甚至重复了一些东西“只是为了确保它理解我”,并且在运行配置脚本后,以下是输出:

util-linux  2.28.2

prefix:            /opt/util-linux/arm
exec prefix:       ${prefix}

localstatedir:     ${prefix}/var
bindir:            /opt/util-linux/arm/bin
sbindir:           /opt/util-linux/arm/sbin
libdir:            ${exec_prefix}/lib
includedir:        ${prefix}/include
usrbin_execdir:    ${exec_prefix}/bin
usrsbin_execdir:   ${exec_prefix}/sbin
usrlib_execdir:    ${exec_prefix}/lib

compiler:          arm-linux-gnueabi-gcc
cflags:            -static
suid cflags:       
ldflags:           -static
suid ldflags:      

Python:            /usr/bin/python
Python version:    2.7
Python libs:       ${exec_prefix}/lib/python2.7/site-packages

Bash completions:  /usr/share/bash-completion/completions
Systemd support:   no
Btrfs support:     yes

warnings:
Run Code Online (Sandbox Code Playgroud)

然后我这样做:

make fdisk
Run Code Online (Sandbox Code Playgroud)

或者

make whereis
Run Code Online (Sandbox Code Playgroud)

编译完成后,我会:

file fdisk
Run Code Online (Sandbox Code Playgroud)

fdisk是刚刚创建的文件并且:

fdisk: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.3, for GNU/Linux 3.2.0, BuildID[sha1]=369363ef8f8173a3a1c2edc178eb77255a2dc415, not stripped
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,它说“动态链接”。我一直在互联网上搜索,但未能找到答案。我也这样做:

./configure --host=arm-linux LDFLAGS=-static --disable-shared --without-tinfo --without-ncurses --disable-ipv6 --disable-pylibmount --prefix=/opt/util-linux/arm --bindir=/opt/util-linux/arm/bin --sbindir=/opt/util-linux/arm/sbin
Run Code Online (Sandbox Code Playgroud)

这与之前的配置命令完全相同,除了缺少“--enable-static-programs”参数,该参数默认“应该”将所有内容编译为静态,但事实并非如此。

我做错了什么还是这是一个 Makefile 错误?

rwx*_*rwx 6

我刚刚弄清楚为什么我的问题中发布的原始命令没有生成静态文件!我必须运行make LDFLAGS="--static "。在我这样做之后,一切都静态链接了!

再说一遍,我跑了:

export CC=arm-linux-gnueabi-gcc
export ac_cs_linux_vers=4
export CFLAGS=-static
export SUID_CFLAGS=-static
export SUID_LDFLAGS=-static
export CPPFLAGS=-static
export LDFLAGS=-static
Run Code Online (Sandbox Code Playgroud)

然后

./configure --host=arm-linux-gnueabi --disable-shared --without-tinfo --without-ncurses --disable-ipv6 --disable-pylibmount --prefix=/opt/util-linux/arm --bindir=/opt/util-linux/arm/bin --sbindir=/opt/util-linux/arm/sbin
Run Code Online (Sandbox Code Playgroud)

进而

make LDFLAGS="--static"
Run Code Online (Sandbox Code Playgroud)

一切都是静态链接的!正如我之前的回答所示,不再需要对象文件收集,但是,这也可以用作替代方案。

另外,为了供您参考,这是我的版本信息,你们中的一些人可能会关心:

$ arm-linux-gnueabi-gcc --version
arm-linux-gnueabi-gcc (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.1) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ arm-linux-androideabi-ld --version
GNU gold (GNU Binutils 2.25.90.20151125) 1.11
Copyright (C) 2015 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.
Run Code Online (Sandbox Code Playgroud)