The second bullet of this week’s 2-Bullet Tuesday! Check out the first bullet here. Subscribe to receive the newsletter on the 2-Bullet Tuesday page!

Omega Tip

Compiling C & C++ Applications on the Omega

Since the Omega is a Linux computer, it supports C and C++ programs. These languages are not interpreted, meaning that they need to be compiled into executable binaries before they can be run. So before you can start writing programs, we’ll need to install the gcc compiler for programming in C and the g++ compiler for programming in C++. We’ll cover the installation process and compile ‘hello world’ programs.

Let’s dive into the installation process! The compiler is quite large and requires more space than even the Omega2+ has on-board. So the first thing we’ll need to do is configure the Omega to boot from the external storage. See our booting from external storage article for the details.

We’ll then need to enable installation of software from the LEDE package repos since gcc and make are currently not in the Onion package repo. Open /etc/opkg/distfeeds.conf and uncomment the following two lines:

src/gz reboot_base http://downloads.lede-project.org/snapshots/packages/mipsel_24kc/base

src/gz rebot_packages http://downloads.lede-project.org/snapshots/packages/mipsel_24kc/packages

Basically line 2 and 5 of the file. Now, we are ready to install the compiler itself. Issue the following commands:

opkg update
opkg install gcc        #gcc compiler
opkg install make        #make utility

But we’re not done yet, it’s usually very useful to have a debugger as well:

opkg update
opkg install gdb

We are all set! Let’s write a simple C program. Create a file called helloworld.c and populate it with some code:

#include <stdio.h>

int main()
{
	printf(“Hello World”n”);
	printf(“We are running a C program on the Omega2!n”);
	return(0);
}

To compile the program, run the following command:

gcc helloworld.c -o helloworld

It will produce an executable binary file called helloworld with compiled code in it! Let’s run it:

root@Omega-4B6B:~# ./helloworld
Hello World!
We're running a C program on the Omega2!

Guess what’s next? Right, C++ programming! The gcc package that we installed also includes the g++ compiler for C++ programs. Let’s create a C++ file called helloworld.cpp and populate it with the following code:

#include <iostream>
using namespace std;

int main() {
   cout << "Hello World!" << endl;
   cout << "Now we're running a C++ program on the Omega2" << endl;
   return 0;
}

To compile it, we will use the following command, very similar to what we used for compiling C:

g++ helloworld.cpp -o helloworld2

Looks familiar, doesn’t it? Now let’s run our C++ compiled program:

root@Omega-665D:~# ./helloworld2
Hello World!
Now we're running a C++ program on the Omega2

And there you have it! You’re a C & C++ programmer!

If you go further and create more complex programs, the `make’ utility allows you to automate and easily work with multiple source code files. But more on that later!

Now you’re free to write C & C++ programs! This is handy since the executables run really, really quickly on the Omega, opening the door to a bunch of cool and more complex applications.

 

Thanks for reading & have a great week!

Have you seen the Omega2S, the smaller and surface-mount version of the Omega2 for high volume commercial and industrial OEMs? See our Omega2S page for more details!

Let us know what kind of stuff you would like to see featured on 2-Bullet Tuesday! Send a tweet to @OnionIoT with your suggestions!

P.S. We’re beyond excited to announce that the Omega2 family is now available on SparkFun!



© 2017 Onion Corporation

View web version | Unsubscribe here