Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Statically link ncurses to program

I'm having some problems statically linking ncurses to one of my programs

Here's a really simple sample program:

#include<ncurses.h>


int main(){

    initscr();
    printw("Hello world\n");
    refresh();
    getch();
    endwin();
    return 0;
}

When I compile it with

gcc -static -lncurses hello_curses.c -o curses

I get these errors:

/tmp/ccwHJ6o1.o: In function `main':
curses_hello.c:(.text+0x5): undefined reference to `initscr'
curses_hello.c:(.text+0x14): undefined reference to `printw'
curses_hello.c:(.text+0x1b): undefined reference to `stdscr'
curses_hello.c:(.text+0x20): undefined reference to `wrefresh'
curses_hello.c:(.text+0x27): undefined reference to `stdscr'
curses_hello.c:(.text+0x2c): undefined reference to `wgetch'
curses_hello.c:(.text+0x31): undefined reference to `endwin'
collect2: ld returned 1 exit status

I'm a little confused why this isn't working. What am I missing here?

like image 540
mdogg Avatar asked Aug 18 '10 17:08

mdogg


1 Answers

You need to pass -l options at the end of the command line:

gcc -static hello_curses.c -o curses -lncurses

When the compiler encounters -lfoo, it links in all the symbols from foo that have been requested by a previous file. If you put -lfoo at the beginning, no symbol has been requested yet, so no symbol gets linked.

like image 64
Gilles 'SO- stop being evil' Avatar answered Oct 25 '22 07:10

Gilles 'SO- stop being evil'