OpenWrt开发入门实战(三)使用GUNmake

1. 创建Makefile

在上一章中我们是在包清单文件中定义如何编译helloopenwrt,以下是上一章中的包清单文件

# Package build instructions; invoke the target-specific compiler to first compile the source file, and then to link the file into the final executable
define Build/Compile
		$(TARGET_CC) $(TARGET_CFLAGS) -o $(PKG_BUILD_DIR)/helloopenwrt.o -c $(PKG_BUILD_DIR)/helloopenwrt.c
		$(TARGET_CC) $(TARGET_LDFLAGS) -o $(PKG_BUILD_DIR)/$1 $(PKG_BUILD_DIR)/helloopenwrt.o
endef

由于这是一个示例项目,只有一个源文件,编译命令比较简单。根据关注点分离原则,一个程序如何编译应该又要在源码中给出。

cd /home/buildbot/helloopenwrt
touch Makefile

将以下内容复制进入Makefile

include $(TOPDIR)/rules.mk

# Name, version and release number
# The name and version of your package are used to define the variable to point to the build directory of your package: $(PKG_BUILD_DIR)
PKG_NAME:=helloopenwrt
PKG_VERSION:=1.0
PKG_RELEASE:=1

# Source settings (i.e. where to find the source codes)
# This is a custom variable, used below
SOURCE_DIR:=/home/buildbot/helloopenwrt

include $(INCLUDE_DIR)/package.mk

# Package definition; instructs on how and where our package will appear in the overall configuration menu ('make menuconfig')
define Package/helloopenwrt
  SECTION:=examples
  CATEGORY:=Examples
  TITLE:=Hello, OpenWrt!
endef

# Package description; a more verbose description on what our package does
define Package/helloopenwrt/description
  A simple "Hello, OpenWrt!" -application.
endef

# Package preparation instructions; create the build directory and copy the source code. 
# The last command is necessary to ensure our preparation instructions remain compatible with the patching system.
define Build/Prepare
		mkdir -p $(PKG_BUILD_DIR)
		cp $(SOURCE_DIR)/* $(PKG_BUILD_DIR) -r
		$(Build/Patch)
endef

# Package build instructions; invoke the target-specific compiler to first compile the source file, and then to link the file into the final executable
define Build/Compile
		$(MAKE) -C $(PKG_BUILD_DIR) \
               CC="$(TARGET_CC)" \
           CFLAGS="$(TARGET_CFLAGS)" \
          LDFLAGS="$(TARGET_LDFLAGS)"
endef

# Package install instructions; create a directory inside the package to hold our executable, and then copy the executable we built previously into the folder
define Package/helloopenwrt/install
		$(INSTALL_DIR) $(1)/usr/bin
		$(INSTALL_BIN) $(PKG_BUILD_DIR)/helloopenwrt $(1)/usr/bin
endef

# This command is always the last, it uses the definitions and variables we give above in order to get the job done
$(eval $(call BuildPackage,helloopenwrt))

Makefile文件写好之后,我们可以输入以下命令测试一下是否有效

cd /home/buildbot/helloopenwrt
make clean
make

如果一切正常,当前目录下会有一个文件helloopenwrt,可以执行./helloopenwrt

2. 修改包清单文件

我们编写完Makefile后,在包清单文件里面还是原有的编译方式,这时我们需要修改包清单文件/home/buildbot/mypackages/examples/helloopenwrt/Makefile。只需要修改Build/Compile部分,修改内容如下

# Package build instructions; invoke the GNU make tool to build our package
define Build/Compile
		$(MAKE) -C $(PKG_BUILD_DIR) \
               CC="$(TARGET_CC)" \
           CFLAGS="$(TARGET_CFLAGS)" \
          LDFLAGS="$(TARGET_LDFLAGS)"
endef

3. 重新构建包

修改包清单文件后,我们就可以再次构建包了。

cd /home/buildbot/openwrt
make package/helloopenwrt/{clean,compile}

如果构建包时出现异常,可能需要更新feeds

cd /home/buildbot/openwrt
./scripts/feeds update mypackages
./scripts/feeds install -a -p mypackages
OpenWrt开发入门培训视频是指针对初学者开发OpenWrt固件的培训视频。OpenWrt是一种开源嵌入式操作系统,用于路由器等嵌入式设备。以下是关于这个培训视频的一些信息。 OpenWrt开发入门培训视频旨在通过系统的讲解帮助开发者了解OpenWrt的基本概念、架构和开发流程。视频内容包括以下几个方面: 1. 环境搭建:视频将引导观众如何在自己的计算机上搭建开发环境,包括安装必要的软件工具和配置编译环境等。 2. OpenWrt介绍:视频会介绍OpenWrt的发展历程、特点和功能,帮助观众了解OpenWrt的优势和适用场景。 3. 开发流程:视频会详细讲解在OpenWrt开发固件的常见流程,包括编译固件、配置网络和文件系统、添加自定义功能等。 4. 应用开发:视频会讲解如何在OpenWrt平台上开发应用程序,如编写脚本、添加软件包和驱动程序等。 5. 调试和故障排除:视频会演示常见的调试技巧和故障排除方法,帮助观众解决在开发过程中可能遇到的问题。 通过观看OpenWrt开发入门培训视频,初学者可以系统地了解OpenWrt开发的基本知识和技巧,从而能够在OpenWrt平台上进行软件和应用程序的开发。同时,观众还可以通过实际操作和案例分析更好地掌握OpenWrt的应用和调试方法,提高开发效率和质量。这些知识和技能对于从事嵌入式设备开发和网络安全等领域的开发者来说非常有用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值