Arch Linuxでarmのクロスコンパイル環境作るのは割と簡単だったのめも

Arch Linuxでarmのクロスコンパイル環境作るのはどうやるんだろと思ったけどかなり簡単だった。
archlinuxarm.orgにあるこの「Distcc Cross-Compiling」を見ればOKというレベル。

crosstool-ngというツールチェインをインストールする必要があるのだけれど、ホストがx86_64だったらコンパイル済みのバイナリがあるらしいので今回はそれを利用。tar.xz形式のファイルをダウンロードして適当なディレクトリに展開するだけ。必要に応じてPATHを設定しましょう。

では、適当なテストプログラムを書いて、

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>

#define NUM_TEST_THREADS 10

struct sleep_setting {
        int id;
        int sec;
};

static void *
hello(void *arg)
{
        struct sleep_setting *ss = arg;
        int i;

        for (i = 0; i < 3; i++) {
                printf("thread %d : %d\n", ss->id, i);
                sleep(ss->sec);
        }

        free(ss);
        return NULL;
}

int 
main(int argc, char **argv)

        free(ss);
        return NULL;
}

int 
main(int argc, char **argv)
{
        pthread_t *ths;
        int i;

        srand(time(NULL));

        ths = malloc(sizeof(pthread_t) * NUM_TEST_THREADS);
        if (!ths) {
                perror("malloc");
                exit(-1);
        }

        for (i = 0; i < NUM_TEST_THREADS; i++) {
                struct sleep_setting *ss = malloc(sizeof(*ss));
                if (!ss) {
                        perror("malloc");
                        exit(-1);
                }
                ss->id = i;
                ss->sec = rand() % 3;
                pthread_create(&ths[i], NULL, &hello, ss);
        }

        for (i = 0; i < NUM_TEST_THREADS; i++)
                pthread_join(ths[i], NULL);

        free(ths);

        return 0;
}

コンパイル実行。

masami@saga:~/arm_cross_tools$ uname -a
Linux saga 3.13.0-rc5-ktest+ #32 SMP PREEMPT Sat Dec 28 19:03:02 JST 2013 x86_64 GNU/Linux
masami@saga:~/arm_cross_tools$ ./x-tools6h/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-gcc hello.c -Wall -lpthread
masami@saga:~/arm_cross_tools$ file a.out
a.out: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.27, not stripped

出来たバイナリをRaspberry Piにscpで転送して実行してみる。

masami@saga:~/arm_cross_tools$ scp a.out masami@mars:~/.
a.out                                                                                                                                  100% 7449     7.3KB/s   00:00    
masami@saga:~/arm_cross_tools$ ssh masami@mars
Last login: Sun Dec 29 03:11:28 2013 from 192.168.1.26
[masami@mars ~]$ uname -a
Linux mars 3.10.18-1-ARCH+ #1 PREEMPT Tue Nov 12 00:08:16 CST 2013 armv6l GNU/Linux
[masami@mars ~]$ ./a.out
thread 2 : 0
thread 3 : 0
thread 1 : 0
〜略〜

(o・∀・)b゙イィ!

Raspberry Pi Type B 512MB

Raspberry Pi Type B 512MB