Pemrograman C di OpenWrt 12.09 Attitude Adjusment
Berikut ini panduan untuk membuat aplikasi lintas-sistem (cross compiling) untuk router yang menjalankan Linux OpenWRT. Dalam tutorial ini sistem operasi yang ditargetkan adalah Linux OpenWRT 12.09. Kendati demikian, secara umum panduan ini berlaku juga untuk semua OpenWRT sampai versi terbaru.
Sistem Operasi
SDK (software development kit) OpenWrt 12.09 Attitude Adjusment hanya bisa dijalankan dari sistem operasi 32-bit. Pilihan sistem operasi terbatas pada:
- Ubuntu 16 32-bit
- ArchLinux 32-bit
- dll
Berikut ini langkah yang digunakan untuk Ubuntu 16.04
install software pendukung
sudo apt install git-core build-essential libssl-dev libncurses5-dev unzip gawk zlib1g-dev
Install SDK
Unduh SDK OpenwRT 12.09
cd /tmp
wget -c https://archive.openwrt.org/attitude_adjustment/12.09/ar71xx/generic/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2
tar xvf OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2
Buka paket dan letakkan di direktori bernama /home/pikachu/openwrt/12.09/sdk
mkdir -p /home/pikachu/openwrt/12.09/source
Pindahkan ke direktori yang sudah disiapkan
mv /tmp/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2 /home/pikachu/openwrt/12.09
Ubah nama direktori menjadi sdk
mv /home/pikachu/openwrt/12.09/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2 /home/pikachu/openwrt/sdk
Buat skrip variabel lingkungan bernama openwrt.conf untuk eksekusi compiler dari lokasi manapun:
sudo nano /etc/etc/openwrt.conf isinya:
export STAGING_DIR=/home/pikachu/openwrt/sdk/staging_dir
export TOOLCHAIN_DIR=$STAGING_DIR/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2
export LDCFLAGS=$TOOLCHAIN_DIR/usr/lib
export LD_LIBRARY_PATH=$TOOLCHAIN_DIR/usr/lib
export PATH=$TOOLCHAIN_DIR/bin:$PATH
Baca konfigurasi di atas sebelum kompilasi kode C/C++ dengan perintah:
source /etc/openwrt.conf
cek apakah compiler sudah terbaca:
mips-openwrt-linux-gcc --version
hasilnya:
mips-openwrt-linux-gcc (Linaro GCC 4.6-2012.02) 4.6.3 20120201 (prerelease)
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Cek compiler C++:
mips-openwrt-linux-g++ --version
Hasilnya:
mips-openwrt-linux-g++ (Linaro GCC 4.6-2012.02) 4.6.3 20120201 (prerelease)
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
program pertama
Buat program pertama:
cd /home/pikachu/openwrt/12.09/source
nano hello.c
Isinya:
# include < stdio.h >
int main()
{
printf("Hello World");
return 0;
}
Jalankan compiler C:
mips-openwrt-linux-gcc -Wall hello.c -o hello
Berkas hello harus dikirim ke sistem operasi Linux OpenWRT 12.09 supaya bisa dijalankan.
Kirim berkas hello ke router yang sudah terpasang OpenWRT 12.09 dan sudah menggunakan password. Apabila IP router adalah: 192.168.1.1 maka perintah pengiriman adalah:
scp hello root@192.168.1.1:/tmp/hello
Masuk ke OpenWRT:
ssh root@192.168.1.1
Setelah masuk ke router, pindah ke direktori /tmp dan jalankan aplikasi hello:
cd /tmp
chmod +x hello
./hello