easycwmp的交叉编译

平台:
Linux version 2.6.32-279.el6.x86_64
交叉编译器路径:/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-
开发板:FL2440
开发板运行内核:linux3.0
开发板文件系统: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.创建fl-easycwmp目录用以存放安装文件

2.创建install目录

二、交叉编译json-c

1.获取json-c源文件

 git clone git://github.com/json-c/json-c.git ./json-c

 

2.创建build.sh

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.执行 build.sh 安装json-c

然后在生成的Makefile里把修改LDFLAGS = -lm,注意每次修改后要make clean(make clean仅仅是清除之前编译的可执行文件及配置文件。 而make distclean要清除所有生成的文件。)

make

make install

三、安装libubox

1、获取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.错误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时加上

export ac_cv_have_decl_isinf=yes
export ac_cv_have_decl_isnan=yes

 

并连接上数学库即LDFLAGS+=-lm

 

四、安装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

 

五、安装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]$

 

六、安装microxml

1.获取源文件

[weishusheng@localhost fl-easycwmp]$ git clone git://dev.freecwmp.org/microxml ./microxml

 

2、建立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.执行 build.sh

 

七、最后编译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开发板上跑,在开发板上还要做相关设置,请参考下一遍博文。

 

posted @ 2015-03-18 22:40  wssheng  阅读(2507)  评论(0编辑  收藏  举报