Sunday, March 15, 2015

Experimenting with ncurses on Linux in Eclipse

Today I'll talk about a new project inspired by a co-worker: to write a "graphical" application to monitor network traffic from the console of an embedded device such as a data gateway or a raspberry pi.
The goal is not to produce a star software but to get back in shape programming with C after years of Perl and Python and learning about ncurses along the way.


A few words about ncurses

ncurses is a C library allowing you to abstract the terminal type while writing ASCII graphical interface. It is used in the Linux kernel menuconfig utility for example.
More information about ncurses can be found on the GNU ncurses page and its wikipedia article.


Setting up the environment

I will start with a Hello World program inspired directly from the Linux documentation project.
#include <stdlib.h>
#include <curses>

int main(void)
{
    initscr();
    printw("Hi!\n");
    refresh();
    getch();
    endwin();

    exit(0);
}

      1. Installation of the ncurses library

Your Linux distribution should come with ncurses already installed. If it is not there, follow the nstructions found in paragraph "1.3. Where to get it" of the tldp ncurses how-to webpage.


      2. Creating a new Eclipse project

I am using the Kepler version of Eclipse. First navigate through the top menu commands:
File > New > C project
Then give your project a name (mine is curses), project type = "empty project", toolchains = "Linux GCC" (for building an executable for your dev machine).
Copy-paste or type the Hello World program above in main.c file for example.


      3. Adding the ncurses library to the ld path

Right-click on your project name from the project explorer window and select the bottom option for Properties.
In "C/C++ General" section, select "Paths and Symbols" then click on the "Libraries" tab.
Add ncurses to the list. End result should look as shown below:

Adding the ncurses library to your project


      4. Building your project

Nothing tricky here: Right-click on your project name then "Build Project" or use the hammer icon from the top menu. Build result, as well as gcc command line, will be displayed in a console:

Project has been built


      5. Launching a terminal from Eclipse

To avoid switching back and forth between Eclipse and your terminal, I have used a trick found on stack Overflow: from the top menu, select Run > External Tools > External Tools Configuration...
Create a new configuration (here: Command_prompt) with the location of your terminal binary, the directory where your executable is and additional arguments for your terminal program to execute your program:

Adding external tool configuration for your terminal


To launch your program, Run > External Tools > Command_prompt (or the name you gave your configuration) and a terminal window should appear with your ncurses program running.

Run Command_prompt opens a new terminal with your program


That's it. You are now set to explore ncurses tutorials that you can find on the web!




No comments :

Post a Comment