Compiling Native Code

Likewise, if codes you wish to run on the COM do not have a Bitbake recipe, and you would rather compile directly from the source, try compiling it natively on a COM.  This article explains how to install native gcc/g++ on your COMs.  Please note that the exact instructions depend on the operating system running on your COM.

For larger projects, you might consider cross-compilation or the OpenEmbedded Cross build system .  Many standard software packages are available using the package manager built-in to your OS; for instructions on the opkg package manager for Angstrom, see this page.

Angstrom

On Angstrom, use the built-in opkg package managment tool to fetch a meta-package consisting of all the necessary components for a C/C++ SDK. Users are advised not to manually install gcc, libgcc etc. as resolving the dependencies correctly can be challenging.

$ opkg install task-native-sdk

Ubuntu

For Ubuntu users, we recommend using the package manager to install the 'build-essential' meta-package consisting of all the necessary components for a C/C++ SDK.

$ sudo apt-get install build-essential

Android

Android ships with a stripped-down version of libc called bionic. While it is theoretically possible to natively compile on Android, the Android build system and the ADB tool make it easy to rapidly cross-compile, deploy and test your source code from your host machine. More information can be found at source.android.com

Note: When doing native development, users may consider using a GNOME image produced by Steve Sakoman. It comes with an environment similar to a typical linux desktop distribution and the SDK is pre-installed.

With a compiler installed, we can write some test code.

C++ Hello World Sample

Try the familiar Hello World as a sample C++ program to test our new compiler.

Begin by creating a new file, hello.cpp, with the following content:

#include <iostream>
using namespace std;

int main() {
cout << "Gumstix can natively compile C++" << endl;
return 0;
}

Now, from the console, compile and test your code.

$ g++ -o hello hello.cpp
$ ./hello
Gumstix can natively compile C++

Kernel Module Sample

We can also download C code and directly compile a kernel module to insert into our running system; your COM needs to be connected to the internet and your kernel should support loadable modules (you would know if you changed from this default). Let's compile Dave Hylands's GPIO event driver so we can test it by toggling an LED.

Begin by downloading the code and installing the required linux-headers using your package manager.

$ wget http://svn.hylands.org/linux/gpio-event/module/gpio-event-drv.c
$ wget http://svn.hylands.org/linux/gpio-event/module/gpio-event-drv.h

Angstrom

$ opkg install kernel-headers

Ubuntu

$ sudo apt-get install linux-libc-dev

Now, we can compile this code and load it into our kernel:

$ make -C /usr/linux -M=`pwd` modules
$ insmod gpio-drv.ko

You have now installed a new kernel module.  Test it by using the module to toggle the GPIO 12 line.  With many expansion boards, this will flash the blue LED, but failing this, probe the line on the 40-pin header with a multimeter or an oscilloscope.

Notes

If you prefer using other languages such as Python this guide may be usefulIt is possible to build substantial projects such as the Linux kernel natively, however it can be slow compared to cross-compilation.  Another option is to use DistCC to distribute compile jobs across multiple computers.  Using a stagecoach board, for example, offers a significant boost in speed.

Home Compile code