Chinaunix首页 | 论坛 | 博客
  • 博客访问: 116076
  • 博文数量: 32
  • 博客积分: 2067
  • 博客等级: 大尉
  • 技术积分: 383
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-08 07:47
文章分类

全部博文(32)

文章存档

2011年(1)

2010年(14)

2009年(17)

我的朋友

分类: LINUX

2009-05-13 12:41:44

交叉编译工具链:

方法一(各linux版本通用):

参考:http://blog.sina.com.cn/s/blog_5a155e330100bqjc.html

1. 安装标准的C开发环境,由于Linux安装默认是不安装的,所以需要先安装一下(如果已经安装好的话,就可以免去这一步了):
  # apt-get install gcc g++ libgcc1 libg++ make gdb

2. 下载arm-linux-gcc-3.4.1.tar.bz2到任意的目录下(我看到有4.3.2了,friendlyarm提供的,我不贴下载链接,自己去下吧 )
 
 wget -c


3. 解压 arm-linux-gcc-3.4.1.tar.bz2
   #tar zxvf arm-linux-gcc-3.4.1.tar.bz2

  解压过程需要一段时间,解压后的文件形成了 usr/local/ 文件夹,进入该文件夹,将arm文件夹拷贝到/usr/local/下
  # cd usr/local/
  #cp -rv arm /usr/local/

  现在交叉编译程序集都在/usr/local/arm/3.4.1/bin下面了

4.  修改环境变量,把交叉编译器的路径加入到PATH。
 修改~/.bashrc文件,编辑.bash_profile也行
  #vim ~/.bashrc
在最后加上:
export PATH=$PATH:/usr/local/arm/3.4.1/bin
export PATH
 
 也可以:
#export PATH=$PATH:/usr/local/arm/3.4.1/bin

  注:(这只能在当前的终端下才是有效的!)

5. 立即使新的环境变量生效,不用重启电脑:
  #source /root/.bashrc



6. 检查是否将路径加入到PATH:

   # echo $PATH

  显示的内容中有/usr/local/arm/bin,说明已经将交叉编译器的路径加入PATH。至此,交叉编译环境安装完成。

7. 测试是否安装成功

# arm-linux-gcc -v

上面的命令会显示arm-linux-gcc信息和版本,这是我显示的信息:
 

Reading specs from /usr/local/arm/3.4.1/lib/gcc/arm-linux/3.4.1/specs
  Configured with: /work/crosstool-0.27/build/arm-linux/gcc-3.4.1-glibc-2.3.2/gcc-  3.4.1/configure --target=arm-linux --host=i686-host_pc-linux-gnu

 --prefix=/usr/local/arm/3.4.1 --with-headers=/usr/local/arm/3.4.1/arm

 -linux/include --with-local-prefix=/usr/local/arm/3.4.1/arm-linux --disable

  -nls --enable-threads=posix --enable-symvers=gnu --enable-__cxa_atexit --enable- languages=c,c++ --enable-shared --enable-c99 --enable-long-long
 Thread model: posix
 gcc version 3.4.1


8.编译Hello World程序,测试交叉工具链
写下下面的Hello World程序,保存为hello.c

    #include
    int main()
    {
    printf("Hello World!\n");
       return 0;
    }


执行下面的命令:

   # arm-linux-gcc -o hello hello.c

源程序有错误的话会有提示,没有任何提示的话,就是通过了,就可以下载到ARM目标板上运行了! 接着可以输入

file hello
的命令,查看生成的hello文件的类型,要注意的是生成的可执行文件只能在ARM体系下运行,不能在其于X86的PC机上运行

方法二(Emdebian):

emdebian的主页

它为debian的dpkg包管理系统提供了一套交叉编译开发工具,binutils,gcc,gdb等都有了

/etc/apt/sources.list里面加入:

#
# -- Emdebian sources.list entries
#
# deb unstable main
# deb-src unstable main
# deb testing main
# deb-src testing main
deb lenny main
deb-src lenny main

apt-get update
出现缺keyring
apt-get install emdebian-archive-keyring
apt-get update

然后aptitude search arm就能看到那些包了

网站上还讲到如何自己制作工具链
http://www.emdebian.org/tools/crossdev.html




linux下面做mini2440开发需要一些工具:


dnw2文件内容:

/* dnw2 linux main file. This depends on libusb.
 *
 * Author:     Fox
 * License:    GPL
 *
 */

#include
#include
#include
#include
#include
#include

#define         QQ2440_SECBULK_IDVENDOR        0x5345
#define        QQ2440_SECBULK_IDPRODUCT    0x1234


struct usb_dev_handle * open_port()
{
    struct usb_bus *busses, *bus;

    usb_init();
    usb_find_busses();
    usb_find_devices();

    busses = usb_get_busses();
    for(bus=busses;bus;bus=bus->next)
    {
         struct usb_device *dev;
        for(dev=bus->devices;dev;dev=dev->next)
        {
            if( QQ2440_SECBULK_IDVENDOR==dev->descriptor.idVendor
            &&  QQ2440_SECBULK_IDPRODUCT==dev->descriptor.idProduct)
            {
                printf("Target usb device found!\n");
                struct usb_dev_handle *hdev = usb_open(dev);
                if(!hdev)
                {
                    perror("Cannot open device");   
                }
                else
                {
                    if(0!=usb_claim_interface(hdev, 0))
                    {
                        perror("Cannot claim interface");
                        usb_close(hdev);
                        hdev = NULL;
                    }
                }
                return hdev;
            }
        }
    }
   
    printf("Target usb device not found!\n");

    return NULL;
}

void usage()
{
    printf("Usage: dnw2 \n\n");
}

unsigned char* prepare_write_buf(char *filename, unsigned int *len)
{
    unsigned char *write_buf = NULL;
    struct stat fs;

    int fd = open(filename, O_RDONLY);
    if(-1==fd)
    {
        perror("Cannot open file");
        return NULL;
    }
    if(-1==fstat(fd, &fs))
    {
        perror("Cannot get file size");
        goto error;
    }
    write_buf = (unsigned char*)malloc(fs.st_size+10);
    if(NULL==write_buf)
    {
        perror("malloc failed");
        goto error;
    }

    if(fs.st_size != read(fd, write_buf+8, fs.st_size))
    {
        perror("Reading file failed");
        goto error;
    }

    printf("Filename : %s\n", filename);
    printf("Filesize : %d bytes\n", fs.st_size);

    *((u_int32_t*)write_buf) = 0x30000000;        //download address
    *((u_int32_t*)write_buf+1) = fs.st_size + 10;    //download size;

    *len = fs.st_size + 10;
    return write_buf;

error:
    if(fd!=-1) close(fd);
    if(NULL!=write_buf) free(write_buf);
    fs.st_size = 0;
    return NULL;
   
}

int main(int argc, char *argv[])
{
    if(2!=argc)
    {
        usage();
        return 1;
    }

    struct usb_dev_handle *hdev = open_port();
    if(!hdev)
    {
        return 1;
    }

    unsigned int len = 0;
    unsigned char* write_buf = prepare_write_buf(argv[1], &len);
    if(NULL==write_buf) return 1;

    unsigned int remain = len;
    unsigned int towrite;
    printf("Writing data ...\n");
    while(remain)
    {
        towrite = remain>512 ? 512 : remain;
        if(towrite != usb_bulk_write(hdev, 0x03, write_buf+(len-remain), towrite, 3000))
        {
            perror("usb_bulk_write failed");
            break;
        }
        remain-=towrite;
        printf("\r%d%\t %d bytes     ", (len-remain)*100/len, len-remain);
        fflush(stdout);
    }
    if(0==remain) printf("Done!\n");
    return 0;
}

复制到文件dnw2.c
gcc dnw2.c -o dnw2 -lusb
即可
阅读(2855) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

Ubuntu初学者2012-03-28 11:41:52

你好 我按您的方法 到第7步,测试是否安装成功  也执行成功了 接下来测试程序时 出现
  /usr/local/arm/3.4.1/bin/../lib/gcc/arm-linux/3.4.1/../../../../arm-linux/bin/ld: cannot find -lgcc_s
collect2: ld returned 1 exit status

什么原因?谢谢你