如何在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?\n\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
在这两个文件的目录下,执行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?

  • openwrt涓婄殑frp瀹㈡埛绔鎬庝箞鎵撳紑?娌℃壘鍒板紑鍚夐」
    绛旓細1. **瀹夎frpc瀹㈡埛绔**锛氬湪OpenWrt鐜涓锛岄鍏堥渶瑕佸畨瑁協rpc瀹㈡埛绔傝繖鍙互閫氳繃OpenWrt鐨勫寘绠$悊鍣紙濡俹pkg锛夋潵瀹屾垚銆2. **瀹夎鍥惧舰鎿嶄綔鐣岄潰**锛氬畨瑁協rpc瀹㈡埛绔箣鍚庯紝杩橀渶瑕佸畨瑁卨uci-app-frpc锛岃繖鏄竴涓熀浜嶭uci鐨勫浘褰㈡搷浣滅晫闈紝鍙互甯姪鎮ㄦ洿鏂逛究鍦扮鐞嗗拰閰嶇疆frpc瀹㈡埛绔3. **閰嶇疆鏈嶅姟**锛氬畨瑁呭畬鎴...
  • 濡備綍鍦∣penWRT鐜涓嬪仛寮鍙
    绛旓細涓嬮潰鎴戜滑灏嗗叾绉绘鍒OpenWRT涓娿 (2)灏OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2瑙e帇tar 鈥搙vf OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2 (3)杩涘叆SDKcd OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0....
  • OpenWRT(涓)寮鍙鐜鎼缓
    绛旓細3銆佷笅杞OpenWRT婧愮爜 鍏堝垱寤篛penWRT鐩綍鏉ヤ繚瀛樻簮鐮 mkdir OpenWRT cd OpenWRT git clone https://github.com/openwrt-mirror/openwrt.git (涓嬭浇婧愮爜)4銆佹洿鏂版簮鐮佷笂鐨勮蒋浠 涓轰簡浣 OpenWrt 鏀寔鏇村鐨勮蒋浠讹紝闇瑕佹洿鏂板拰瀹夎鍏跺畠婧愪笂闈㈢殑杞欢 cd openwrt ./scripts/feeds update -a...
  • openwrt鏃佽矾鐢辫缃
    绛旓細瀹為檯鎿嶄綔杩囩▼涓鑸寘鎷互涓嬫楠わ細棣栧厛锛岄渶瑕佽缃富璺敱鍣紝淇濊瘉鍏舵甯歌繛鎺ヤ簰鑱旂綉锛涚劧鍚庯紝閰嶇疆OpenWrt璺敱鍣紝璁惧畾鍏禝P鍦板潃銆佺綉鍏炽丏NS绛夊弬鏁帮紝浣垮叾鑳藉鎺ュ叆涓昏矾鐢卞櫒鏋勫缓鐨勭綉缁鐜锛涙渶鍚庯紝鏍规嵁闇姹鍦∣penWrt璺敱鍣ㄤ笂瀹夎鍜岄厤缃浉搴旂殑缃戠粶鏈嶅姟锛屼互杈惧埌鎵╁睍缃戠粶鍔熻兘鐨勭洰鐨勩備緥濡傦紝鍋囪浣犵殑涓昏矾鐢卞櫒鏃犳硶瑕嗙洊瀹朵腑鐨...
  • 璺敱鍣ㄦ柊鎵嬪叆闂ㄢ斺OpenWRT鍩虹璁剧疆绡
    绛旓細ACC绛夌壒鎬у垯鑳芥彁鍗囩綉缁滄晥鐜囥傚浜庡绾垮鎷ㄧ敤鎴凤紝璐熻浇鍧囪 鏄紭鍖栫綉缁滄ц兘鐨勫叧閿墜娈点OpenWRT鐨勫熀纭璁剧疆鏃ㄥ湪甯姪浣犳墦閫犱竴涓己澶т笖涓у寲鐨勭綉缁鐜锛屼絾姣忎釜姝ラ閮介渶瑕佹牴鎹疄闄呴渶姹傝繘琛屻傛垜浠紦鍔变綘鍦ㄥ疄璺典腑瀛︿範鍜屽垎浜粡楠岋紝鍏卞悓鎻愬崌缃戠粶涓栫晫鐨勭悊瑙c傝鎴戜滑涓璧锋帰绱㈣繖涓厖婊″彲鑳芥х殑璺敱鍣ㄤ笘鐣屽惂锛
  • openwrt鍙寃an鎺ュ叆鍏ㄦ祦绋,鎵嬪姩鍒嗛厤璁惧鎺ュ叆缃戠粶,鏀寔璐熻浇鍧囪 绛栫暐_鐧惧害...
    绛旓細1. 璁剧疆鍙學AN鎺ュ彛棣栧厛锛岀櫥褰OpenWRT鐨勫懡浠よ鐣岄潰锛屼负浣犵殑璺敱鍣ㄦ坊鍔犱袱涓猈AN鎺ュ彛銆傚湪 缃戠粶</ - 浜ゆ崲鏈洪厤缃</ 涓紝鍒嗗埆璁剧疆eth1鍜屾柊鍒涘缓鐨剋anb鎺ュ彛銆俥th1浣滀负涓籛AN锛屼笌LAN1-3WAN鐩歌繛锛岃寃anb浣跨敤PPPoe锛岃繛鎺ヨ嚦lan1鐨別th1.12銆備负姣忎釜鎺ュ彛鍒嗛厤涓嶅悓鐨刅LAN锛堝VLAN11鍜孷LAN12锛夛紝骞剁‘淇漧an鍙h缃负鏈...
  • openWRT浣跨敤鎶宸
    绛旓細濡傛灉浣犲 Linux 鏈夋墍浜嗚В锛岀壒鍒槸鎯宠笍鍏ュ祵鍏ュ紡 Linux 鐨勪笘鐣岋紝OpenWRT 鏄竴涓粷浣宠捣鐐广傚畠鑳借浣犳帉鎻℃棤绾胯矾鐢卞櫒鐨勫熀纭鐭ヨ瘑锛屽悓鏃朵簡瑙d竴鑸祵鍏ュ紡 Linux 寮鍙戞祦绋嬶紝涓嶈浣犵殑澶勭悊鍣ㄦ槸 ARM銆丳owerPC 杩樻槸 MIPS锛屽熀鏈楠ゅ涓嬶細鍒涘缓 Linux 浜ゅ弶缂栬瘧鐜锛岃繖鏄紑鍙戠殑绗竴姝ワ紝瀹冨厑璁镐綘鍦ㄧ洰鏍囩郴缁熶笂杩涜缂栬瘧銆
  • 鍦╫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鐨凱andoraBox鐜涓嬫庝箞閲嶆柊鍒峰叆u-boot
    绛旓細OpenWrt鐨凱andoraBox鐜涓嬫庝箞閲嶆柊鍒峰叆u-boot 鎴戞槸灏忓害璺敱,鍒峰叆uboot浠ュ悗,鎺ョ潃鍒峰叆OpenWrt鐨凱andoraBox,鍦ㄨ繖閲岄潰閲屽崌绾т簡涓娆″浐浠,鐒跺悗璺敱鍣╱-boot鎭㈠妯″紡杩涗笉鍘讳簡,,璇存壘涓嶅埌鏂囦欢,,鎯抽噸鏂板埛u-boot鍒峰洖灏忓害瀹樻柟鍥轰欢...甯屾湜澶у鑳... 鎴戞槸灏忓害璺敱,鍒峰叆uboot浠ュ悗,鎺ョ潃鍒峰叆OpenWrt鐨凱andoraBox,鍦ㄨ繖閲...
  • 濡備綍涓虹幇鏈夌殑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婧愪唬鐮佸拰瀹夎鍖咃紝...
  • 扩展阅读:软路由openwrt官网 ... openwrt登录地址忘了 ... openwrt软件包列表 ... openwrt安装主题包 ... openwrt安装本地软件包 ... openwrt恢复初始口令 ... openwrt官网固件下载 ... openwrt删除软件包 ... 想在openwrt安装其他软件 ...

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