如何在OpenWRT环境下做开发

\u5982\u4f55\u5728OpenWRT\u73af\u5883\u4e0b\u505a\u5f00\u53d1

\u3000\u30001\u3001\u642d\u5efa\u5f00\u53d1\u73af\u5883 \u9996\u5148\uff0c\u5728\u6267\u884cmake menuconfig\u540e\uff0c\u4f1a\u51fa\u73b0\u4e0b\u56fe\uff1a


\u3000\u3000\u5176\u4e2d\uff0c\u56fe\u4e2d\u7ea2\u6846\u90e8\u5206\u662f\u6211\u5b9a\u5236\u8def\u7531\u5668\u7684\u7cfb\u7edf\u7248\u672c\uff0c\u5927\u5bb6\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u8def\u7531\u5668\u8fdb\u884c\u4e0d\u540c\u7684\u9009\u62e9\uff1b\u7eff\u6846\u90e8\u5206\u8868\u793a\u6211\u4eec\u9700\u8981\u7f16\u8bd1\u4e00\u4e2aSDK\u5f00\u53d1\u73af\u5883\uff08\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6b64\u9879\u672a\u52fe\u9009\uff09\u3002
\u3000\u3000\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\u9700\u8981\u901a\u8fc7\u5b98\u7f51\u4e0b\u8f7d\u5f88\u591a\u76f8\u5173\u7684\u8f6f\u4ef6\u5305\uff0c\u6240\u4ee5\u5fc5\u987b\u4fdd\u8bc1\u80fd\u591f\u987a\u5229\u8fde\u4e0a\u5916\u7f51\u3002\u7531\u4e8e\u4e0b\u8f7d\u901f\u5ea6\u7684\u9650\u5236\uff0c\u7f16\u8bd1\u8fc7\u7a0b\u5927\u6982\u9700\u8981\u6570\u5c0f\u65f6\u3002\u7f16\u8bd1\u7ed3\u675f\u540e\uff0c\u6240\u6709\u7684\u4ea7\u54c1\u90fd\u4f1a\u653e\u5728\u7f16\u8bd1\u6839\u76ee\u5f55\u4e0b\u7684bin/yourtarget/. \u4f8b\u5982:\u6211\u6240\u7f16\u8bd1\u7684\u4ea7\u7269\u90fd\u653e\u5728./bin/brcm47xx/\u4e0b\uff0c\u5176\u4e2d\u6587\u4ef6\u4e3b\u8981\u6709\u51e0\u7c7b\uff1a
\u3000\u3000\uff081\uff09.bin/.trx \u6587\u4ef6: \u8fd9\u4e9b\u90fd\u662f\u5728\u6211\u4eec\u6240\u9009\u7684target-system\u7684\u7c7b\u522b\u4e4b\u4e0b\uff0c\u9488\u5bf9\u4e0d\u540c\u8def\u7531\u5668\u578b\u53f7\u3001\u7248\u672c\u7f16\u8bd1\u7684\u8def\u7531\u5668\u56fa\u4ef6\u3002\u8fd9\u4e9b\u4e0d\u540c\u8def\u7531\u5668\u7684\u578b\u53f7\u548c\u7248\u672c\u662fopenwrt\u9884\u5148\u8bbe\u7f6e\u597d\u7684\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u66f4\u6539\u3002\u81f3\u4e8e.bin\u548c.trx\u7684\u533a\u522b\uff0c\u4e00\u79cd\u8bf4\u6cd5\u662f\uff0c\u7b2c\u4e00\u6b21\u5237\u8def\u7531\u5668\u7684\u65f6\u5019\uff0c\u9700\u8981\u7528.bin\u6587\u4ef6\uff0c\u5982\u679c\u9700\u8981\u518d\u5347\u7ea7\uff0c\u5219\u4e0d\u80fd\u518d\u4f7f\u7528.bin\u6587\u4ef6\uff0c\u800c\u9700\u8981\u7528.trx\u6587\u4ef6\u3002\u539f\u56e0\u662f\uff0c.bin\u662f\u5c06\u8def\u7531\u5668\u7684\u76f8\u5173\u914d\u7f6e\u4fe1\u606f\u548c.trx\u5c01\u88c5\u5728\u4e00\u8d77\u800c\u751f\u6210\u7684\u5c01\u5305\uff0c\u4e5f\u5c31\u662f\u8bf4\u662f\u5305\u542b\u8def\u7531\u5668\u7248\u672c\u4fe1\u606f\u7684.trx\u3002\u5728\u7b2c\u4e00\u6b21\u5237\u56fa\u4ef6\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u63d0\u4f9b\u8fd9\u6837\u7684\u4fe1\u606f\uff0c\u800c\u5728\u540e\u7eed\u5347\u7ea7\u65f6\uff0c\u5219\u4e0d\u518d\u9700\u8981\uff0c\u7528.trx\u6587\u4ef6\u5373\u53ef\u3002
\u3000\u3000\uff082\uff09packages\u6587\u4ef6\u5939: \u91cc\u9762\u5305\u542b\u4e86\u6211\u4eec\u5728\u914d\u7f6e\u6587\u4ef6\u91cc\u8bbe\u5b9a\u7684\u6240\u6709\u7f16\u8bd1\u597d\u7684\u8f6f\u4ef6\u5305\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4f1a\u6709\u9ed8\u8ba4\u9009\u62e9\u7684\u8f6f\u4ef6\u5305\u3002
\u3000\u3000\uff083\uff09OpenWrt-SDK.**.tar.bz2: \u8fd9\u4e2a\u4e5f\u5c31\u662f\u6211\u4eec\u5b9a\u5236\u7f16\u8bd1\u597d\u7684OpenWRT SDK\u73af\u5883\u3002\u6211\u4eec\u5c06\u7528\u8fd9\u4e2a\u6765\u8fdb\u884cOpenWrt\u8f6f\u4ef6\u5305\u7684\u5f00\u53d1\u3002\u4f8b\u5982\uff0c\u6211\u6240\u7f16\u8bd1\u597d\u7684SDK\u73af\u5883\u5305\u4e3a\uff1a/bin/brcm47xx/OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2\u53ef\u4ee5\u4ece\u540d\u79f0\u4e0a\u770b\u51fa\uff0ctarget system\u662fbrcm47xx\uff0chost system\u662fLinux-x86_64\uff0c\u4f7f\u7528\u7684\u7f16\u8bd1\u5de5\u5177\u4ee5\u53ca\u5e93\u662f4.3.3+cs_uClibc-0.9.30.1\u3002
\u3000\u3000\uff084\uff09md5sums \u6587\u4ef6: \u8fd9\u4e2a\u6587\u4ef6\u8bb0\u5f55\u4e86\u6240\u6709\u6211\u4eec\u7f16\u8bd1\u597d\u7684\u6587\u4ef6\u7684MD5\u503c\uff0c\u6765\u4fdd\u8bc1\u6587\u4ef6\u7684\u5b8c\u6574\u6027\u3002\u56e0\u4e3a\u6587\u4ef6\u7684\u4e0d\u5b8c\u6574\uff0c\u5f88\u5bb9\u6613\u5c06\u8def\u7531\u5668\u53d8\u6210\u201c\u7816\u5934\u201d\u3002
\u3000\u3000\u9700\u8981\u4e3b\u8981\u7684\u662f\uff0c\u7f16\u8bd1\u5b8c\u6210\u540e\uff0c\u4e00\u5b9a\u8981\u5c06\u7f16\u8bd1\u597d\u7684bin\u76ee\u5f55\u8fdb\u884c\u5907\u4efd\uff08\u5982\u679c\u91cc\u9762\u4e1c\u897f\u5bf9\u4f60\u5f88\u91cd\u8981\u7684\u8bdd\uff09\uff0c\u56e0\u4e3a\u5728\u4e0b\u6b21\u7f16\u8bd1\u4e4b\u524d\uff0c\u6267\u884cmake clean \u4f1a\u5c06bin\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u7ed9\u6e05\u9664\u6389!!
\u3000\u30002\u3001 \u66f4\u6539\u539f\u6709packages
\u3000\u3000\u5728\u7f16\u8bd1\u6839\u76ee\u5f55\u4e0b\u4f1a\u6709\u4e00\u4e2adl\u7684\u76ee\u5f55\uff0c\u8fd9\u4e2a\u76ee\u5f55\u5176\u5b9e\u662f\u201cdownload\u201d\u7684\u7b80\u5199\uff0c\u5728\u7f16\u8bd1\u524d\u671f\uff0c\u9700\u8981\u4ece\u7f51\u7edc\u4e0b\u8f7d\u7684\u6570\u636e\u5305\u90fd\u4f1a\u653e\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\uff0c\u8fd9\u4e9b\u8f6f\u4ef6\u5305\u7684\u4e00\u4e2a\u7279\u70b9\u5c31\u662f\uff0c\u4f1a\u81ea\u52a8\u5b89\u88c5\u5728\u6240\u7f16\u8bd1\u7684\u56fa\u4ef6\u4e2d\uff0c\u4e5f\u5c31\u662f\u6211\u4eecmake menuconfig\u7684\u65f6\u5019\uff0c\u4e3a\u56fa\u4ef6\u914d\u7f6e\u7684\u4e00\u4e9b\u8f6f\u4ef6\u5305\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u66f4\u6539\u8fd9\u4e9b\u6e90\u7801\u5305\uff0c\u53ea\u9700\u8981\u5c06\u66f4\u6539\u597d\u7684\u6e90\u7801\u5305\u6253\u5305\u6210\u76f8\u540c\u7684\u540d\u5b57\u653e\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b\uff0c\u7136\u540e\u5f00\u59cb\u7f16\u8bd1\u5373\u53ef\u3002\u7f16\u8bd1\u65f6\uff0c\u4f1a\u5c06\u8f6f\u4ef6\u5305\u89e3\u538b\u5230build_dir\u76ee\u5f55\u4e0b\u3002\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u81ea\u5df1\u5728dl\u91cc\u9762\u521b\u5efa\u81ea\u5df1\u7684\u8f6f\u4ef6\u5305\uff0c\u7136\u540e\u66f4\u6539\u76f8\u5173\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u8ba9openwrt\u53ef\u4ee5\u8bc6\u522b\u8fd9\u4e2a\u6587\u4ef6\u5305\u3002
\u3000\u3000\u7531\u4e8e\u6211\u7684\u9879\u76ee\u66f4\u6539\u7684\u5185\u5bb9\u662f\u5e95\u5c42\u7684\uff0c\u9700\u8981\u8ddf\u56fa\u4ef6\u4e00\u8d77\u5b89\u88c5\u3002\u6240\u4ee5\uff0c\u6211\u4f7f\u7528\u7684\u65b9\u6cd5\u5c31\u662f\u76f4\u63a5\u66f4\u6539dl\u76ee\u5f55\u4e0b\u8f6f\u4ef6\u5305\uff0c\u7136\u540e\u91cd\u65b0\u8fdb\u884c\u56fa\u4ef6\u7f16\u8bd1\u3002\u611f\u89c9\u7c7b\u4f3c\u4e8eLinux\u7684\u5185\u6838\u7f16\u8bd1\u3002\u53cd\u590d\u7f16\u8fc7\u5341\u591a\u6b21\uff0c\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u3002
\u3000\u30003\u3001 \u65b0\u5efa\u81ea\u5df1\u7684packages\u5bf9\u4e8e\u81ea\u5df1\u65b0\u5efa\u7684package\uff0c\u800c\u8fd9\u4e2apackage\u53c8\u4e0d\u9700\u8981\u968f\u56fa\u4ef6\u4e00\u8d77\u5b89\u88c5\uff0c\u6362\u53e5\u8bdd\u8bf4\uff0c\u5c31\u662f\u53ef\u4ee5\u5f53\u505a\u4e00\u4e2a\u53ef\u9009\u8f6f\u4ef6\u5305\u7684\u8bdd\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u6211\u4eec\u7684SDK\u73af\u5883\u6765\u5355\u72ec\u7f16\u8bd1\uff0c\u7f16\u8bd1\u540e\u4f1a\u751f\u6210\u4e00\u4e2aipk\u7684\u6587\u4ef6\u5305\u3002\u7136\u540e\u5229\u7528 opkg install xxx.ipk \u6765\u5b89\u88c5\u8fd9\u4e2a\u8f6f\u4ef6\u3002
\u3000\u3000\u4e0b\u9762\u5177\u4f53\u8bf4\u4e0b\uff0c\u5982\u4f55\u7f16\u8bd1\u4e00\u4e2ahelloword\u7684\u8f6f\u4ef6\u5305\u3002\uff081\uff09\u9996\u5148\uff0c\u7f16\u5199helloworld\u7a0b\u5e8f \u7f16\u5199helloworld.c /**************** * Helloworld.c * The most simplistic C program ever written. * An epileptic monkey on crack could write this code. *****************/
\u3000\u3000#include #include int main(void) { printf("Hell! O' world, why won't my code compile?\n\n"); return 0; }
\u3000\u3000\u7f16\u5199Makefile\u6587\u4ef6# build helloworld executable when user executes "make"
\u3000\u3000helloworld: helloworld.o$(CC) $(LDFLAGS) helloworld.o -o helloworld
\u3000\u3000helloworld.o: helloworld.c$(CC) $(CFLAGS) -c helloworld.c
\u3000\u3000# remove object files and executable when user executes "make clean"clean: rm *.o helloworld \u5728\u8fd9\u4e24\u4e2a\u6587\u4ef6\u7684\u76ee\u5f55\u4e0b\uff0c\u6267\u884cmake \u5e94\u8be5\u53ef\u4ee5\u751f\u6210helloworld\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u6267\u884chelloworld\u540e\uff0c\u80fd\u591f\u6253\u5370\u51fa\u201cHell! O' world, why won't my code compile?\u201d\u3002 \u8fd9\u4e00\u6b65\uff0c\u4e3b\u8981\u4fdd\u8bc1\u6211\u4eec\u7684\u6e90\u7a0b\u5e8f\u662f\u53ef\u4ee5\u6b63\u5e38\u7f16\u8bd1\u7684\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u5176\u79fb\u690d\u5230OpenWRT\u4e0a\u3002
\u3000\u3000\uff082\uff09\u5c06OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2\u89e3\u538btar \u2013xvf OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2
\u3000\u3000\uff083\uff09\u8fdb\u5165SDKcd OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1 \u53ef\u4ee5\u770b\u5230\u91cc\u9762\u7684\u76ee\u5f55\u7ed3\u6784\u8ddf\u6211\u4eec\u4e4b\u524dsource\u7684\u76ee\u5f55\u7ed3\u6784\u57fa\u672c\u76f8\u540c\uff0c\u6240\u9700\u8981\u7f16\u8bd1\u7684\u8f6f\u4ef6\u5305\uff0c\u9700\u8981\u653e\u7f6e\u5728package\u76ee\u5f55\u4e0b
\u3000\u3000\uff084\uff09\u5728package\u76ee\u5f55\u4e0b\u521b\u5efahelloworld\u76ee\u5f55cd package mkdir helloworld cd helloworld
\u3000\u3000\uff085\uff09\u521b\u5efasrc\u76ee\u5f55\uff0c\u62f7\u8d1d helloworld\u6587\u4ef6mkdir src cp /home/wrt/test/helloworld.c src cp /home/wrt/test/Makefile src
\u3000\u3000\uff086\uff09\u5728helloworld\u76ee\u5f55\u4e0b\u521b\u5efaMakefile\u6587\u4ef6\u8fd9\u4e2aMakefile\u6587\u4ef6\u662f\u7ed9OpenWRT\u8bfb\u7684\uff0c\u800c\u4e4b\u524d\u5199\u7684\u90a3\u4e2aMakefile\u6587\u4ef6\u662f\u9488\u5bf9helloworld\u7ed9\u7f16\u8bd1\u5176\u8bfb\u7684\u3002\u4e24\u4e2aMakefile\u4e0d\u5728\u540c\u4e00\u5c42\u76ee\u5f55\u4e0b\u3002
\u3000\u3000touch Makefilevim Makefile
\u3000\u3000Makefile\u6587\u4ef6\u6a21\u677f\u5185\u5bb9\u5982\u4e0b\uff1a############################################## # 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. # ##############################################
\u3000\u3000include $(TOPDIR)/rules.mk
\u3000\u3000# Name and release number of this packagePKG_NAME:=helloworld PKG_RELEASE:=1
\u3000\u3000# 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)
\u3000\u3000include $(INCLUDE_DIR)/package.mk
\u3000\u3000# 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 define Package/helloworld SECTION:=utils CATEGORY:=Utilities TITLE:=Helloworld -- prints a snarky message endef
\u3000\u3000# Uncomment portion below for Kamikaze and delete DESCRIPTION variable abovedefine Package/helloworld/description If you can't figure out what this program does, you're probably brain-dead and need immediate medical attention. endef
\u3000\u3000# 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
\u3000\u3000# 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
\u3000\u3000# 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 $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/ endef
\u3000\u3000# 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))
\u3000\u3000\uff087\uff09\u8fd4\u56de\u5230SDK\u7684\u6839\u76ee\u5f55\u6267\u884cmake\u8fdb\u884c\u7f16\u8bd1 \u7f16\u8bd1\u8fc7\u7a0b\u4f1a\u5728build_dir\u76ee\u5f55\u4e0b\u5b8c\u6210 \u7f16\u8bd1\u7ed3\u679c\u4f1a\u653e\u5728 bin/[yourtarget]/package\u76ee\u5f55\u4e0bhelloworld_1_bcm47xx.ipk
\u3000\u3000\uff088\uff09\u4e0a\u4f20helloworld_1_bcm47xx.ipk\u4f7f\u7528sftp\u8f6f\u4ef6\u4e0a\u4f20helloworld_1_bcm47xx.ipk\u81f3\u8def\u7531\u5668 \u6267\u884c opkg install helloworld_1_bcm47xx.ipk \u8f93\u5165hello\u7136\u540e\u6309Tab\u952e\uff0c\u53d1\u73b0openwrt\u4e2d\u5df2\u7ecf\u6709helloworld\u53ef\u6267\u884c\u547d\u4ee4\u3002 \u6267\u884c helloworld \u67e5\u770b\u7a0b\u5e8f\u7684\u6548\u679c\u3002
\u3000\u3000Hell! O' world, why won't my code compile?
\u3000\u3000\u3010End\u3011
\u3000\u3000\u5e0c\u671b\u5bf9\u5927\u5bb6\u80fd\u6709\u5e2e\u52a9 :)
\u3000\u3000\u8f6c\u8f7d

1\u3001\u642d\u5efa\u5f00\u53d1\u73af\u5883 \u9996\u5148\uff0c\u5728\u6267\u884cmake menuconfig\u540e\uff0c\u4f1a\u51fa\u73b0\u4e0b\u56fe\uff1a \u5176\u4e2d\uff0c\u56fe\u4e2d\u7ea2\u6846\u90e8\u5206\u662f\u6211\u5b9a\u5236\u8def\u7531\u5668\u7684\u7cfb\u7edf\u7248\u672c\uff0c\u5927\u5bb6\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u8def\u7531\u5668\u8fdb\u884c\u4e0d\u540c\u7684\u9009\u62e9\uff1b\u7eff\u6846\u90e8\u5206\u8868\u793a\u6211\u4eec\u9700\u8981\u7f16\u8bd1\u4e00\u4e2aSDK\u5f00\u53d1\u73af\u5883\uff08\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6b64\u9879\u672a\u52fe\u9009\uff09\u3002

1、搭建开发环境
       首先,在执行make menuconfig后,会出现下图:

  其中,图中红框部分是我定制路由器的系统版本,大家可以根据不同的路由器进行不同的选择;绿框部分表示我们需要编译一个SDK开发环境(默认情况下,此项未勾选)。

  编译过程中需要通过官网下载很多相关的软件包,所以必须保证能够顺利连上外网。由于下载速度的限制,编译过程大概需要数小时。编译结束后,所有的产品都会放在编译根目录下的bin/yourtarget/. 例如:我所编译的产物都放在./bin/brcm47xx/下,其中文件主要有几类:

  (1).bin/.trx 文件: 这些都是在我们所选的target-system的类别之下,针对不同路由器型号、版本编译的路由器固件。这些不同路由器的型号和版本是openwrt预先设置好的,我们不需要更改。至于.bin和.trx的区别,一种说法是,第一次刷路由器的时候,需要用.bin文件,如果需要再升级,则不能再使用.bin文件,而需要用.trx文件。原因是,.bin是将路由器的相关配置信息和.trx封装在一起而生成的封包,也就是说是包含路由器版本信息的.trx。在第一次刷固件的时候,我们需要提供这样的信息,而在后续升级时,则不再需要,用.trx文件即可。

  (2)packages文件夹: 里面包含了我们在配置文件里设定的所有编译好的软件包。默认情况下,会有默认选择的软件包。

  (3)OpenWrt-SDK.**.tar.bz2: 这个也就是我们定制编译好的OpenWRT SDK环境。我们将用这个来进行OpenWrt软件包的开发。例如,我所编译好的SDK环境包为:/bin/brcm47xx/OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2
可以从名称上看出,target system是brcm47xx,host system是Linux-x86_64,使用的编译工具以及库是4.3.3+cs_uClibc-0.9.30.1。

  (4)md5sums 文件: 这个文件记录了所有我们编译好的文件的MD5值,来保证文件的完整性。因为文件的不完整,很容易将路由器变成“砖头”。

  需要主要的是,编译完成后,一定要将编译好的bin目录进行备份(如果里面东西对你很重要的话),因为在下次编译之前,执行make clean 会将bin目录下的所有文件给清除掉!!

  2、 更改原有packages

  在编译根目录下会有一个dl的目录,这个目录其实是“download”的简写,在编译前期,需要从网络下载的数据包都会放在这个目录下,这些软件包的一个特点就是,会自动安装在所编译的固件中,也就是我们make menuconfig的时候,为固件配置的一些软件包。如果我们需要更改这些源码包,只需要将更改好的源码包打包成相同的名字放在这个目录下,然后开始编译即可。编译时,会将软件包解压到build_dir目录下。
当然,你也可以自己在dl里面创建自己的软件包,然后更改相关的配置文件,让openwrt可以识别这个文件包。

  由于我的项目更改的内容是底层的,需要跟固件一起安装。所以,我使用的方法就是直接更改dl目录下软件包,然后重新进行固件编译。感觉类似于Linux的内核编译。反复编过十多次,没有任何问题。

  3、 新建自己的packages
对于自己新建的package,而这个package又不需要随固件一起安装,换句话说,就是可以当做一个可选软件包的话。我们可以利用我们的SDK环境来单独编译,编译后会生成一个ipk的文件包。然后利用 opkg install xxx.ipk 来安装这个软件。

  下面具体说下,如何编译一个helloword的软件包。
(1)首先,编写helloworld程序 
编写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?

"); 
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 
在这两个文件的目录下,执行make 应该可以生成helloworld的可执行文件。执行helloworld后,能够打印出“Hell! O' world, why won't my code compile?”。 这一步,主要保证我们的源程序是可以正常编译的。下面我们将其移植到OpenWRT上。

  (2)将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)进入SDK
cd OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1 
可以看到里面的目录结构跟我们之前source的目录结构基本相同,所需要编译的软件包,需要放置在package目录下

  (4)在package目录下创建helloworld目录
cd package 
mkdir helloworld 
cd helloworld

  (5)创建src目录,拷贝 helloworld文件
mkdir src 
cp /home/wrt/test/helloworld.c src 
cp /home/wrt/test/Makefile src

  (6)在helloworld目录下创建Makefile文件
这个Makefile文件是给OpenWRT读的,而之前写的那个Makefile文件是针对helloworld给编译其读的。两个Makefile不在同一层目录下。

  touch Makefile
vim Makefile

  Makefile文件模板内容如下:
############################################## 
# 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 
define Package/helloworld 
SECTION:=utils 
CATEGORY:=Utilities 
TITLE:=Helloworld -- prints a snarky message 
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 
$(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))

  (7)返回到SDK的根目录
执行make进行编译 
编译过程会在build_dir目录下完成 
编译结果会放在 bin/[yourtarget]/package目录下helloworld_1_bcm47xx.ipk

  (8)上传helloworld_1_bcm47xx.ipk
使用sftp软件上传helloworld_1_bcm47xx.ipk至路由器 
执行 opkg install helloworld_1_bcm47xx.ipk 
输入hello然后按Tab键,发现openwrt中已经有helloworld可执行命令。 
执行 helloworld 查看程序的效果。

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

  【End】

  希望对大家能有帮助 :)



  • 鎬庝箞ubuntu涓嬫惌寤openwrt寮鍙鐜?
    绛旓細2 瑙e帇鏂囦欢銆傝繘鍒板瓨鏀句笅杞芥枃浠剁殑鐩綍涓嬫墽琛宼ar-zxvfqt-everywhere-opensource-src-4.7.0.tar.gz 鍏朵腑qt-everywhere-opensource-src-4.7.0.tar.gz鏄垰涓嬭浇鏂囦欢鐨勬枃浠跺悕 3 榛 璁ゆ儏鍐典笅绯荤粺鏄病鏈塯++缂栬瘧鐜鐨勩傞渶瑕佹墜鍔ㄥ畨瑁呫傛墽琛宻udoapt-getinstall build-essential銆傚鏋滄彁绀烘壘涓嶅埌鍖呭彲浠ユ洿鏂颁竴涓嬫簮...
  • 鍦ㄥ皬绫宠矾鐢卞櫒3G鐨OpenWRT鐜瀹夎node.js
    绛旓細鍦ㄨ矾鐢卞櫒鐨 OpenWRT 鐜瀹夎 node.js 鐜闇瑕佹湁浠ヤ笅鍑犱釜鏉′欢锛欻irokazu MORIKAWA鐨刧ithub椤圭洰 https://github.com/nxhack/openwrt-node-packages 鏄敮鎸乤rm/mips/x86鏋舵瀯鐨勶紝鑳藉婊¤冻澶у鏁扮殑宓屽叆寮忚澶囥備笅闈㈡槸鎴戠殑缂栬瘧杩囩▼锛氳嚦姝わ紝浠诲姟杈炬垚銆傚鏋滆寰楃紪璇戝お楹荤儲锛屽彲浠ョ洿鎺ヤ娇鐢ㄦ垜缂栬瘧鐨勭増鏈傦紙娉ㄦ剰...
  • 濡備綍涓虹幇鏈夌殑openwrt缂栬瘧涓涓猳pkg涓婃病鏈夌殑杞欢
    绛旓細渚濇杈撳叆浠ヤ笅鍛戒护锛1.ubuntu寮鍙鐜闇瑕佺殑杞欢锛歴udo apt-get install gcc g++ binutils patch bzip2 flex bison make autoconf gettext texinfo unzip sharutils subversion libncurses5-dev ncurses-term zlib1g-dev gawk sudo apt-get update 2.鍒涘缓鐩綍 mkdir openwrt 3.鑾峰彇OpenWrt婧愪唬鐮佸拰瀹夎鍖咃紝...
  • 鍦╫penwrt涓鎬庝箞缂栬瘧鑷畾涔夌殑鍐呮牳妯″潡
    绛旓細寮鍙鐜涓簎buntu.棣栧厛鎼缓缂栬瘧鐜銆俿udo apt-get install gcc g++ binutils patch bzip2 flex bison make autoconf gettext texinfo unzip sharutils subversion libncurses5-dev ncurses-term zlib1g-dev gawk asciidoc libz-dev git-core build-essential libssl-dev 涓嬮潰灏辨槸涓嬭浇婧愮爜锛屾簮鐮佸垎涓ょ锛屼竴绉...
  • openwrt娌℃湁澶氱嚎澶氭嫧
    绛旓細鍦ㄥ鐢ㄨ矾鐢卞櫒涓紝涓鑸緢灏戝甫璐熻浇鍧囪 鍙婂绾垮鎷ㄦ帶鍒跺姛鑳界殑锛屽洜涓鸿繖浜涘姛鑳藉彧鍦ㄩ偅浜涢潪甯搁珮绔殑璺敱鍣ㄤ笂鎵嶆湁锛屼絾浣犵殑璺敱鍣ㄨ鏄埛浜哋penWrt绯荤粺锛岄偅瑕佸疄鐜拌繖浜涘姛鑳藉氨闈炲父绠鍗曚簡銆傛湰鏂囨垜浠氨鏉ヨ瑙penWrt鐨勮礋杞藉潎琛″強澶氱嚎澶氭嫧鎺у埗搴旂敤mwan3锛岃澶у涓璧锋潵浜彈涓涓嬭繖浜涚湅浼煎緢楂樻繁鐨勫姛鑳芥槸濡備綍鍦∣penWrt鐜涓...
  • 濡備綍缂栬瘧OpenWrt
    绛旓細鎮ㄦ兂鍦ㄥ疄楠屾х殑骞冲彴涓婃祴璇OpenWrt; 鎮ㄥ弬涓庢祴璇曟垨鍙備笌寮鍙慜penWrt鐨勫伐浣; 鎴栬,鏈绠鍗曠殑鐩殑灏辨槸涓轰簡淇濇寔鑷繁鐨Openwrt涓烘渶鏂扮増鏈; 鑻ユ兂瀹炵幇涓婅堪鐩殑,鍏跺疄寰堢畝鍗,鎸変笅杩版枃瀛楀嵆鍙垚鍔熺紪璇戝嚭涓涓偍鐨凮penwrt鏉ャ 鍑嗗宸ヤ綔 鍦ㄥ紑濮嬬紪璇慜penwrt涔嬪墠闇瑕佹偍鍋氫簺鍑嗗宸ヤ綔;涓庡叾浠栫紪璇戣繃绋嬩竴鏍,绫讳技鐨勭紪璇戝伐鍏峰拰缂栬瘧鐜鏄繀涓...
  • 缂栬瘧OpenWrt绯荤粺姹傚姪
    绛旓細OpenWrt缂栬瘧鐜鎼缓鍜岀紪璇戞柟娉 涓銆 鏇存柊寮鍙戠郴缁熴 瀹夎瀹寀buntu 鍚庯紝閲嶆柊寮鏈虹殑鏃跺欙紝浼氭湁涓涓猇Mware tools 瀹夎鐨勬彁绀虹晫闈紝鑰愬績绛夊緟锛岀瓑鍒癡Mware tools 瀹夎濂藉悗 鍥犱负鏇存柊绯荤粺鍜屽畨瑁呬竴浜涘伐鍏峰寘闇瑕佺敤鍒扮綉缁滐紝鎵浠ヨ繘鍏buntu鐨勭晫闈㈠悗锛 灏唘...
  • openwrt寮鍙戜娇鐢ㄤ粈涔堢紪璇戝櫒
    绛旓細OpenWrt鎼缓缂栬瘧鍣ㄧ紪璇慜penWrt闇瑕丩inux鐜鎴杕ac鐜锛屼笉鏀寔Windows锛屽鏋滆鍦╓indows涓婂紑鍙戯紝閭d箞鑲畾鏄瑁呰櫄鎷熸満鐨勩
  • 銆愯浆.寰呴獙璇併鍦∣penWRT涓婇厤缃甦ebian妯″紡
    绛旓細銆 鍘熸枃鍑哄 銆戞煇浜涘彴杩囦簬寮烘倣鐨 ARM 杞矾鐢憋紝浣嗘槸鏇村鍚堥傜殑绯荤粺鍙湁 OpenWRT 锛屾晠鍙娇鐢 chroot 寤虹珛涓涓闈鐜鏉ュ埄鐢ㄥ浣欑殑鎬ц兘銆傞鍏堬紝閫氬父 OpenWRT 閲嶇疆鐨 shell 涓 ash 锛屽湪 chroot 鍚庝細鍥犱负鎵惧埌鏇夸唬鐨 ash 鑰屽垏鎹㈠け璐ワ紝闇瑕佹浛鎹㈢殑 shell 涓 bash 锛岄氳繃 $SHELL 鐜鍙橀噺鍙互鏌ョ湅褰撳墠鐨 ...
  • Linux鍙戣鐗(璺敱鍣ㄥ浐浠)OpenWrt鍏ラ棬鍏ㄩ泦
    绛旓細鐜伴樁娈openwrt瀹樻柟鏀寔鐨勮矾鐢卞垪琛:wiki.openwrt.org/toh/start 濡傛灉瑕佸涔爋penwrt鐨勮瘽,涔颁簺鍒楄〃涓簩鎵嬬殑璺敱鍣ㄦ潵瀹炶返涓嬫洿瀹规槗瀛︿範,澶ч儴鍒嗚鍗囩骇涓涓媟om鑺墖鍜屽唴瀛樿姱鐗,鍏稿瀷閰嶇疆閮芥槸4M銆64M銆 澶у鍏堝涔犱竴涓,绛夊緟灏忕背寮鏀炬椂鍒荤殑鍒版潵,鏈夊鏃犳偅鈥︹ OpenWrt 鍙互琚弿杩颁负涓涓祵鍏ュ紡鐨 Linux 鍙戣鐗,(涓绘祦璺敱鍣ㄥ浐...
  • 扩展阅读:软路由openwrt官网 ... openwrt 打不开视频 ... 刷openwrt最稳的路由器 ... openwrt进不去luci ... openwrt clash ... openwrt在哪里找软件 ... openwrt挂梯子 ... openwrt官网下载中心 ... openwrt开启无线wifi ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网