Example of Native GCC for Yun

Help compile C (sshpass) application:


opkg update
opkg install tar

wget http://pkgs.fedoraproject.org/repo/pkgs/sshpass/sshpass-1.05.tar.gz/c52d65fdee0712af6f77eb2b60974ac7/sshpass-1.05.tar.gz

tar -zxvf sshpass-1.05.tar.gz

cd sshpass-1.05

make install

Help Compile/Installing Python Modules:

Two type Python Modules; The first type is the pure Python one will be easily install by

pip install #modulename

or even simply copy/download python files into proper location.


The second type is C-API which is needed compile, gcc will help.

Sample Install crcmod:

Install python_devel

cd /usr/include/python2.7
wget -O  python_devel.tar.gz https://www.dropbox.com/s/z49hrigv3umaaj8/python_devel.tar.gz?dl=0 --no-check-certificate
tar -zxvf python_devel.tar.gz 
rm python_devel.tar.gz

Download crcmod

cd /mnt/sda1
wget https://pypi.python.org/packages/source/c/crcmod/crcmod-1.7.tar.gz --no-check-certificate
tar -zxvf  crcmod-1.7.tar.gz
cd crcmod-1.7
python setup.py build
gcc -fno-strict-aliasing -Os -pipe \
-mips32r2 -mtune=mips32r2 -fno-caller-saves  \
-Wno-error=unused-but-set-variable -msoft-float -DNDEBUG -Os \
-fPIC -I/usr/include/python2.7 \
-c python2/src/_crcfunext.c -o build/temp.linux-mips-2.7/python2/src/_crcfunext.o

gcc -pthread -shared build/temp.linux-mips-2.7/python2/src/_crcfunext.o \
-L/usr/lib  -o build/lib.linux-mips-2.7/crcmod/_crcfunext.so
python setup.py install

Install driver, library, application for Yun.

The freetds-dev is open source library for Sybase and Microsoft SQL server.

wget ftp://ftp.freetds.org/pub/freetds/current/freetds-dev.0.92.812.tar.gz
tar -zxvf freetds-dev.0.92.812.tar.gz
cd  freetds-dev.0.92.812
make install

Here you need take very long coffee break at stage "make"!

Testing connect to MS SQL Server

tsql -H192.168.0.111 -p 1433  -U sa -P password
locale is "C"
locale charset is "ASCII"
using default charset "ISO-8859-1"
1> use AdventureWorks2008
2> go
1> SELECT * FROM Person.Address where AddressID=1
2> go
AddressID       AddressLine1    AddressLine2    City    StateProvinceID PostalCode      SpatialLocation rowguid ModifiedDate
1       1970 Napa Ct.   NULL    Bothell 79      98011   e6100000010cae8bfc28bce4474067a89189898a5ec0    9AADCB0D-36CF-483F-84D8-585C2D4EC6E9    Jan  4 1998 12:00:00:000AM
(1 row affected)

Now Yun is able to directly talk to Sybase and Microsoft SQL server.

How to read serial data in Yun with C


one thing to note - try avoiding doing stuff in the main file system on the Yun; remember it is flash memory and has a limit of how many writes it can handle before going kaput. best to use a mounted SD card (i formatted mine with ext3 file system) and/or the /tmp directory which is mapped to a RAM drive i believe. so many cool things you can do with the Linux side of the Yun :P

Compile and install Lua Module(C API)

Install lua_devel:

cd /usr/local/include
wget -O lua_devel.tar.gz https://www.dropbox.com/s/66zxd9gvdf7lvib/lua_devel.tar.gz?dl=0 --no-check-certificate
tar -xzf lua_devel.tar.gz
rm lua_devel.tar.gz

Download and compile lua-cjson:

cd /mnt/sda1/
wget http://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz
tar -xzf lua-cjson-2.1.0.tar.gz
cd /mnt/sda1/lua-cjson-2.1.0
gcc -c -O3 -Wall -pedantic -DNDEBUG  -I/usr/local/include -fpic -o lua_cjson.o lua_cjson.c
gcc -c -O3 -Wall -pedantic -DNDEBUG  -I/usr/local/include -fpic -o strbuf.o strbuf.c
gcc -c -O3 -Wall -pedantic -DNDEBUG  -I/usr/local/include -fpic -o fpconv.o fpconv.c
gcc  -shared -o cjson.so lua_cjson.o strbuf.o fpconv.o

Install lua-cjson:

cp cjson.so /usr/lib/lua/

Testing lua-cjson:

cd /mnt/sda1/
nano arduino.lua
-------simple data-------
local tab ={}
tab["arduino"] = "arduino.cc"
--encode json -------
local cjson = require "cjson"
local jsonData = cjson.encode(tab)

-- print result:  {"arduino":"arduino.cc"}

--decode json -------
local data = cjson.decode(jsonData)

-- print result:  arduino.cc
root@Arduino:/mnt/sda1# chmod 755 arduino.lua
root@Arduino:/mnt/sda1# ./arduino.lua

lua-cjson (C-API) V.S. pure lua json module performance boost 1000% to 2000%!

C++ compile:

nano hello.cpp
// 'Hello World!' program
#include <iostream>
int main()
  std::cout << "Hello World!" << std::endl;
  return 0;
g++  hello.cpp -o hello
root@Arduino:/mnt/sda1# ./hello
./hello: can't load library 'libstdc++.so.6'

if I recompile it with static link

g++  hello.cpp -o hello  -static

Every thing is working, but obviously that is not I want.

After google ,

opkg update
opkg install libstdcpp
g++  hello.cpp -o hello
root@Arduino:/mnt/sda1# ./hello
Hello World!

GNU utilities:

"file" Utility - Determine File Type:

opkg update
opkg install file
root@Arduino:/mnt/sda1# file hello
hello: ELF 32-bit MSB executable, MIPS, MIPS32 rel2 version 1, dynamically linked (uses shared libs), 
with unknown capability 0x41000000 = 0xf676e75, with unknown capability 0x10000 = 0x70403, not stripped

The Grep from BusyBox is not complete:

Download latest Grep here(For testing purpose only):

wget -O grep_2.14-1_ar71xx.ipk https://www.dropbox.com/s/a9zuwmh3mipj7jt/grep_2.14-1_ar71xx.ipk?dl=0 --no-check-certificate

opkg install grep_2.14-1_ar71xx.ipk

GNU utilities:

The utility "nm" lists symbol table of object files.

root@Arduino:/mnt/sda1# nm hello
00400180 r _DYNAMIC
00400a70 t _GLOBAL__sub_D_main
00400a38 t _GLOBAL__sub_I_main

GNU Binary Utilities - objdump: objdump - display information from object files.

opkg update
opkg install objdump

sonnyyu: option fstype        ext3

for application which do a lot of read use ext4, for a lot of write use ext2. no go for ext3.

ext4 has speed advantage at reading , ext2 has speed advantage at writing. select base on which one you are looking after.

Compile barcode program:

nano /mnt/sda1/barcode.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>

#define KEY_PRESS 1
int main (int argc, char *argv[])
struct input_event ev[64];
int fd, rd,size = sizeof (struct input_event);
char name[256] = "Unknown",i;
char *device = NULL;
char decode_scancode[]={0,0,1,2,3,4,5,6,7,8,9,0};
if (argv[1] == NULL){
printf("Please enter path to device\n");
return 1;
if ((getuid ()) != 0)
printf ("You must be as root for correct work!\n");
if (argc > 1)
device = argv[1];
if ((fd = open (device, O_RDONLY)) == -1){
printf ("Open error of device %s\n", device);
return 1;
ioctl (fd, EVIOCGNAME (sizeof (name)), name);
printf ("Reading From : %s (%s)\n", device, name);
while (1){
if ((rd = read (fd, ev, size * 64)) < size){
perror ("Read error");
return 1;
for (i=0; i< ((int)(rd/size)); i++) {
if (ev[i].type == EV_KEY) {
if ((ev[i].value == KEY_PRESS) || (ev[i].value == KEY_KEEPING_PRESSED)) {
if (ev[i].code < 12) {
printf ("%d", (decode_scancode[ev[i].code]));
else if (ev[i].code == 28) {
printf ("\n");
return 0;
gcc -o barcode barcode.c

barcode excutable for Yun:

set up arbitrary-baud at Linux:

nano arbitrary-baud.c
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <linux/termios.h>

int ioctl(int d, int request, ...);

int main(int argc, char *argv[])
  struct termios2 t;
  int fd,baud;

  if (argc != 3)
      fprintf(stderr,"usage: %s <device> <baud>\n", argv[0]);
      return 1;

  fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY);

  if (fd == -1)
        fprintf(stderr, "error opening %s: %s", argv[1], strerror(errno));
        return 2;

  baud = atoi(argv[2]);

  if (ioctl(fd, TCGETS2, &t))
      return 3;

  t.c_cflag &= ~CBAUD;
  t.c_cflag |= BOTHER;
  t.c_ispeed = baud;
  t.c_ospeed = baud;

  if (ioctl(fd, TCSETS2, &t))
      return 4;

  if (ioctl(fd, TCGETS2, &t))
      return 5;

  printf("actual speed reported %d\n", t.c_ospeed);
  return 0;
gcc -o arbitrary-baud arbitrary-baud.c

download Yun excutable here:

@sonnyyu- you mark the grep download as for testing

Download latest Grep here For testing purpose only

What is the reason? Why is it not for general use?

I don’t recall what stopped me to compile grep by Native GCC for Yun, but the link of package is cross compile. I guess every one find it working then for general use.

To complete example, here is hello world C example.

nano hello.c
//C hello world example
#include <stdio.h>
int main()
  printf("Hello world\n");
  return 0;
gcc -Wall hello.c -o hello

Hello world

These are great examples, thank you! Between these and noblepepper's blog on the subject, I had no problems getting things going (once I got over my hesitation and actually tried!)

What's missing from these examples is accessing the Yun's bridge from C or C++. Is it possible to get/put bridge values or access the bridge mailbox mechanism from C or C++? (lots of Python and PHP examples, but I can't find any in C.)

sonnyyu: Since UART's speed is only 0.5Mb/s and will be bottleneck, compiled programming languages assembly/c/c++ has no advantage than script languages php/python/lua. UART is interface between Yun and Arduino.

sonnyyu: Profile bridge speed:

Arduino Yun Bridge example from IDE.

ab - Apache HTTP server benchmarking tool


ab -n 20 -c 1

Concurrency Level:      1 Time taken for tests:   3.425 seconds Complete requests:      20 Failed requests:        0 Write errors:           0 Non-2xx responses:      20 Total transferred:      2100 bytes HTML transferred:       0 bytes Requests per second:    5.84 #/sec Time per request:       171.275 ms Time per request:       171.275 ms Transfer rate:          0.60 [Kbytes/sec] received

Connection Times (ms)              min  mean[+/-sd] median   max Connect:        0    0   0.0      0       0 Processing:   164  171   7.9    169     192 Waiting:      153  159   6.2    156     171 Total:        164  171   7.9    169     192

171.275 [ms] (0.17 s) is average speed of bridge speed, and **should meet average application's requirement**.

0.17 samples/s and 0.5Mb/s make compiled programming languages assembly/c/c++ is not good candidates.

sonnyyu: 0.17 samples/s and 0.5Mb/s make compiled programming languages assembly/c/c++ is not good candidates.

Understood that the bridge is a bottleneck, and the increased speed of C over Python is not necessarily an advantage. But that doesn't mean that there is no place for C or C++. Perhaps there are other things that must be done at a high speed, and the Bridge portion of it is a minor percentage. Or an existing application is being ported to the Yun, and rewriting it in Python is a large effort. Or the code uses a library with a C interface that is not available (or too cumbersome) to use in Python. Or the coder is just more comfortable with C and would prefer using it. Or...

There are many valid reasons for wanting to use C or C++ that go beyond speed.

Edit: I recently posted the same basic question in another thread, where I stated that I had a C application that needed to post to the Bridge mailbox, and which wasn't practical to rewrite in Python. After spending way too much time researching libraries and rewriting code, I was able to get it working in Python, but it would've been a much more elegant solution if it stayed in C. And the next project might be much more work to translate, just to get around the lack of a C Bridge interface.