openwrt 第一個程序--helloword

openwrt 第一個程序–helloword

1.在package 下新建文件夾helloword

helloword
├── Makefile
└── src
    ├── helloworld.c
    └── Makefile

helloword 文件夾下的Makefile如下:
註釋部分# zzk 解決找不到libc.so.6庫問題

##############################################
# OpenWrt Makefile for HelloWorld program
#
#
# Most of the variables used here are defined in
# the include directives below. We just need to
# specify a basic description of the package,
# where to build our program, where to find
# the source files, and where to install the
# compiled program on the router.
#
# Be very careful of spacing in this file.
# Indents should be tabs, not spaces, and
# there should be no trailing whitespace in
# lines that are not commented.
#
##############################################
include $(TOPDIR)/rules.mk
# Name and release number of this package
PKG_NAME:=helloworld
PKG_RELEASE:=1
 
 
# 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
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
 
 
include $(INCLUDE_DIR)/package.mk
 
# Specify package information for this program.
# The variables defined here should be self explanatory.
# If you are running Kamikaze, delete the DESCRIPTION
# variable below and uncomment the Kamikaze define
# directive for the description below
DEPENDS:=+libc ### zzk 
define Package/helloworld
        SECTION:=utils
        CATEGORY:=Utilities
        TITLE:=helloworld -- prints a snarky message
        DEPENDS:=+libc ### zzk 
endef
 
 
# Uncomment portion below for Kamikaze and delete DESCRIPTION variable above
define Package/helloworld/description
        If you can't figure out what this program does, you're probably brain-dead and need immediate medical attention.
endef
 
# Specify what needs to be done to prepare for building the package.
# In our case, we need to copy the source files to the build directory.
# This is NOT the default.  The default uses the PKG_SOURCE_URL and the
# PKG_SOURCE which is not defined here to download the source from the web.
# In order to just build a simple program that we have just written, it is
# much easier to do it this way.
define Build/Prepare
        mkdir -p $(PKG_BUILD_DIR)
        $(CP) ./src/* $(PKG_BUILD_DIR)/
endef
# We do not need to define Build/Configure or Build/Compile directives
# The defaults are appropriate for compiling a simple program such as this one
# Specify where and how to install the program. Since we only have one file,
# the helloworld executable, install it by copying it to the /bin directory on
# the router. The $(1) variable represents the root directory on the router running
# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install
# directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the
# command to copy the binary file from its current location (in our case the build
# directory) to the install directory.
define Package/helloworld/install
        $(INSTALL_DIR) $(1)/bin
        $(CP) /lib/x86_64-linux-gnu/libc.so.6 $(1)/bin ### zzk 
        $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef
# This line executes the necessary commands to compile our program.
# The above define directives specify all the information needed, but this
# line calls BuildPackage which in turn actually uses this information to
# build a package.
$(eval $(call BuildPackage,helloworld))

2.src 文件夾下面創建helloword.c 和 Makefile

helloword.c代碼如下:

#include <stdio.h>
int main()
{
        printf("hello world ! \t\n");
        return 0;
}

Makefile

# build helloworld executable when user executes "make"
helloworld: helloworld.o
	$(CC) $(LDFLAGS) helloworld.o -o helloworld
helloworld.o: helloworld.c
	$(CC) $(CFLAGS) -c helloworld.c
# remove object files and executable when user executes "make clean"
clean:
	rm *.o helloworld

3.回到編譯根目錄,make menuconfig 選中新增的helloword 模塊:

在這裏插入圖片描述
編譯helloword模塊
make package/helloworld/compile V=s

在這裏插入圖片描述

4.在openwrt 系統上,拷貝剛生成的.ipk

scp [email protected]://opt/wifi6/qsdk/bin/ipq/packages/base/helloworld_1_ipq.ipk /tmp/

運行opkg 安裝
opkg install helloworld_1_ipq.ipk
在這裏插入圖片描述
執行helloword,可以看到helloword被運行:
在這裏插入圖片描述
end

如果遇到編譯出現缺少libc.so.6庫,參考以下文檔:

原連接:
這個是在我在Openwrt的SDK下編譯模塊的時候碰到的問題。
缺少類庫,然後其實我發現我的類庫在系統裏是存在的:

locate libc.so.6

結果:

/lib/i386-linux-gnu/libc.so.6
/lib64/libc.so.6

看~ 明顯存在,我用的應該是上面的那個類庫,然後我弄了一晚上沒弄好,今天所有工作做完終於弄好了。我做的工作包括這些:
第一步:
把/lib/i386-linux-gnu/libc.so.6這個文件拷貝到/home/user/attitude/staging_dir/target-mips_r2_uClibc-0.9.33.2/usr/lib這裏。其中attitude是我的配置的Openwrt的編譯環境。

cp /lib/i386-linux-gnu/libc.so.6 /home/user/attitude/staging_dir/target-mips_r2_uClibc-0.9.33.2/usr/lib

第二步:
在你寫的代碼文件夾下(我寫的是個helloworld)下的Makefile裏
增加:DEPENDS:=+libc
就是在

define Package/helloworld  
        SECTION:=utils  
        CATEGORY:=Utilities  
        TITLE:=Helloworld -- prints a snarky message  
        DEPENDS:=+libc
endef

這個define下面。

第三步:也是在Makefile裏增加:

define Package/helloworld/install  
        $(INSTALL_DIR) $(1)/bin  
        $(CP) /lib/i386-linux-gnu/libc.so.6 $(1)/bin
        $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/  
endef

這裏多了一行$(CP)的代碼。
最後一步:
/home/user/attitude/staging_dir/target-mips_r2_uClibc-0.9.33.2/pkginfo下的libc.provides文件裏增加兩行:

/lib/i386-linux-gnu/libc.so.6
libc.so.6

這樣子就解決了~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章