OpenWrt:构建helloworld应用

Igor Openwrt评论27,483字数 3694阅读12分18秒阅读模式
SDK中其实是包含toolchain的,SDK就是将编译好的文件在打包到一个ipk中,便于安装。SDK与toolchain一样,可以自己编译也可以从网上下载。SDK生成ipk包时需要的只是一个makefile文件,makefile里写了所需下载的文件、生成规则、软件版本、类型等。makefile的写法可以到openwrt的官方wiki中找到。在package目录下创建项目目录:

$ mkdir helloworld
$ mkdir helloworld/src

helloworld/src目录下创建源码文件helloworld.c:

#include <stdio.h>

int main(int argc, char** argv)
{
    printf("hello world!\n");
    
    return 0;
}

helloworld/src目录下创建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

在helloworld目录下创建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))

Makefile语法可参考官网:http://wiki.openwrt.org/doc/devel/packages

编译:

$ make package/helloworld/compile -j1 V=s
$ ls bin/ramips/packages/base/helloworld_1_ramips_24kec.ipk 
bin/ramips/packages/base/helloworld_1_ramips_24kec.ipk

安装:

root@OpenWrt:/# opkg install helloworld_1_ramips_24kec.ipk
Installing helloworld (1) to root...
Configuring helloworld.

运行:

root@OpenWrt:/# helloworld
hello world!

参考文章

Openwrt之helloworld
openWRT自学---如何开发新的用户态模块-helloworld
用CMakeLists.txt替代makefile创建openwrt软件包

文章末尾固定信息

weinxin
我的微信
我的微信
一个码农、工程狮、集能量和智慧于一身的、DIY高手、小伙伴er很多的、80后奶爸。
 
Igor
  • 本文由 Igor 发表于 2020-08-0106:42:22
Openwrt

高性价比AC2600路由D-Link DIR-882

近年来,面对可连网终端设备的不断暴增以及国内宽带的提速升级,无线路由器迎来了新的更新换代潮。然而对于一般消费者来说,似乎还无法从产品外观上直接感受到路由器产品的迭代与演进。而《拆机实验室》是一档带您深...
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证