OpenWrt Code Framework Analysis

Source: Internet
Author: User

This time talk about the structure of OpenWrt.

1. There are several important directories in the code: package, Target, build_root, bin, dl ....
---build_dir/host directory is a temporary directory when the tool chain is established
---build_dir/toolchain- is the directory of the tool chain that corresponds to the hardware
---staging_dir/toolchain-
is where the tool chain is installed
---target/linux/directory is the relevant code for each platform (arch)
---target/linux//config-3.10 file is the config file.
---The DL directory is ' download ' abbreviation, in the pre-compilation, the need to download packets from the network will be placed in this directory, one of the features of these packages is automatically installed in the compiled firmware, that is, when we make menuconfig, the firmware configuration of some software packages. If we need to change these source packages, we just need to package the changed source package into the same name in this directory, and then start compiling. At compile time, the package is extracted to the Build_dir directory.
---and in the build_dir/directory for decompression, compilation and patching, and so on.
The---Package directory contains all the compiled packages that we set up in the configuration file. By default, there will be a default selection of packages. IPK is everything in openwrt, we can use

$ ./scripts/feeds update来对软件包进行更新.$ ./scripts/feeds search nmap 查找软件包‘nmap‘ Search results in feed ’packages’: nmap       Network exploration and/or security auditing utility $ ./scripts/feeds install nmap 安装‘nmap‘这个软件$ make package/symlinks  //估计意思是更新软件源之类的

A lot of bin files are generated---the bin directory and are differentiated according to different platforms. In addition Bin//package directory, there are many ipk suffix files, are the package directory under the source code in the Build_dir directory compiled after the result of the build.

2. Create your own packages
For your new package, which does not need to be installed with the firmware, in other words, it can be used as an optional bundle. We can use our SDK environment to compile it separately and build a IPK package after compiling it. Then install the software using the Opkg install XXX.IPK.

Here's how to compile a Helloword package.
(1) First, write the HelloWorld program
Writing HELLOWORLD.C

/***************** Helloworld.c* The most simplistic C program ever written.* An epileptic monkey on crack could write this code.*****************/#include <stdio.h>#include <unistd.h>int main(void){     printf("Hell! O‘ world, why won‘t my code compile?\n\n");     return 0;}

Writing makefile Files

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

In the directory of these two files, execute make should be able to generate HelloWorld executable files. After executing the HelloWorld, you can print out the "hell! O ' World, why won ' t my Code compile? ". This step, the main guarantee that our source program can be compiled properly.

Below we will transplant it to OpenWrt.
(2) Unzip the OPENWRT-SDK-BRCM47XX-FOR-LINUX-X86_64-GCC-4.3.3+CS_UCLIBC-0.9.30.1.TAR.BZ2
TAR–XVF openwrt-sdk-brcm47xx-for-linux-x86_64-gcc-4.3.3+cs_uclibc-0.9.30.1.tar.bz2
(3) Enter the SDK

cd OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1

You can see that the directory structure is basically the same as the directory structure of our previous source, the packages that need to be compiled are placed in the package directory
(4) Create the HelloWorld directory under the package directory

cd packagemkdir helloworldcd helloworld

(5) Create src directory, copy helloworld file

mkdir srccp /home/wrt/test/helloworld.c srccp /home/wrt/test/Makefile src

(6) Create the makefile file in the HelloWorld directory
This makefile file is read to OpenWrt, and the makefile file previously written is for HelloWorld to compile its read. Two makefile are not in the same level directory.

touch Makefilevim Makefile

The contents of the Makefile file template are as follows:

############################################### OpenWrt Makefile for HelloWorld program### Most of the variables used her E is 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.## is very careful of spacing in this file.# indents should is T ABS, not spaces, and# there should is no trailing whitespace in# lines that is not commented.############################ ################## #include $ (topdir)/rules.mk# Name and release number of this packagepkg_name:=helloworldpkg_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 directorypkg_build _dir: = $ (Build_dir)/$ (pkg_name) include $ (include_dir)/package.mk# Specify package information for this program.# the Var Iables defined here should be sElf explanatory.# If You is running kamikaze, delete the description# variable below and uncomment the kamikaze define# D Irective for the description belowdefine package/helloworld section:=utils category:=utilities TITLE: =helloworld--Prints a snarky messageendef# uncomment portion below for kamikaze and delete DESCRIPTION variable abovedef INE package/helloworld/description If You can ' t figure out what the This program does, you ' re probably brain-dea D and need immediate medical attention.endef# specify what needs to is done to prepare for building the package.# in our C  ASE, we need to copy the source files to the build directory.# the.  The default uses the Pkg_source_url and the# Pkg_source which are not defined here to download the SOURCE from the web.# in         Order to just build a simple program that we had just written, it is# much easier to does it this way.define build/prepare Mkdir-p $ (Pkg_build_dir) $ (CP)./src/* $ (Pkg_build_dir)/endef# We don't need to define build/configure or Build/compile directives# the defaults is appropriate F or compiling a simple program such as this one# specify where and what to install the program. Since we only has 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 their current location (the build# di Rectory) to the install Directory.define Package/helloworld/install $ (install_dir) $ (1)/bin $ (install_bin) $ (Pkg_build_dir)/helloworld $ (1)/bin/endef# This line executes the necessary commands to compile our program.# the above de Fine 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)) 

(7) Return to the root directory of the SDK
Perform make to compile
The compilation process is done in the Build_dir directory
The compilation results will be placed in the Bin/[yourtarget]/package directory HELLOWORLD_1_BCM47XX.IPK
(8) Upload HELLOWORLD_1_BCM47XX.IPK
Upload HELLOWORLD_1_BCM47XX.IPK to Router
Execute # opkg Install HELLOWORLD_1_BCM47XX.IPK
Then enter Hello and press the TAB key to discover that there are already HelloWorld executable commands in OpenWrt.
Execute the HelloWorld command to see the effect of the program.

Hell! O‘ world, why won‘t my code compile?

This article by http://www.wifidog.pro/2015/08/17/openwrt-%E6%A1%86%E6%9E%B6.html compilation, reprint please indicate the source

OpenWrt Code Framework Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.