Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building HelloWorld C++ Program in Linux with ncurses

Tags:

linux

ncurses

I successfully ran sudo apt-get install libncurses5-dev

Within my Eclipse window I then try to build the following HelloWord.cpp program:

#include <ncurses.h>

int main()
{
    initscr();                 /* Start curses mode     */
    printw("Hello World !!!"); /* Print Hello World    */
    refresh();                 /* Print it on to the real screen */
    getch();                   /* Wait for user input */
    endwin();                  /* End curses mode    */

    return 0;
}

I get the following error:

Invoking: GCC C++ Linker
g++ -m32 -lncurses -L/opt/lib -o "Test_V"  ./src/curseTest.o ./src/trajectory.o ./src/xJus-epos.o   -lEposCmd
/usr/bin/ld: cannot find -lncurses
collect2: error: ld returned 1 exit status
make: *** [Test_V] Error 1

It looks like the compiler is searching for the ncurses library and can't find it? I checked /usr/lib and the library does not exist there so do I need to manually link the ncurses library there - I thought the get-apt installer would automatically do this?

like image 800
user2109105 Avatar asked Feb 25 '13 22:02

user2109105


2 Answers

g++ HelloWorld.cpp -lncurses -o HelloWolrd

If you have a 32-bit machine, gcc compile m32 auto. If you have a 64-bit machine and you want to compile 32bits you

like image 194
user3021733 Avatar answered Sep 18 '22 11:09

user3021733


Your arguments are not in the correct order. You must specify all source files first and then linker search directories before specifying the libraries to link with. Your command should be like this:

g++ HelloWorld.o -L/opt/lib -lncurses -o HelloWorld

Taken from comment by @ChrisDodd:

Your options are in the wrong order -- -L must be BEFORE -l and both must be after all .o

like image 29
Akib Azmain Avatar answered Sep 20 '22 11:09

Akib Azmain