openwrt 第一个程序--helloword

openwrt 第一个程序–helloword

第一个程序

  • openwrt 第一个程序--helloword
    • 1.在package 下新建文件夹helloword
    • 2.src 文件夹下面创建helloword.c 和 Makefile
    • 3.回到编译根目录,`make menuconfig` 选中新增的helloword 模块:
    • 4.在openwrt 系统上,拷贝刚生成的.ipk
    • 如果遇到编译出现缺少libc.so.6库,参考以下文档:

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 
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 模块:

openwrt 第一个程序--helloword_第1张图片
编译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

这样子就解决了~

你可能感兴趣的:(openwrt,linux,c,openwrt)