openWRT的ipk编译方法

编译时要配置内核,make menuconfig

配置Build the OpenWrt SDK ,前提条件是openwrt编译成功

1.在编译好的文件中会有/bin/ramips/OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2文件
2.然后解压这个文件

tar xvf OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2

进入这个sdk,会看见一个package,在下新建mkdir -p helloworld/src 两个目录

在目录src下新建helloworld.c
  1.     #include  
  2.     #include   
  3.     int main(void)  
  4.     {  
  5.                printf("Hell! O' world, why won't my code compile?\n\n");  
  6.                return 0;  
  7.     }  
复制代码
然后在此目录下创建Makefile( 注意Makefile的语法要用tab )
  1.     # build helloworld executable when user executes "make"  
  2.     helloworld: helloworld.o  
  3.          $(CC) $(LDFLAGS) helloworld.o -o helloworld  
  4. helloworld.o: helloworld.c  
  5.                     $(CC) $(CFLAGS) -c helloworld.c  
  6.     # remove object files and executable when user executes "make clean"  
  7.     clean:  
  8.                    rm *.o helloworld  
复制代码
然后返回上层目录,即helloworld目录,在此目录下创建一个Makefile
  1.     ##############################################  
  2.     # OpenWrt Makefile for helloworld program  
  3.     #  
  4.     #  
  5.     # Most of the variables used here are defined in  
  6.     # the include directives below. We just need to  
  7.     # specify a basic description of the package,  
  8.     # where to build our program, where to find  
  9.     # the source files, and where to install the  
  10.     # compiled program on the router.  
  11.     #  
  12.     # Be very careful of spacing in this file.  
  13.     # Indents should be tabs, not spaces, and  
  14.     # there should be no trailing whitespace in  
  15.     # lines that are not commented.  
  16.     #  
  17.     ##############################################  
  18.     include $(TOPDIR)/rules.mk  
  19.     # Name and release number of this package  
  20.     PKG_NAME:=helloworld  
  21.     PKG_RELEASE:=1  
  22.       
  23.       
  24.     # This specifies the directory where we're going to build the program.   
  25.     # The root build directory, $(BUILD_DIR), is by default the build_mipsel  
  26.     # directory in your OpenWrt SDK directory  
  27.     PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)  
  28.       
  29.       
  30.     include $(INCLUDE_DIR)/package.mk  
  31.        
  32.     # Specify package information for this program.  
  33.     # The variables defined here should be self explanatory.  
  34.     # If you are running Kamikaze, delete the DESCRIPTION  
  35.     # variable below and uncomment the Kamikaze define  
  36.     # directive for the description below  
  37.     define Package/helloworld  
  38.                    SECTION:=utils  
  39.                    CATEGORY:=Utilities  
  40.                    TITLE:=Helloworld -- prints a snarky message  
  41.     endef  
  42.     # Uncomment portion below for Kamikaze and delete DESCRIPTION variable above  
  43.     define Package/helloworld/description  
  44.             If you can't figure out what this program does, you're probably  
  45.             brain-dead and need immediate medical attention.  
  46.     endef  
  47.     # Specify what needs to be done to prepare for building the package.  
  48.     # In our case, we need to copy the source files to the build directory.  
  49.     # This is NOT the default.  The default uses the PKG_SOURCE_URL and the  
  50.     # PKG_SOURCE which is not defined here to download the source from the web.  
  51.     # In order to just build a simple program that we have just written, it is  
  52.     # much easier to do it this way.  
  53.     define Build/Prepare  
  54.                    mkdir -p $(PKG_BUILD_DIR)  
  55.                    $(CP) ./src/* $(PKG_BUILD_DIR)/  
  56.     endef  
  57.     # We do not need to define Build/Configure or Build/Compile directives  
  58.     # The defaults are appropriate for compiling a simple program such as this one  
  59.     # Specify where and how to install the program. Since we only have one file,  
  60.     # the helloworld executable, install it by copying it to the /bin directory on  
  61.     # the router. The $(1) variable represents the root directory on the router running  
  62.     # OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install  
  63.     # directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the  
  64.     # command to copy the binary file from its current location (in our case the build  
  65.     # directory) to the install directory.  
  66.     define Package/helloworld/install  
  67.                     $(INSTALL_DIR) $(1)/bin  
  68.                     $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/  
  69.     endef  
  70.     # This line executes the necessary commands to compile our program.  
  71.     # The above define directives specify all the information needed, but this  
  72.     # line calls BuildPackage which in turn actually uses this information to  
  73.     # build a package.  
  74.     $(eval $(call BuildPackage,helloworld))
复制代码


这里的Makefile文件要注意tab(如果是复制过去的话)

然后回到OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.6-linaro_uClibc-0.9.33.2目录执行make

编译过程会在build_dir目录下完成
make[1] world
make[2] package/compile
make[3] -C package/helloworld compile
make[2] package/index

说明编译成功

编译好的文件放置在 bin/[yourtarget]/package目录下

你可能感兴趣的:(openwrt)