openwrt--创建一个可执行程序

切换到openwrt/package/utils文件夹下,创建一个文件夹hello_world,文件夹内容如下。

    moqi@ubuntu:~/sources/openwrt/package/utils$ tree hello_world/
    hello_world/
    ├── Makefile
    ├── files
    │   ├── helloworld.conf
    │   └── helloworld.init
    └── src
        ├── Makefile
        ├── include
        │   └── main.h
        └── main
            └── main.c
  • hello_world/Makefile:用于告诉openwrt源码内容与编译需要的库、安装到文件系统中的位置。
  • hello_world/files/helloworld.init:开机启动脚本。
  • hello_world/files/helloworld.conf:uci配置文件。
  • hello_world/src/:这个文件夹是我自己创建的一个源码工程,可以用Makefile编译即可,我的工程下的Makefile比较复杂,主要是为了可以编译多个C文件。

文件内容

根文件夹下的Makefile

    include $(TOPDIR)/rules.mk

    PKG_NAME:=hello_world
    PKG_VERSION:=1.0

    PKG_BUILD_DIR:= $(BUILD_DIR)/$(PKG_NAME)
    PKG_CONFIG_DEPENDS:= 

    include $(INCLUDE_DIR)/package.mk

    define Package/hello_world
        SECTION:=base
        CATEGORY:=Utilities
        TITLE:=Hello world -prints a hello world message
        DEPENDS:=+libmosquitto
    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 Package/hello_world/install
        $(INSTALL_DIR) $(1)/etc/config
        $(INSTALL_CONF) ./files/helloworld.conf $(1)/etc/config/helloworld
        $(INSTALL_DIR) $(1)/etc/init.d
        $(INSTALL_BIN) ./files/helloworld.init $(1)/etc/init.d/helloworld
        $(INSTALL_DIR) $(1)/usr/sbin/
        $(INSTALL_BIN) $(PKG_BUILD_DIR)/hello_world $(1)/usr/sbin/
    endef

    define Build/Prepare
        mkdir -p $(PKG_BUILD_DIR)
        $(CP) ./src/* $(PKG_BUILD_DIR)/
    endef

    $(eval $(call BuildPackage,hello_world))

第二层文件夹下的Makefile

    #################################### hello_world/Makefile ####################################
    CC := mipsel-openwrt-linux-gcc
    # 使用的交叉编译链
    TARGET := hello_world
    # 编译出的目标文件
    SRC_DIR = main
    # 源码存放目录,编译时会递归查询该文件夹下三层文件夹的所有c文件
    BUILD_DIR = Build
    # 中间文件存放文件夹,编译时如果不存在会创建该文件夹
    INCLUDE_DIR = include
    # 头文件存放路径
    OPENWRT_USR_DIR = /home/moqi/sources/openwrt/staging_dir/target-mipsel_24kc_musl/usr
    # openwrt源码编译出来的交叉编译库
    LIB_PARAMETER = -lpthread 
    # 编译时使用的动态链接库

    OBJ_DIR = $(BUILD_DIR)/obj
    DEPS_DIR  = $(BUILD_DIR)/deps
    INC_DIR = -I./$(INCLUDE_DIR) \
            -I$(OPENWRT_USR_DIR)/include
    DIRS := $(shell find $(SRC_DIR) -maxdepth 3 -type d)
    # 这里递归遍历3级子目录
    VPATH = $(DIRS)
    # 将每个子目录添加到搜索路径

    SOURCES   = $(foreach dir, $(DIRS), $(wildcard $(dir)/*.c))
    OBJS   = $(addprefix $(OBJ_DIR)/,$(patsubst %.c,%.o,$(notdir $(SOURCES))))
    DEPS  = $(addprefix $(DEPS_DIR)/, $(patsubst %.c,%.d,$(notdir $(SOURCES))))
    # 查找src_dir下面包含子目录的所有c文件

    CC_FLAGS := $(INC_DIR) $(LIB_PARAMETER)
    LNK_FLAGS := -L$(OPENWRT_USR_DIR)/lib

    all:$(OBJS)
        $(CC) $^ $(LNK_FLAGS) $(CC_FLAGS) -o $(TARGET)

    # 编译之前要创建OBJ目录,确保目录存在
    $(OBJ_DIR)/%.o:%.c
        if [ ! -d $(OBJ_DIR) ]; then mkdir -p $(OBJ_DIR); fi;\
        $(CC) -c $(CC_FLAGS) -o $@ $<

    # 编译之前要创建DEPS目录,确保目录存在
    # 前面加@表示隐藏命令的执行打印
    $(DEPS_DIR)/%.d:%.c
        @if [ ! -d $(DEPS_DIR) ]; then mkdir -p $(DEPS_DIR); fi;\
        set -e; rm -f $@;\
        $(CC) -MM $(CC_FLAGS) $< > $@.$$$$;\
        sed 's,\($*\)\.o[ :]*,$(OBJ_DIR)/\1.o $@ : ,g' < $@.$$$$ > $@;\
        rm -f $@.$$$$

    # 前面加-表示忽略错误
    -include $(DEPS)
    .PHONY : clean
    clean:
        rm -rf $(BUILD_DIR)/* $(TARGET)

C源代码

    /*################################### hello_world/src/main/main.c ####################################*/
    #include <stdio.h>
    #include <unistd.h>

    int main(int argc, char *argv[])
    {
        while (1)
        {
            printf ("Hello World\r\n");
            sleep (1);
        }
        return 0;
    }
    
    /*################################### hello_world/src/include/main.h ####################################*/
    #ifndef __MAIN_H__
    #define __MAIN_H__

    #if __DEBUG_LOG__
    #define DEBUG_LOG(format, ...)          printf (format, ##__VA_ARGS__)
    #else
    #define DEBUG_LOG(format, ...)
    #endif

    #endif
    /* __MAIN_H__ */

开机启动文件 helloworld.init

    #!/bin/sh /etc/rc.common
    # Basic init script for mosquitto
    # April 2012, OpenWrt.org
    # Provides support for the luci-app-mosquitto package, if installed

    START=90
    STOP=90
    USE_PROCD=1

    start_service() {
        echo "start hello wrold"
        return 0
    }

    stop_service() {
        echo "stop hello wrold"
        return 0
    }

    reload_service() {
        echo "reload hello wrold"
        return 0
    }

    shutdown() {
        echo "shutdown hello wrold"
        return 0
    }

    boot() {
        echo "shutdown hello wrold"
        return 0
    }

我创建的已经上传到gitee,需要的可以自行下载

    https://gitee.com/moqi-smile/linux-openwrt-project/tree/master/apply/hello_world

将软件加入固件

编译固件之前,需要先在menuconfig中打开以下选项。其中的Utilities是我们在Makefile中定义的。

    Utilities  --->
        <*> hello_world.................... Hello world -prints a hello world message
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值