openwrt交叉编译

本文介绍了如何使用Buildroot工具链对OpenWRT进行编译,包括设置交叉编译环境、编译SDK、创建自定义工程、编译IPK包并安装至目标机器的过程。

一、交叉编译

1.      建立交叉编译环境

在使用buildroot对openwrt进行编译之后,在buildroot目录下会有一个名叫staging_dir的目录,针对当前平台的toolchain都在这个目录下。

1.1增加toolchain的目录到PATH目录中

Vim ~/.bash_profile

添加代码:

# add openWrt cross-compile path

PATH=$PATH:/home/jason/openWrt/trunk/staging_dir/toolchain-i386_gcc-4.6-linaro_uClibc-0.9.33.2/bin/

1.2   增加staging_dir的目录到toolchain PATH

Vim ~/.bash_profile

添加代码:

STAGING_DIR=/home/jason/openWrt/trunk/staging_dir/

exportSTAGING_DIR

1.3 保存退出

2.      编译

2.1 configure

 ./configure--target=i486-openwrt-linux-uclibc

2.2 make

make CC=i486-openwrt-linux-uclibc-gcc LD=i486-openwrt-linux-uclibc-ld

二、编译ipk包

1.      编译SDK

在buildroot目录下make menuconfig,然后选中SDK进行编译


选中后,进行make编译。

编译完成后,对应生成的SDK会出现类似这样的目录:“openWrt/trunk/bin/x86”,进入SDK后,打印当前工作路径如下:

“/openWrt/trunk/bin/x86/OpenWrt-SDK-x86-for-redhat-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2”

2.      创建工程

在sdk的package目录下创建我们的工程“helloworld”:

新建目录中包含src目录,这个目录就是我们的源代码所在地,另外一个非常重要的文件Makefile,这个Makefile的组成与GNU的有所不同,有点类似于制作rpm包时的spec文件。到后面会有更详细的介绍。以下是具体文件的位置:


其中helloworld.c的内容如下:


Src目录下的Makefile文件内容如下:

最后是helloworld目录下的Makefile的内容:

##############################################

# OpenWrtMakefile for helloworld program

#

#

# Most ofthe variables used here are defined in

# theinclude directives below. We just need to

# specifya basic description of the package,

# whereto build our program, where to find

# thesource files, and where to install the

#compiled program on the router.

#

# Be verycareful of spacing in this file.

# Indentsshould be tabs, not spaces, and

# thereshould be no trailing whitespace in

# linesthat are not commented.

#

##############################################

 

include$(TOPDIR)/rules.mk

 

# Nameand release number of this package

PKG_NAME:=helloworld

PKG_RELEASE:=1

 

 

# Thisspecifies the directory where we're going to build the program.

# Theroot build directory, $(BUILD_DIR), is by default the build_mipsel

#directory in your OpenWrt SDK directory

PKG_BUILD_DIR:= $(BUILD_DIR)/$(PKG_NAME)

 

 

include$(INCLUDE_DIR)/package.mk

 

 

 

# Specifypackage information for this program.

# Thevariables defined here should be self explanatory.

# If youare running Kamikaze, delete the DESCRIPTION

#variable below and uncomment the Kamikaze define

# directivefor the description below

definePackage/helloworld

SECTION:=utils

CATEGORY:=Utilities

TITLE:=Helloworld-- prints a snarky message

endef

 

 

 

# Specifywhat needs to be done to prepare for building the package.

# In ourcase, we need to copy the source files to the build directory.

# This isNOT the default.  The default uses thePKG_SOURCE_URL and the

#PKG_SOURCE which is not defined here to download the source from the web.

# Inorder to just build a simple program that we have just written, it is

# mucheasier to do it this way.

defineBuild/Prepare

mkdir -p $(PKG_BUILD_DIR)

$(CP) ./src/* $(PKG_BUILD_DIR)/

endef

 

 

# We donot need to define Build/Configure or Build/Compile directives

# Thedefaults are appropriate for compiling a simple program such as this one

 

 

# Specifywhere and how to install the program. Since we only have one file,

# thehelloworld executable, install it by copying it to the /bin directory on

# therouter. The $(1) variable represents the root directory on the router running

#OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install

#directory if it does not already exist. Likewise $(INSTALL_BIN) contains the

# commandto copy the binary file from its current location (in our case the build

#directory) to the install directory.

definePackage/helloworld/install

$(INSTALL_DIR) $(1)/bin

$(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld$(1)/bin/

endef

 

 

# Thisline executes the necessary commands to compile our program.

# Theabove define directives specify all the information needed, but this

# linecalls BuildPackage which in turn actually uses this information to

# build apackage.

$(eval $(call BuildPackage,helloworld))

这个Makefile的语法规则还是参考官网吧:

http://wiki.openwrt.org/doc/devel/packages

3.      编译

将当前目录返回到SDK:

执行make进行编译。如果一切顺利,最后的结果会保存在SDK/bin/x86/packages目录下,名称为helloword_1_x86.ipk。

4.      安装最新编译的包

通过scp将该包拷贝到目的机器上,通过opkg包管理工具进行安装:

Opkg install helloworld_1_x86.ipk

一切顺利,包安装成功,在当前路径下执行helloworld查看程序执行结果。

<think>我们正在为OpenWrt交叉编译libxml2。OpenWrt通常使用其构建系统(如buildroot)来管理包,但有时我们需要手动交叉编译。 步骤: 1. 设置OpenWrt交叉编译环境(SDK) 2. 下载libxml2源码 3. 配置交叉编译参数 4. 编译和安装 假设我们已经下载并解压了OpenWrt SDK,并且已经设置了环境变量。 详细步骤: </think>### 使用 OpenWrt 交叉编译 libxml2 的完整指南 以下是使用 OpenWrt SDK 交叉编译 libxml2 的详细步骤和代码: #### 1. 准备工作 ```bash # 1.1 下载 OpenWrt SDK(以 22.03.5 版本为例) wget https://downloads.openwrt.org/releases/22.03.5/targets/x86/64/openwrt-sdk-22.03.5-x86-64_gcc-11.3.0_musl.Linux-x86_64.tar.xz tar xvf openwrt-sdk-*.tar.xz cd openwrt-sdk-* # 1.2 设置环境变量 export STAGING_DIR=$(pwd)/staging_dir export PATH=$STAGING_DIR/toolchain-x86_64_gcc-11.3.0_musl/bin:$PATH # 1.3 下载 libxml2 源代码 ./scripts/feeds install libxml2 git clone https://gitlab.gnome.org/GNOME/libxml2.git cd libxml2 ``` #### 2. 创建自定义 Makefile 在 `libxml2` 目录中创建自定义 `Makefile`: ```makefile include $(TOPDIR)/rules.mk PKG_NAME:=libxml2 PKG_VERSION:=2.12.6 PKG_RELEASE:=1 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz PKG_SOURCE_URL:=https://download.gnome.org/sources/libxml2/2.12/ PKG_HASH:=d3a8b2e2d0b3d9e40a2a6d0c3a5a9b4b0d8e8b5d2c7e8a3b0c5d2f7e9a1b0c include $(INCLUDE_DIR)/package.mk define Package/libxml2 SECTION:=libs CATEGORY:=Libraries TITLE:=XML C parser and toolkit URL:=http://xmlsoft.org/ DEPENDS:=+zlib +libpthread endef define Package/libxml2/description Libxml2 is the XML C parser and toolkit developed for the Gnome project. endef CONFIGURE_ARGS += \ --enable-shared \ --enable-static \ --without-python \ --without-lzma \ --host=$(GNU_TARGET_NAME) \ --build=$(GNU_HOST_NAME) \ --prefix=/usr define Build/Configure $(call Build/Configure/Default) endef define Build/Compile $(MAKE) -C $(PKG_BUILD_DIR) \ DESTDIR="$(PKG_INSTALL_DIR)" \ all install endef define Build/InstallDev $(INSTALL_DIR) $(1)/usr/include $(CP) $(PKG_INSTALL_DIR)/usr/include/libxml2 $(1)/usr/include/ $(INSTALL_DIR) $(1)/usr/lib $(CP) $(PKG_INSTALL_DIR)/usr/lib/libxml2.{a,so*} $(1)/usr/lib/ $(INSTALL_DIR) $(1)/usr/lib/pkgconfig $(CP) $(PKG_INSTALL_DIR)/usr/lib/pkgconfig/libxml-2.0.pc $(1)/usr/lib/pkgconfig/ endef define Package/libxml2/install $(INSTALL_DIR) $(1)/usr/lib $(CP) $(PKG_INSTALL_DIR)/usr/lib/libxml2.so.* $(1)/usr/lib/ endef $(eval $(call BuildPackage,libxml2)) ``` #### 3. 配置和编译 ```bash # 3.1 更新 feeds ./scripts/feeds update -a ./scripts/feeds install libxml2 # 3.2 配置编译选项 make menuconfig # 在菜单中导航到:Libraries -> libxml2 并选择 <*> # 3.3 开始编译 make package/libxml2/compile V=s ``` #### 4. 获取编译结果 ```bash # 查找生成的 ipk 包 find bin -name "libxml2*.ipk" # 示例输出路径: # bin/packages/x86_64/base/libxml2_2.12.6-1_x86_64.ipk ``` #### 5. 安装到 OpenWrt 设备 ```bash # 将 ipk 文件复制到设备 scp bin/packages/*/base/libxml2*.ipk root@openwrt-device:/tmp # SSH 登录设备安装 opkg install /tmp/libxml2*.ipk ``` #### 常见问题解决 1. **缺少 zlib 依赖**: ```bash # 安装 zlib 开发包 ./scripts/feeds install zlib make menuconfig # 启用 zlib make package/zlib/compile ``` 2. **启用 Python 绑定**: 修改 `Makefile`: ```makefile DEPENDS:=+zlib +libpthread +python3 CONFIGURE_ARGS += --with-python=$(STAGING_DIR)/host/bin/python3 ``` 3. **静态库编译**: 在 `Makefile` 中添加: ```makefile CONFIGURE_ARGS += --enable-static=yes ``` #### 编译流程图 ``` [准备 SDK] → [设置环境] → [获取源码] ↓ ↓ [创建 Makefile] → [配置选项] → [开始编译] ↓ ↓ [生成 IPK] → [设备安装] ← [解决依赖] ``` ### 关键点解释 1. **环境变量设置**:`STAGING_DIR` 和 `PATH` 确保使用 OpenWrt 的工具链 2. **Makefile 结构**: - `PKG_*` 变量定义包信息 - `CONFIGURE_ARGS` 传递交叉编译参数 - `Build/InstallDev` 处理开发文件 3. **依赖管理**:`DEPENDS` 字段声明运行时依赖 4. **输出格式**:OpenWrt 使用 `.ipk` 包格式进行分发
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值