选择皮肤
jiang
2023-09-21 12:48:01
jiang\n楼主

openwrt 一个比较重要的特点就是它采用 ipk 包的形式安装软件。有点像是 windows 下面的安装包一样,用户只需用简单的命令就可以将 ipk 安装包安装到 openwrt 系统中,非常方便。下面来介绍如何制作编译一个简单的安装包。


jiang
2023-09-21 12:52:15
jiang\n楼主

源码

切换到 openwrt 源码根目录,然后执行下列命令:
mkdir hello_world //创建一个名为“hello_world”的文件夹,用于放置安装包源码。
cd hello_world //进入“hello_world”目录
mkdir src //新建一个名为“src”的目录用于放置源码
mkdir root //新建一个名为“root ”的目录用于放置文件
vi src/hello_world.c //在 src 目录下新建一个名为 hello_world.c 文件
输入 hello_world.c 中的内容:
#include <stdio.h>
int main(char argc, char *argv[])
{
    int i = 1;
    while(1){
        //1~10 循环
        printf("Hello world!!!%d\n",i); //打印内容
		if (i < 10){
			i++;
		}else{
			i = 1;
		}
			sleep(1);// 一秒钟打印一次
	}
	return 0;
}
vi src/Makefile,//在 src 目录下新建一个 Makefile,输入下面内容:
all: hello_world
hello_world: hello_world.o
	$(CC) $(LDFLAGS) hello_world.o -o hello_world
helloworld.o: hello_world.c
	$(CC) $(CFLAGS) -c hello_world.c
clean:
	rm *.o hello_world
vi Makefile,输入下面内容:
include $(TOPDIR)/rules.mk
 
PKG_NAME:=hello_world
PKG_VERSION:=1.0
PKG_BUILD_DIR:= $(BUILD_DIR)/$(PKG_NAME)
 
include $(INCLUDE_DIR)/package.mk
 
define Package/hello_world
	SECTION:=base
	CATEGORY:=Utilities
	TITLE:=Hello world -prints a hello world message
endef
 
define Package/hello_world/description
	If you can't figure out what this program does, you're probably
	brain-dead and need immediate medical attention.
endef
 
define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
 
define Package/hello_world/install
	$(INSTALL_DIR) $(1)/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/hello_world $(1)/bin/
	$(CP) $(PKG_BUILD_DIR)/root/* $(1)/
endef
 
$(eval $(call BuildPackage,hello_world))
jiang
2023-09-21 12:54:20
jiang\n楼主

配置

make menuconfig,选择我们已经加进去的安装包
  Utilities  --->
    <M> hello_world.................... Hello world -prints a hello world message
jiang
2023-09-21 12:54:49
jiang\n楼主

编译

make V=99
make package/hello_world/compile V=99
文件:./bin/packages/hello_world_1.0_arm.ipk