我使用 autoconf/automake 做了一个小项目。
该项目旨在进行 native 编译和交叉编译,例如因此:
./configure --host=arm-none-eabi
这很好用。但一个问题是,如果未安装交叉编译器,配置的脚本将忽略它,并使用安装的默认编译器愉快地编译它,例如
./configure --host=arm-none-eabi
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for arm-none-eabi-strip... no
checking for strip... strip
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether make supports nested variables... (cached) yes
checking whether make sets $(MAKE)... (cached) yes
checking for arm-none-eabi-gcc... no
checking for gcc... gcc
checking whether the C compiler works... yes
此处未找到 arm-none-eabi-gcc。但它会找到 native gcc,并继续使用 native 编译器。
如果我使用 --host=XXXX 请求交叉编译并且找不到相应的编译器,我可以在 configure.ac 中放入什么来让配置脚本停止并出错?
最佳答案
当您指定 --host=<host-type>
时,并且此值不同于运行 config.guess
的结果脚本,autoconf
输入 cross-compilation模式。具体来说,变量 cross_compiling
设置为 yes
.
如果配置脚本处于“交叉编译”模式,它无法运行任何生成的可执行文件,因此无法判断生成的二进制文件是否是有效的“主机”二进制文件。据推测,文件的大型数据库 magic值可能能够判断是否已生成有效的主机二进制文件。尽管 autotools 拥有几十年的经验,但仍有一些组合问题永远跟不上所有可能的架构和 ABI。
autoconf
C 编译器 tests检查编译器 - 即 $CC
- 可以构建可执行文件。 autoconf
如果编译器没有以主机三元组为前缀,例如 arm-none-eabi-gcc
,可能会发出警告, 但如果它找到一个可用的编译器就不会“失败”,例如 native gcc
.
因此,确保使用正确的编译器进行交叉编译的唯一方法是指定编译器:
./configure --host=arm-none-eabi CC="arm-none-eabi-gcc"
如果此编译器无法构建可执行文件,则配置脚本将失败。
关于c - autotools,如果找不到交叉编译器如何失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19708917/