1、在package下创建自己的工作目录user-apps
cd xxx/openwrt/package
mkdir user-apps
cd user-apps
mkdir helloworld
cd helloworld
mkdir src
2、编写代码(放到src目录)
#include
int main(void)
{
printf(“hello world\n”);
return 0;
}
3、编写编译程序的Makefile(放到src目录)
TARGET := helloworld
#所有源文件
CSRCS = $(wildcard *.c)
CPPSRCS = $(wildcard *.cpp)
COBJS := $(CSRCS:.c=.o)
CPPOBJS := $(CPPSRCS:.cpp=.o)
CFALGS := -Wall -g -O2 -Werror
all:$(COBJS) $(CPPOBJS)
$(CC) $(LDFLAGS) -o $(TARGET) $(COBJS) $(CPPOBJS) $(LIBS) $(CFLAGS)
$(COBJS) : %.o : %.c
$(CC) -c $< -o $@
$(CPPOBJS) : %.o : %.cpp
$(CPP) -c $< -o $@
clean:
rm -rf $(TARGET)
rm -rf *.o
4、编写模块编译Makefile
#
# Copyright (C) 2007-2010 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
include $(TOPDIR)/rules.mk
PKG_NAME:=helloworld
PKG_RELEASE:=1
PKG_BUILD_DIR:=$(BUILD_DIR)/user-apps/$(PKG_NAME)$(if $(PKG_VERSION),-$(PKG_VERSION))
# 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
include $(INCLUDE_DIR)/package.mk
define Package/$(PKG_NAME)
SECTION:=utils
CATEGORY:=Utilities
TITLE:=$(PKG_NAME)
DEPENDS:=+libpthread +libpcap
endef
define Package/$(PKG_NAME)/description
$(PKG_NAME)
endef
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
#---- 这个决定了最后通过opkg命令,将此ipk安装到设备中的哪个目录?
define Package/$(PKG_NAME)/install
$(INSTALL_DIR) $(1)/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME) $(1)/bin/
endef
$(eval $(call BuildPackage,$(PKG_NAME)))
5、返回openwrt的根目录,编写
5.1 make menuconfig
选中Utilities ---> helloworld.................................................... helloworld
5.2 单独编译模块
make package/user-apps/helloworld/compile
在bin/ramips/packages/base/目录下会找到对应的bin,拷贝到nfs共享目录下.
在开发板已安装模块
opkg install helloworld_1_ramips_24kec.ipk --nodeps
查看已经安装上的模块
opkg list-installed
卸载模块
opkg remove helloworld
./helloworld执行
5.3 跟内核一起编译,重新烧录固件,在/bin目录下存在helloworld,./helloworld执行。