并不是每个开发者都需要修改源码的,使用 SDK 也可以更快速简单的开发应用程序,SDK 可
以从官网下载,但尝试下了几个版本都没有编译成功可运行的应用程序,最后使用能成功编
译的源码编译出了 SDK 包,再用这个 SDK 包编译应用程序,终于成功。
以 HelloWorld 为例说明开发应用程序流程,
编译环境:Fedora 14
1、编译 SDK 包
源码下执行 make menuconfig 时,选择 SDK
2、源码编译完后在 bin/x86 目录下,就看到编译好的 SDK 包了
将 SDK 包拷出解压,久可以单独使用了
2、编写应用程序
(1)新建 hello_world 文件夹,在 hello_world 文件夹下新建 makefile 文件 makefile 文件的
内容如下:
#
# Top level makefile for example application
#
include $(TOPDIR)/rules.mk
PKG_NAME:=hello_world
PKG_VERSION:=1.0.0
PKG_RELEASE:=1
include $(INCLUDE_DIR)/package.mk
define Package/hello_world
SECTION:=utils
CATEGORY:=Utilities
TITLE:=hello_world -- prints hello_world 1 to 99
endef
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)
endef
define Build/Configure
endef
TARGET_CFLAGS += $(FPIC)
define Package/hello_world/install
$(INSTALL_DIR) $(1)/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/hello_world $(1)/bin/
endef
$(eval $(call BuildPackage,hello_world)) #(此处不能有Tab)
(2)在 hello_world 文件夹下新建 src 文件夹,src 文件夹下新建 hello_world.c 和 makefile 文
件,hello_world 文件内容如下:
#include
int main()
{
printf("This is a first hello world app!");
return 0;
}
makefile 内容如下:
# build executable on typing make
all: hello_world
%.o: %.c
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -I. -Iinclude -o $@ $^
hello_world: hello_world.o
$(CC) -o $@ $^ -L.
clean:
rm -f *.o hello_world
注意:最好是直接从其他文件夹下拷贝一个已有的文件。再修改,直接创建的文件可能因为
文件类型等原因不能被识别;makefile 文件的每个可执行命令(不是赋值之类的)一定要以
Tab 开始,否则会出现识别不了字符而编译失败
(3 ) 执 行 make
hello_world 的 ipk 文件
V=s 进 行 编 译 , 编 译 完 后 在 bin/x86/packages/base 目 录 下 就 生 成 了
3、运行
使用 scp 命令将 ipk 文件拷贝到 openWrt 虚拟机上
使用 opkg install 命令安装
输入 hello_world 运行
hello_world.tar.gz
OpenWrt-SDK-x86-for-redhat-i686-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2