понедельник, 18 октября 2010 г.

Programming C/C++ in Linux Pt.2 (Linking with libs)

Secons part is about linking...

For example i want to link with ncurses library that is called libncurses (lib prefix)

I have such code:


#include <time.h>
#include <curses.h>

int current_getch;
int doloop = 1;
static WINDOW *mainwnd;
static WINDOW *screen;
WINDOW *my_win;

int now_sec, now_min, now_hour, now_day, now_wday, now_month, now_year;
time_t now;
struct tm *now_tm;
 
void screen_init(void) {
   mainwnd = initscr();
   noecho();
   cbreak();
   nodelay(mainwnd, TRUE);
   refresh(); // 1)
   wrefresh(mainwnd);
   screen = newwin(13, 27, 1, 1);
   box(screen, ACS_VLINE, ACS_HLINE);
}

static void update_display(void) {
   curs_set(0);
   mvwprintw(screen,1,1,"-------- HEADER --------");
   mvwprintw(screen,3,6,"TIME: %d:%d:%d", now_hour, now_min, now_sec);
   mvwprintw(screen,5,6,"DATE: %d-%d-%d", now_day, now_month, now_year);
   mvwprintw(screen,7,6,"PRESS q TO END");
   mvwprintw(screen,10,1,"-------- FOOTER --------");
   wrefresh(screen);
   refresh();
}

void screen_end(void) {
   endwin();
}

void maketime(void) {
 // Get the current date/time
 now = time (NULL);
 now_tm = localtime (&now);
   now_sec = now_tm->tm_sec;
 now_min = now_tm->tm_min;
 now_hour = now_tm->tm_hour;
 now_day = now_tm->tm_mday;
 now_wday = now_tm->tm_wday;
 now_month = now_tm->tm_mon + 1;
 now_year = now_tm->tm_year + 1900;
}

int main(void) {
   screen_init();
   while (doloop) {
      current_getch = getch();
      if (current_getch == 113) {
         doloop = 0;
      }
      maketime();
      update_display();
      sleep(1);
   }
   screen_end();
   printf("TEST ENDS\n");
   return 0;
}

I just compile it with gcc using -l argument with library name.For my example:

gcc main.c -o progr -lncurses
This will links my program with library libncurses (note lib prefix)


If it havent found library or cant link then check directories for gcc and if that library is installed (-dev postfix).
You can give gcc libraries directory location by -L argument. (-L /usr/lib )

Thats all.

Note. NCurses is library for providing an API, allowing the programmer to write text user interfaces in a terminal-independent manner.
Such programs writteln in ncurses: mc,aptitude,yast etc.

Комментариев нет:

Отправить комментарий