交叉编译:在x86 CPU环境下编译适用于MIPS CPU架构的二进制程序html
所需环境:x86(32位)linux系统【注意,必定得是32位的,否则没法使用提供的32位mips交叉编译程序。并且,智龙上的芯片也是32位的。以前安装64位的结果编译出来的程序也是64的,形成运行失败】linux
编译好的程序经过USB线链接,经过putty使用Series模式登陆到龙芯智龙的linux操做系统:c++
设置上注意Flow Control和通信Speed为115200git
这里给出一个benchmark程序:shell
#include <stdio.h> #include <sys/time.h> #include <unistd.h> int main(){ int t, len1, len2; len1 = 181000000; len2 = 81000000; struct timeval start, end; gettimeofday( &start, NULL ); for(t=0;t<len1;t++); gettimeofday( &end, NULL );printf("%d times Empty loop need: %d us (%.3f ms)\n", len1, (int) (1000000*(end.tv_sec-start.tv_sec)+end.tv_usec-start.tv_usec), (float) (1000000*(end.tv_sec-start.tv_sec)+end.tv_usec-start.tv_usec) / 1000.0); long a=0; gettimeofday( &start, NULL ); for(t=0;t<len2;t++){ a = a + 1; }; gettimeofday( &end, NULL );printf("%d times + need: %d us (%.3f ms)\n", len2, (int) (1000000*(end.tv_sec-start.tv_sec)+end.tv_usec-start.tv_usec), (float) (1000000*(end.tv_sec-start.tv_sec)+end.tv_usec-start.tv_usec) / 1000.0); double b=39.2; gettimeofday( &start, NULL ); for(t=0;t<len2;t++){ b = b + 1.0; b = b * 0.99; }; gettimeofday( &end, NULL );printf("%d times +* need: %d us (%.3f ms)\n", len2, (int) (1000000*(end.tv_sec-start.tv_sec)+end.tv_usec-start.tv_usec), (float) (1000000*(end.tv_sec-start.tv_sec)+end.tv_usec-start.tv_usec) / 1000.0); return 0; }
编译命令为:ubuntu
编译为x86程序: gcc benchmark.c -o benchmark_x86 编译为MIPS程序: mipsel-linux-gcc benchmark.c -o benchmark_mips
注意:putty刚链接上智龙开发板的时候,是一片漆黑的,这时候会误觉得还在链接中,一直等着。其实按一下回车就进入到shell界面了……真是囧死vim
出现bus error和segment fault是同理的,mips上是bus error
开启网络bash
ifconfig eth0 up
设置静态IP网络
ifconfig eth0 192.168.3.2
【注意】这里不能直接经过设置静态IP来启动网卡,必须先eth up再eth ip地址,不然ping不通,找不到网络。架构
跟电脑端网线直连(不经过路由器),电脑端只须要设置
IP地址
192.168.3.1
子网掩码
255.255.255.0
这两个便可
设置完后,经过putty连接,连接方式选择telnet便可
登陆帐户为root,若是没改过是不须要密码的
【设置动态获取DNS】
此时虽然设置了静态ip,咱们会发觉咱们ping内网ping得通,外网彻底不行,dns也所有失效没法解析www.baidu.com等域名。这就须要开启动态dhc客户端,自动更新ip地址以及dns:
修改以下文件:
# 若是是debian系统,则应该修改这个文件: vi /etc/network/interfaces # 若是是ubuntu系统,则应该修改这个文件: vim /etc/sysconfig/network-scripts/ifcfg-eth1
添加以下代码:
# 启动系统激活设备. # Loop回环地址. auto lo iface lo inet loopback # 启动系统激活设备. # 网卡eth0设置为DHCP类型. auto eth0 iface eth0 inet dhcp
添加后须要重启网络服务:
/etc/init.d/networking restart 若是发觉重启networking后不起做用,能够尝试执行如下语句: dhclient eth1
若是还不行,尝试以下配置再重启:
# DNS地址设置 vi /etc/resolv.conf # 必须设置.不然没法访问任何URL nameserver x.x.x.x nameserver x.x.x.x
编译大型带configure的项目的时候,须要注意工具链等一系列问题,例如编译openssh,参考编译代码以下:
./configure --with-zlib=/home/liangzulin/Downloads/zlib-1.2.11/ --host=mipsel-linux --with-cflags=-I/home/liangzulin/Downloads/openssl/include --with-ldflags=-L/home/liangzulin/Downloads/openssl/final_mips_openssl
make CC="mipsel-linux-gcc" AR="mipsel-linux-ar rc" RANLIB="mipsel-linux-ranlib"
一键拷贝全部生成的可执行文件:
for i in scp ssh ssh-keyscan sshd sftp ssh-add ssh-keysign sftp-server ssh-keygen ssh-pkcs11-helper; do cp $i mipsel_file_2/; done;
小插曲:使用的kylin ubuntu系统缺少vim,而后apt源也比较旧了
【传输文件的问题】
因为没有SSH,传输文件暂时比较麻烦。咱们能够交叉编译一个小工具,使用xshell来解决:
lrzsz is a portable and fast implementation of the X/Y/Zmodem protocols.
老版本的debian系统可能不含gcc,可是含有cpp、cc一、as、ld等编译工具链,能够采用以下方式编译:
mipsel-linux-cpp hello_mips_test.c -o hello_mips_test.i mipsel-linux-c++ hello_mips_test.i -o hello_mips_test.s mipsel-linux-as hello_mips_test.s -o hello_mips_test.o mipsel-linux-ld hello_mips_test.o -o hello_mips_test.out
四个步骤的说明: