OpenWRT编译自定义应用的办法

来源:互联网 发布:2016美国大选结果数据 编辑:程序博客网 时间:2024/05/22 14:21

OpenWRT编译成功之后,相应编译得到的文件会被放在编译的根目录下的bin/{TARGET}/下面。比如说我编译完之后的相应的文件都可以在bin/ar71xx/的文件夹下面找到,其中的packages文件夹里面包含了我们再配置文件里设定的所有编译好的软件包,这些软件包不一定都被编译进了固件当中,只有那些在配置文件中(也就是在make menuconfig的时候)一Ctrl+Y选中的软件包才会被编译进固件当中,一Ctrl+M选中的软件包如果需要的话需要单独再在openwrt中进行安装。

在编译的根目录下会有一个dl的目录,这个目录其实就是”download“的缩写,在编译的前期,需要熊网络下载的数据包都会放在这个目录下,这些软件包的一个特点就是会自动安装在所编译的固件中。如果我们需要膝盖源码包,只需要将更改好的源码包打包成相同的名字放在这个目录下,然后开始编译即可。编译时,会将软件包解压到build_dir目录下。也可以自己在dl里面创建自己的软件包,然后更改相关的配置文件,让openwrt可以识别这个文件包。

新建和编译自己的packages:对于自己新建的package,在不需要随固件一起安装时,也就是这个package是作为一个可选软件包的,可以利用SDK环境来单独编译,编译完成之后会生成一个ipk包,利用opkg installxxx.ipk来安装这个软件。

下面以建立一个helloworld为例(相关的原始例子说明可以在openwrt的官网上找到):

1.建立源码目录:

cd package
mkdir Helloworld ; cd Helloworld
mkdir src
touch src/Makefile   /* Helloworld 编译Makefile */
touch ./Makefile /*建立顶层Makefile,这个Makefile文件是OpenWRT读的*/

(1)首先,编写helloworld程序
/****************
* Helloworld.c
* The most simplistic C program ever written.
* An epileptic monkey on crack could write this code.
*****************/
#include <stdio.h>
#include <unistd.h>
int main(void)
{
     printf("Hell! O' world, why won't my code compile?\n\n");
     return 0;
}

编写Makefile文件
# build helloworld executable when user executes "make"
helloworld: helloworld.o
        $(CC) $(LDFLAGS) helloworld.o -o helloworld
helloworld.o: helloworld.c
        $(CC) $(CFLAGS) -c helloworld.c
# remove object files and executable when user executes "make clean"
clean:
        rm *.o helloworld
(2)在helloworld目录下创建Makefile文件
这个Makefile文件是给OpenWRT读的,而之前写的那个Makefile文件是针对helloworld给编译其读的。
两个Makefile不在同一层目录下。
vim Makefile
Makefile文件模板内容如下:
##############################################
# OpenWrt Makefile for helloworld program
#
#
# Most of the variables used here are defined in
# the include directives below. We just need to
# specify a basic description of the package,
# where to build our program, where to find
# the source files, and where to install the
# compiled program on the router.
#
# Be very careful of spacing in this file.
# Indents should be tabs, not spaces, and
# there should be no trailing whitespace in
# lines that are not commented.
#
##############################################
include $(TOPDIR)/rules.mk
# Name and release number of this package
PKG_NAME:=helloworld
PKG_RELEASE:=1

# This specifies the directory where we're going to build the program. 
# The root 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
 
# Specify package information for this program.
# The variables defined here should be self explanatory.
# If you are running Kamikaze, delete the DESCRIPTION
# variable below and uncomment the Kamikaze define
# directive for the description below
define Package/helloworld
        SECTION:=utils
        CATEGORY:=Utilities
        TITLE:=Helloworld -- prints a snarky message
endef

# Uncomment portion below for Kamikaze and delete DESCRIPTION variable above
define Package/helloworld/description
        If you can't figure out what this program does, you're probably
        brain-dead and need immediate medical attention.
endef
 
# Specify what needs to be done to prepare for building the package.
# In our case, we need to copy the source files to the build directory.
# This is NOT the default.  The default uses the PKG_SOURCE_URL and the
# PKG_SOURCE which is not defined here to download the source from the web.
# In order to just build a simple program that we have just written, it is
# much easier to do it this way.
define Build/Prepare
        mkdir -p $(PKG_BUILD_DIR)
        $(CP) ./src/* $(PKG_BUILD_DIR)/
endef

# We do not need to define Build/Configure or Build/Compile directives
# The defaults are appropriate for compiling a simple program such as this one

# Specify where and how to install the program. Since we only have one file,
# the helloworld executable, install it by copying it to the /bin directory on
# the router. 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
# command to copy the binary file from its current location (in our case the build
# directory) to the install directory.
define Package/helloworld/install
        $(INSTALL_DIR) $(1)/bin
        $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef

# This line executes the necessary commands to compile our program.
# The above define directives specify all the information needed, but this
# line calls BuildPackage which in turn actually uses this information to
# build a package.
$(eval $(call BuildPackage,helloworld))

返回目录最顶层,执行make menuconfig,在utilitys中选中刚刚添加的模块名(这里是根据sections中定义的),保存.config.然后再执行make。

我这里再执行这一步时没有看到相应的自己想要添加的组件,想来应该是makefile文件哪里没有写好,选择利用单独编译模块的方法:

make package/helloworld/compile

make package/hello/install

最后再执行make命令,也可以编译生成相应的helloworld*.ipk文件到bin/{TARGET}/packages下面,然后将这个ipk文件拷贝到路由器中再利用opkg命令进行安装。

0 0
原创粉丝点击