原創做品,轉載請註明出處html
copyright:weishusheng 2015.3.18 linux
email:642613208@qq.comgit
平臺:github
Linux version 2.6.32-279.el6.x86_64json
交叉編譯器路徑:/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-vim
開發板:FL2440bash
開發板運行內核:linux3.0curl
開發板文件系統:initramfs函數
easycwmp依賴的許多軟件的Makefile都是用cmake語法生成的,故簡單介紹一下要常常用到的幾個cmake語法:工具
1.LINK_DIRECTORIES:動態連接庫或靜態連接庫的搜索路徑,至關於gcc的-L參數
2.INCLUDE_DIRECTORIES:指定頭文件搜索路徑,至關於-I參數
3.TARGET_LINK_LIBRARIES(uci ubox)添加連接庫,相同於指定-l參數。
如:target_link_libraries(demo Hello) #將可執行文件與Hello鏈接成最終文件demo
4.find_library(THREAD_DB_LIBRARY NAMES thread_db PATHS /usr/lib/ /usr/local/lib/)
在目錄"/usr/lib"和"/usr/local/lib"下查找libthread_db.so,若是找到則把庫文件的絕對路徑賦給THREAD_DB_LIBRARY
1、準備工做
1.建立fl-easycwmp目錄用以存放安裝文件
[weishusheng@localhost ~]$ mkdir fl-easycwmp
2.建立install目錄
[weishusheng@localhost ~]$ cd fl-easycwmp/
[weishusheng@localhost fl-easycwmp]$ ls
[weishusheng@localhost fl-easycwmp]$ mkdir install
[weishusheng@localhost fl-easycwmp]$ ls
install
2、交叉編譯json-c
1.獲取json-c源文件
[weishusheng@localhost fl-easycwmp]$ git clone git://github.com/json-c/json-c.git ./json-c
2.建立build.sh
[weishusheng@localhost json-c]$ vim build.sh
#!/bin/bash
PRJ_PATH=`pwd`
#CROSS=/opt/buildroot-2011.11/arm920t/usr/bin/arm-linux-
CROSS=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-
make clean
#autoreconf -i
export CC=${CROSS}gcc
export CPP=${CROSS}cpp
export AS=${CROSS}as
export LD=${CROSS}ld
export AR=${CROSS}ar
export RANLIB=${CROSS}ranlib
export STRIP=${CROSS}strip
export ac_cv_func_malloc_0_nonnull=yes
export ac_cv_func_realloc_0_nonnull=yes
export ac_cv_have_decl_isinf=yes
export ac_cv_have_decl_isnan=yes
#export LDFLAGS += -lm
./configure --enable-rdrand --disable-dependency-tracking --with-PACKAGE --with-gnu-ld --with-pic --bu
ild=i686-pc-linux-gnu --host=arm-linux --prefix=${PRJ_PATH}/../install
順便說一下makefile裏cflags的-Wall -o2 -g
-Wall 是打開警告開關,-O表明默認優化,可選:-O0不優化,-O1低級優化,-O2中級優化,-O3高級優化,-Os代碼空間優化。
-g是生成調試信息,生成的可執行文件具備和源代碼關聯的可調試的信息;-pg是生成函數調用關係,函數執行時間等信息,將這些信息保存在gmon.out中,可用gprof命令進行查看
3.安裝json-c
[weishusheng@localhost json-c]$ sh build.sh
而後在生成的Makefile裏把修改LDFLAGS = -lm,注意每次修改後要make clean(make clean僅僅是清除以前編譯的可執行文件及配置文件。 而make distclean要清除全部生成的文件。)
make
make install
3、安裝libubox
一、獲取libubox
[weishusheng@localhost fl-easycwmp]$ git clone git://nbd.name/luci2/libubox.git ./libubox
2.在工程主CMakeLists.txt 中首先加入以下語句:(##注意Linux第一個字母爲大寫,不然cmake不認識)
[weishusheng@localhost libubox]$ vim CMakeLists.txt
SET(CMAKE_SYSTEM_NAME Linux) #告訴編譯器你要交叉編譯,Linux是你要編譯過去的平臺
SET(TOOLCHAIN_DIR "/opt/buildroot-2012.08/arm920t/usr") #告訴編譯器交叉編譯器路徑
SET(CMAKE_FIND_ROOT_PATH ${TOOLCHAIN_DIR}) #告訴cmake查找的根目錄是什麼
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH) #告訴cmake怎麼查找編譯時的工具程序的位置
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY "/home/weishusheng/fl-easycwmp/install/lib/") #告訴cmake怎麼查找編譯時的庫文件的位置
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) #告訴cmake怎麼查找編譯時的頭文件的位置
SET(CMAKE_C_COMPILER ${TOOLCHAIN_DIR}/bin/arm-linux-gcc)
SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_DIR}/bin/arm-linux-g++)
SET(CMAKE_INSTALL_PREFIX "/home/weishusheng/fl-easycwmp/install/")
注意:
ONLY表示只在CMAKE_FIND_ROOT_PATH下找
BOTH表示能夠在CMAKE_FIND_ROOT_PATH下找,也能夠在系統目錄下找
NEVER表示不查找
3.生成Makefile
[weishusheng@localhost libubox]$ cmake CMakeLists.txt -DBUILD_LUA=OFF
4.make
錯誤解決:
錯誤1
/home/weishusheng/fl-easycwmp/libubox/blobmsg_json.c: In function '__blobmsg_add_json':
/home/weishusheng/fl-easycwmp/libubox/blobmsg_json.c:78:2: error: implicit declaration of function 'is_error'
分析:is_error沒有定義,在pc上編譯時也遇到這個問題,咱們知道他的定義爲#define is_error(ptr) (ptr == NULL),因而添加定義
[weishusheng@localhost libubox]$ vim /home/weishusheng/fl-easycwmp/libubox/blobmsg_json.c
#include "blobmsg.h"
#include "blobmsg_json.h"
#define is_error(ptr) (ptr == NULL)
錯誤2
cannot find -ljson-c
在CMakeLists.txt裏指定json-c庫路徑
[weishusheng@localhost libubox]$ vim CMakeLists.txt
LINK_DIRECTORIES(/home/weishusheng/fl-easycwmp/install/lib/)
錯誤3
/home/weishusheng/fl-easycwmp/libubox/jshn.c: In function 'jshn_parse':
/home/weishusheng/fl-easycwmp/libubox/jshn.c:162:2: error: implicit declaration of function 'is_error'
添加定義
#define is_error(ptr) (ptr == NULL)
4.
/home/weishusheng/cross_easycwmp/install/lib/libjson-c.so: undefined reference to `__isnan'
/home/weishusheng/cross_easycwmp/install/lib/libjson-c.so: undefined reference to `__isinf'
這個問題讓我抓狂,但大家按個人博客作下來不會出現這個問題,由於我在前面寫json-c的安裝時爲大家解決了,若是有遇到相似問題的,能夠嘗試在編譯json-c時加上
export ac_cv_have_decl_isinf=yes
export ac_cv_have_decl_isnan=yes
並鏈接上數學庫即LDFLAGS+=-lm
4、安裝uci
1.獲取uci
[weishusheng@localhost fl-easycwmp]$ git clone git://nbd.name/uci.git ./uci
2.修改CMakeList.txt
[weishusheng@localhost uci]$ vim CMakeLists.txt
SET(CMAKE_SYSTEM_NAME Linux)
SET(TOOLCHAIN_DIR "/opt/buildroot-2012.08/arm920t/usr")
SET(CMAKE_FIND_ROOT_PATH ${TOOLCHAIN_DIR})
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
SET(CMAKE_C_COMPILER ${TOOLCHAIN_DIR}/bin/arm-linux-gcc)
SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_DIR}/bin/arm-linux-g++)
SET(CMAKE_INSTALL_PREFIX "/home/weishusheng/fl-easycwmp/install/")
cmake_minimum_required(VERSION 2.6)
3.生成makefile
[weishusheng@localhost uci]$ cmake CMakeLists.txt -DBUILD_LUA=OFF
4.make
/opt/buildroot-2012.08/arm920t/usr/lib/gcc/arm-unknown-linux-uclibcgnueabi/4.5.4/../../../../arm-unknown-linux-uclibcgnueabi/bin/ld: cannot find -lubox
在CMakeList.txt里加入LINK_DIRECTORIES(/home/weishusheng/fl-easycwmp/install/lib/),問題解決
5.make install
5、安裝ubus
1.獲取ubus
git clone git://nbd.name/luci2/ubus.git ./ubus
2.修改CMakeList.txt
[weishusheng@localhost ubus]$ vim CMakeLists.txt
SET(CMAKE_SYSTEM_NAME Linux)
SET(TOOLCHAIN_DIR "/opt/buildroot-2012.08/arm920t/usr")
SET(CMAKE_FIND_ROOT_PATH ${TOOLCHAIN_DIR})
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
SET(CMAKE_C_COMPILER ${TOOLCHAIN_DIR}/bin/arm-linux-gcc)
SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_DIR}/bin/arm-linux-g++)
SET(CMAKE_INSTALL_PREFIX "/home/weishusheng/fl-easycwmp/install/")
3.生成makefile
[weishusheng@localhost ubus]$ cmake CMakeLists.txt -DBUILD_LUA=OFF
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
json
linked by target "cli" in directory /home/weishusheng/fl-easycwmp/ubus
解決辦法:在CMakeList.txt裏指定json庫路徑,經過修改SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)爲
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY "/home/weishusheng/fl-easycwmp/install/lib")可解決。
4.make
錯誤:cannot find -lubox
解決辦法:在CMakeLists里加上LINK_DIRECTORIES(/home/weishusheng/fl-easycwmp/install/lib)
錯誤:json/json.h: No such file or directory
解決辦法:交叉編譯時默認標準頭文件是交叉編譯器路徑下的,能夠用echo 'main(){}'|/opt/buildroot-
2012.08/arm920t/usr/bin/arm-linux-gcc -E -v - 命令進行查看,咱們把須要用到的頭文件加到上訴命令找到的位置就能夠
了,另外,交叉編譯會到系統默認標準庫目錄下找庫文件,這會致使格式不符合,咱們須要用-L及-l指定庫文件
[weishusheng@localhost json-c]$ pwd
/home/weishusheng/fl-easycwmp/install/include/json-c
[weishusheng@localhost json-c]$ sudo mkdir /opt/buildroot-2012.08/arm920t/usr/arm-unknown-linux-
uclibcgnueabi/sysroot/usr/include/json
[weishusheng@localhost json-c]$ sudo cp * /opt/buildroot-2012.08/arm920t/usr/arm-unknown-linux-
uclibcgnueabi/sysroot/usr/include/json
[weishusheng@localhost json-c]$
6、安裝microxml
1.獲取源文件
[weishusheng@localhost fl-easycwmp]$ git clone git://dev.freecwmp.org/microxml ./microxml
二、創建build.sh文件
[weishusheng@localhost microxml]$ vim build.sh
#!/bin/bash
PRJ_PATH=`pwd`
#CROSS=/opt/buildroot-2011.11/arm920t/usr/bin/arm-linux-
CROSS=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-
make distclean
autoreconf -i
export CC=${CROSS}gcc
export CPP=${CROSS}cpp
export AS=${CROSS}as
export LD=${CROSS}ld
export AR=${CROSS}ar
export RANLIB=${CROSS}ranlib
export STRIP=${CROSS}strip
./configure --host=arm-linux --prefix=${PRJ_PATH}/../install
make && make install
3.執行
[weishusheng@localhost microxml]$ sh build.sh
//7、把shflags.sh放到/usr/share/shflags/目錄裏
7、最後終於要編譯easycwmp了
1.獲取源文件
[weishusheng@localhost fl-easycwmp]$ wget http://easycwmp.org/download/easycwmp-1.0.5.tar.gz
2.解壓,把easycwmp-1.0.5改名爲easycwmp並進入該目錄
[weishusheng@localhost fl-easycwmp]$ cp ../cross_easycwmp/easycwmp-1.0.5.tar.gz .
[weishusheng@localhost fl-easycwmp]$ mv easycwmp-1.0.5 easycwmp
[weishusheng@localhost fl-easycwmp]$ cd easycwmp
3.生成makefile
[weishusheng@localhost easycwmp]$ autoreconf -i
3.生成makefile
[weishusheng@localhost easycwmp]$CC=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc AR=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-ar LD=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-ld AS=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-as RANLIB=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-ranlib CPP=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-cpp ./configure --host=arm-linux --prefix=/home/weishusheng/myfl2440/cwmp/fl-easycwmp/install MICROXML_CFLAGS="-I/home/weishusheng/myfl2440/cwmp/fl-easycwmp/install/include -D_THREAD_SAFE -D_REENTRANT" MICROXML_LIBS="-L/home/weishusheng/myfl2440/cwmp/fl-easycwmp/install/lib -lmicroxml -lpthread" --enable-debug --enable-devel --enable-acs=multi --enable-jsonc=1 --with-uci-include-path=/home/weishusheng/myfl2440/cwmp/fl-easycwmp/uci/
4.[weishusheng@localhost easycwmp]$ make
錯誤:curl/curl.h: No such file or directory
解決辦法:a、先交叉編譯curl(下載地址http://curl.haxx.se/download.html),
b、[weishusheng@localhost include]$ sudo mkdir /opt/buildroot-2012.08/arm920t/usr/arm-unknown-linux-
uclibcgnueabi/sysroot/usr/include/curl
c、[weishusheng@localhost fl-easycwmp]$ tar -xzf curl-7.41.0.tar.gz
d、vim build.sh
#!/bin/bash
PRJ_PATH=`pwd`
#CROSS=/opt/buildroot-2011.11/arm920t/usr/bin/arm-linux-
CROSS=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-
make distclean
autoreconf -i
export CC=${CROSS}gcc
export CPP=${CROSS}cpp
export AS=${CROSS}as
export LD=${CROSS}ld
export AR=${CROSS}ar
export RANLIB=${CROSS}ranlib
export STRIP=${CROSS}strip
./configure --host=arm-linux --prefix=${PRJ_PATH}/../install
make && make install
e.把頭文件複製到相應位置
[weishusheng@localhost curl]$ pwd
/home/weishusheng/fl-easycwmp/install/include/curl
[weishusheng@localhost curl]$ ls
curlbuild.h curlrules.h easy.h multi.h typecheck-gcc.h
curl.h curlver.h mprintf.h stdcheaders.h
[weishusheng@localhost curl]$ sudo cp * /opt/buildroot-2012.08/arm920t/usr/arm-unknown-linux-uclibcgnueabi/sysroot/usr/include/curl
錯誤:src/ubus.c:14:21: fatal error: libubus.h: No such file or directory
解決辦法:
[weishusheng@localhost ubus]$ pwd
/home/weishusheng/fl-easycwmp/ubus
[weishusheng@localhost ubus]$ sudo cp libubus.h /opt/buildroot-2012.08/arm920t/usr/arm-unknown-linux-
uclibcgnueabi/sysroot/usr/include/
錯誤:ubusmsg.h: No such file or directory
解決辦法:
[weishusheng@localhost ubus]$ pwd
/home/weishusheng/cross_easycwmp/ubus
[weishusheng@localhost ubus]$ sudo cp ubusmsg.h /opt/buildroot-2012.08/arm920t/usr/arm-unknown-linux-
uclibcgnueabi/sysroot/usr/include/
錯誤:ubus_common.h: No such file or directory
解決辦法:
[weishusheng@localhost ubus]$ sudo cp ubus_common.h /opt/buildroot-2012.08/arm920t/usr/arm-unknown-linux-
uclibcgnueabi/sysroot/usr/include/
錯誤:src/json.c:42: undefined reference to `is_error'
解決辦法:
vim src/json.c
#define is_error(ptr) (ptr == NULL)
[weishusheng@localhost easycwmp]$ make
[weishusheng@localhost easycwmp]$ make install
至此easycwmp的交叉編譯完成,接下來放到FL2440開發板上跑,在開發板上還要作相關設置,請參考下一遍博文。