本文共 7088 字,大约阅读时间需要 23 分钟。
在编译和运行环境的设置上,本文使用以下工具和版本:
ubuntu-1804
(x86_64)qca-IPQ6018
,支持arm
和arm64
,基于openwrt
系统本文将逐步解释以下内容:
编写本地程序:"
指定toolchain,交叉编译:
多级文件如何定义、生成Makefile相关文件:
autoscanf
:用于生成configure.scan
文件scan
文件改名为configure.ac
,并对相关内容进行修改aclocal
:用于生成aclocal.m4
文件autoconf
:根据上述M4文件生成configure
脚本automake --add-missing
命令./configure
并编译项目以下是完整的命令列表:
# cmd : autoscanautoscan# cmd : mv configure.scan configure.acmv configure.scan configure.ac# cmd : aclocalaclocal# cmd : autoconfautoconf# cmd : touch Makefile.amtouch Makefile.am# cmd : touch src/Makefile.amtouch src/Makefile.am# cmd : automakeautomake --add-missing# cmd : ./configure./configure
在交叉编译过程中,需要指定以下参数:
# 交叉编译示例./configure --host=arm-openwrt-linux --prefix=/[路径]/toolchain-qca/toolchain-arm_cortex-a7_gcc-5.2.0_musl-1.1.16_eabi
.├── bin/├── include/│ └── common.h└── src/ ├── func.c └── main.c
configure.ac
中的AC_CONFIG_SRCDIR
和AC_OUTPUT
命令bin/
和src/
目录下创建Makefile.amautomake
工具进行编译#!/bin/sh# Liam# 20210524# GPL Version 3# 1. 检查环境和命令# 2. 创建项目并初始化源代码# 3. 执行autoscan并修改配置文件# 4. 执行aclocal、autoconf# 5. 创建Makefile.am文件并生成Makefile.in# 6. 编译和运行function func_0_check_project_name { if [[ -z "$1" ]]; then echo "Error: need [project name]" exit 1 fi}function func_1_check_env { CMD_AUTOSCAN=$(which autoscan) CMD_ACLOCAL=$(which aclocal) CMD_AUTOCONF=$(which autoconf) CMD_AUTOMAKE=$(which automake) if [[ -z "$CMD_AUTOSCAN" ]] || [[ -z "$CMD_ACLOCAL" ]] || [[ -z "$CMD_AUTOCONF" ]] || [[ -z "$CMD_AUTOMAKE" ]]; then echo "> env check : fail" echo "Tips: try 'sudo apt install automake'" exit 1 else echo "> env check : OK" fi}function func_2_new_pjt_init_code { if [[ -d "$1" ]] || [[ -f "$1" ]]; then echo "> init : fail" echo "Tips: change [project name] or try 'rm -r $1' first" exit 1 fi touch "$1" mkdir -p "$1/bin" "$1/include" "$1/src" touch "$1/include/common.h" touch "$1/src/main.c" touch "$1/src/func.c" echo "#ifndef __COMMON_H__#define __COMMON_H__#define __SOFT_VERSION__ \"1.0.0\"void show_version(void);#endif" > "$1/include/common.h" echo "#include#include \"common.h\"int main() { show_version(); return 0;}" > "$1/src/main.c" echo "#include void show_version(void) { printf(\"ver:%s\\n\", __SOFT_VERSION__);}" > "$1/src/func.c" if [[ -f "$1/include/common.h" ]] || [[ -f "$1/src/main.c" ]] || [[ -f "$1/src/func.c" ]]; then echo "> init : OK" else echo "> init : fail" exit 1 fi}function func_3_autosacn_M_ac { autoscan "$1" || exit 1 if [[ ! -f "$1/configure.scan" ]]; then echo "> autoscan : fail" exit 1 fi mv "$1/configure.scan" "$1/configure.ac" sed -i "s#FULL-PACKAGE-NAME#$1#g" "$1/configure.ac" sed -i "s#VERSION#1.0#g" "$1/configure.ac" sed -i "s#BUG-REPORT-ADDRESS#liam@liam.com#g" "$1/configure.ac" sed -i "s/AC_CONFIG_HEADERS/#AC_CONFIG_HEADERS(g)/g" "$1/configure.ac" sed -i "s#(\\[config.h\\])#(\\[config.h\\])\\nAM_INIT_AUTOMAKE#g" "$1/configure.ac" sed -i "s#AC_OUTPUT#AC_OUTPUT(Makefile src/Makefile)#g" "$1/configure.ac" echo "> autoscan : done"}function func_4_aclocal { cd "$1" if [[ ! -f "configure.ac" ]]; then echo "> aclocal : fail" echo "Tips: no configure.ac" exit 1 fi aclocal if [[ ! -f "aclocal.m4" ]]; then echo "> aclocal : fail" exit 1 else echo "> aclocal : OK" fi cd "`pwd`" > /dev/null}function func_4_autoconf { cd "$1" if [[ ! -f "aclocal.m4" ]]; then echo "> aclocal.m4 : fail" echo "Tips: no aclocal.m4" exit 1 fi autoconf if [[ ! -f "configure" ]]; then echo "> autoconf : fail" exit 1 else echo "> autoconf : OK" fi cd "`pwd`" > /dev/null}function func_5_automake { cd "$1" echo "AUTOMAKE_OPTIONS=foreignSUBDIRS=src" > Makefile.am echo "AM_CPPFLAGS=-I\$(top_srcdir)/includebin_PROGRAMS=\$(top_srcdir)/bin/test__top_srcdir__= Blogsdirtop_srcdir= Blogsdir" > src/Makefile.am if [[ ! -f "configure" ]] || [[ ! -f "configure.ac" ]]; then echo "> automake : fail" echo "Tips: no configure / configure.ac" exit 1 fi if [[ ! -f "Makefile.am" ]] || [[ ! -f "src/Makefile.am" ]]; then echo "> automake : fail" echo "Tips: no ./*/Makefile.am" exit 1 fi automake --add-missing if [[ ! -f "Makefile.in" ]] || [[ ! -f "src/Makefile.in" ]]; then echo "> automake : fail" exit 1 else echo "> automake : OK" fi cd "`pwd`" > /dev/null}function func_6_configure { cd "$1" if [[ ! -f "configure" ]] || [[ ! -f "configure.ac" ]]; then echo "> automake : fail" echo "Tips: no configure / configure.ac" exit 1 fi ./configure --host=arm-openwrt-linux --prefix=/[路径]/toolchain-qca/toolchain-arm_cortex-a7_gcc-5.2.0_musl-1.1.16_eabi if [[ ! -f "Makefile" ]]; then echo "> configure : fail" exit 1 else echo "> configure : OK" fi cd "`pwd`" > /dev/null}function func_0_make_run { cd "$1" if [[ ! -f "Makefile" ]] || [[ ! -f "src/Makefile" ]]; then echo "> test : fail" echo "Tips: no Makefile / src/Makefile" exit 1 fi if [[ ! -f "include/common.h" ]] || [[ ! -f "src/main.c" ]] || [[ ! -f "src/func.c" ]]; then echo "> test : fail" echo "Tips: no source code" exit 1 fi make if [[ ! -f "bin/test" ]]; then echo "> test : fail" echo "Tips: no bin/test" exit 1 fi ./bin/test cd "`pwd`" > /dev/null echo "> test : done"}case $1 in --env) func_1_check_env ;; --init) func_0_check_project_name $2 || exit 1 func_2_new_pjt_init_code $2 ;; --autoscan) func_0_check_project_name $2 || exit 1 func_3_autosacn_M_ac $2 ;; --aclocal) func_0_check_project_name $2 || exit 1 func_4_aclocal $2 ;; --autoconf) func_0_check_project_name $2 || exit 1 func_4_autoconf $2 ;; --automake) func_0_check_project_name $2 || exit 1 func_5_automake $2 ;; --configure) func_0_check_project_name $2 || exit 1 func_6_configure $2 ;; --test) func_0_check_project_name $2 || exit 1 func_0_make_run $2 ;; --new) func_1_check_env || exit 1 func_0_check_project_name $2 || exit 1 func_2_new_pjt_init_code $2 || exit 1 func_3_autosacn_M_ac $2 || exit 1 func_4_aclocal $2 || exit 1 func_4_autoconf $2 || exit 1 func_5_automake $2 || exit 1 func_6_configure $2 || exit 1 echo "DE done, new project named : $2" ;; --help) echo "usage : ./script.sh --new [project name]" ;; *) echo "usage : ./script.sh --new [project name]" ;;esac
转载地址:http://luuuk.baihongyu.com/