Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

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!




Sunday, January 4, 2009

A few assumptions

Alright, the fun is about to start.
Sorry for delaying the actual Perl programming part of this blog and thank you for your patience. I just wanted to make sure that I would actually have an audience before starting head first. Now that Google analytics is installed (more in a later post), I'll be able to monitor how many people come and pay me a visit! If you don't want to miss any post, please consider subscribing to this blog (links to the right). For Firefox users, I recommend the Sage feed reader (I like it).

I am monitoring the comments by email so even if you drop a line or two on a previous post, I will be seeing it (and possibly replying to it in time).

I am going to make a few assumptions about my readership. Please correct me if I'm wrong:
  1. Some of you have no experience in programming, or very little.
  2. You like to learn by getting your hands dirty: no lengthy and theoretical lecture but practice on subjects that interest you.
  3. You think adding Perl in your toolbox can help you with your computer skills. Improve your blog, parse web pages and add a line to your resume while having fun!
  4. You know how to use a computer. You can open a directory to check for files and use the basic features of your operating system.

  5. Side note for Linux users:
    Perl is present in most standard Linux distributions. In order to check the Perl version that you are using, open a terminal and type:
    perl -v
    Typing
    which perl
    will show in which directory the Perl interpreter is located.

    From now on, I will also assume that:
  6. You are using a Windows operating system (Vista, XP, NT or 98). I am personally using Vista. Google analytics tells me that most of you use Windows, except for the one person who surfs on his iPhone. I know who you are!
Perl is a very portable language and I will do my best to write scripts that run on both Windows and Linux (and possibly Mac).

The first exercize is to choose and download a Perl distribution for Windows. We have two main options:
I let you research the web for the distribution that you prefer. Both are free.
I already have my opinion on the question but I would like your take on the subject.
I will open a second poll in the right-hand side of this post for you, or you can just leave a comment. I will go with the popular vote.

French word of the day:
"une découverte": a discovery

Next posts:
  • What's that Blogroll thing that you added?
  • How to get Perl and set up your Windows environment?
  • How to install Google Analytics on your Blogger blog
  • Hello World program
  • How to publicize your blog - Part II
  • First version of FileInfo script

A few assumptions

Alright, the fun is about to start.
Sorry for delaying the actual Perl programming part of this blog and thank you for your patience. I just wanted to make sure that I would actually have an audience before starting head first. Now that Google analytics is installed (more in a later post), I'll be able to monitor how many people come and pay me a visit! If you don't want to miss any post, please consider subscribing to this blog (links to the right). For Firefox users, I recommend the Sage feed reader (I like it).

I am monitoring the comments by email so even if you drop a line or two on a previous post, I will be seeing it (and possibly replying to it in time).

I am going to make a few assumptions about my readership. Please correct me if I'm wrong:
  1. Some of you have no experience in programming, or very little.
  2. You like to learn by getting your hands dirty: no lengthy and theoretical lecture but practice on subjects that interest you.
  3. You think adding Perl in your toolbox can help you with your computer skills. Improve your blog, parse web pages and add a line to your resume while having fun!
  4. You know how to use a computer. You can open a directory to check for files and use the basic features of your operating system.

  5. Side note for Linux users:
    Perl is present in most standard Linux distributions. In order to check the Perl version that you are using, open a terminal and type:
    perl -v
    Typing
    which perl
    will show in which directory the Perl interpreter is located.

    From now on, I will also assume that:
  6. You are using a Windows operating system (Vista, XP, NT or 98). I am personally using Vista. Google analytics tells me that most of you use Windows, except for the one person who surfs on his iPhone. I know who you are!
Perl is a very portable language and I will do my best to write scripts that run on both Windows and Linux (and possibly Mac).

The first exercize is to choose and download a Perl distribution for Windows. We have two main options:
I let you research the web for the distribution that you prefer. Both are free.
I already have my opinion on the question but I would like your take on the subject.
I will open a second poll in the right-hand side of this post for you, or you can just leave a comment. I will go with the popular vote.

French word of the day:
"une découverte": a discovery

Next posts:
  • What's that Blogroll thing that you added?
  • How to get Perl and set up your Windows environment?
  • How to install Google Analytics on your Blogger blog
  • Hello World program
  • How to publicize your blog - Part II
  • First version of FileInfo script