Program show errors while using functions of curses.h

Asked by pepperblue

when i try to build a program using code::blocks in which ncurses are used, it shows some error.
For example in this program
#include<ncurses.h>

int main() {
    initscr();
    getch();
    wclear(stdscr);
}

It shows the error:
Compiling: /home/ansy/Desktop/asdf.cpp
Linking console executable: /home/ansy/Desktop/asdf
/home/ansy/Desktop/asdf.o: In function `main':
asdf.cpp:(.text+0x12): undefined reference to `initscr'
asdf.cpp:(.text+0x17): undefined reference to `stdscr'
asdf.cpp:(.text+0x1f): undefined reference to `wgetch'
asdf.cpp:(.text+0x24): undefined reference to `stdscr'
asdf.cpp:(.text+0x2c): undefined reference to `wclear'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
5 errors, 0 warnings

What should i do to rectify this problem?
Thank you in advance..
The Yash!!!

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Wong Yong Jie
Solved:
Last query:
Last reply:
Revision history for this message
Wong Yong Jie (yjwong) said :
#1

Hi,

I think you need to link your program against ncurses. You can add a -lncurses argument to GCC to do this:

$ g++ asdf.cpp -o asdf -lncurses

Regards.

Revision history for this message
Wong Yong Jie (yjwong) said :
#2

Ah, I realised you are using Code::Blocks instead of GCC directly. I'm not too sure how to do this in Code::Blocks though. They must have some "Linker options" somewhere.

Revision history for this message
pepperblue (ayashif) said :
#3

Ya, but i need to know what the "Linker Option" is. Because, I liked this tool very much as it can handle most of my requirements... I tried yours too, and thank; it's working.. Please say why does it happen and what is the necessity of linkin like this, i mean how does it rectify the aforesaid problem?
Thanks for your reply.
The Yash!!!

Revision history for this message
Wong Yong Jie (yjwong) said :
#4

Hi,

This might be a good read for you:
http://www.network-theory.co.uk/docs/gccintro/gccintro_17.html (a run-through of what linking is and why it is necessary)

I have installed Code::Blocks (Version 8.02, on Ubuntu 9.10 Karmic Koala). Not too sure if it's in the same version, but here goes:
1. Go to Project -> Build Options.
2. On the left pane, select your project name.
3. On the right pane, select the "Linker settings" tab.
4. Under "Link Libraries", click "Add".
5. Type in "ncurses" in the resulting "File" box and press OK.
6. Click "OK" to save your project settings.
7. Try a build (:

Should work

Regards.

Revision history for this message
pepperblue (ayashif) said :
#5

It is for a project. But i need it for individual files. i am not a professional, am a student.

Revision history for this message
Best Wong Yong Jie (yjwong) said :
#6

I see. I'm not too sure how to edit linker settings per-file though. However, you can change the global compiler settings (applies to any file that you open in Code::Blocks) via Settings -> Compiler and Debugger -> Linker settings.

Revision history for this message
pepperblue (ayashif) said :
#7

That solved my problem, Mr. Jie.. Thanks a lot. :)
The Yash!!!

Revision history for this message
pepperblue (ayashif) said :
#8

Thanks Wong Yong Jie, that solved my question.