1

Hope you are doing great!

I am using codeblocks 13.12 in my Elementary OS (Loki) which is based on ubuntu 16.04. Recently , I tried to use getch(), getche() function in my C-language coding and to do that i installed ncurses library in my system. When i try to build code, I get two error messages-

  1. Undefined reference to 'stdscr'
  2. Undefined reference to 'wgetch'

Here is my sample code:

#include'stdio.h'
#include'ncurses.h'
int main(){
    char ch;
    printf("Enter a character: ");
    ch=getch();
    printf("\nIts ASCII is : %d .",ch);
    return 0;
}        

What is the solution? I know i have to link the library to my source code file in command line.

Here is my command line execution :
gcc -o example_3_1_1test1 example_3_1_1.c -lncurses

complies without error

Enter a character: 
Its ASCII is : -1

which is wrong. Because my program don't take input from keyboard and automatically returns -1.

I want to build/complile my code from codeblocks automatically. So, how can i link ncurses library in codeblocks 13.12?

Thanks in advance.

Nazmul

Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44
nazmulwanted
  • 13
  • 1
  • 4

1 Answers1

0

setting > complier > search directories

Just click on add

enter image description here

I linked the boost library

enter image description here

and

enter image description here

and
put your -lncurses here as I put -pthread and std=c11

enter image description here

Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44
  • Thanks for quick response. I followed your instruction like this- https://i.stack.imgur.com/JDmNC.png and https://i.stack.imgur.com/l5HQJ.png Still I find errors - Undefined reference to 'stdscr' Undefined reference to 'wgetch' Any idea why? – nazmulwanted Feb 12 '17 at 16:38
  • @nazmulwanted You said: *I know i have to link the library to my source code file in command line* what **command** did you try? you should find the location of the file – Shakiba Moshiri Feb 12 '17 at 16:47
  • @nazmulwanted May help you [How do I link to a library with Code::Blocks?](http://stackoverflow.com/questions/5862757/how-do-i-link-to-a-library-with-codeblocks) – Shakiba Moshiri Feb 12 '17 at 16:52
  • here is my main code- #include #include int main(){ char ch; printf("Enter a character: "); ch=getch(); printf("\nIts ASCII is : %d .",ch); return 0; } and in command line i wrote this gcc -o example_3_1_1test1 example_3_1_1.c -lncurses which complies fine but output wrong. output is - 'code'Enter a character: Its ASCII is : -1 'code' but also in codeblocks shows mentioned error. – nazmulwanted Feb 12 '17 at 16:54
  • @nazmulwanted Oh no! put your code in your question not here! and see that like that I comment you – Shakiba Moshiri Feb 12 '17 at 16:55
  • ops! sorry! i am a newbie here. Sorry again. – nazmulwanted Feb 12 '17 at 16:58
  • @nazmulwanted Do not worry. **Just** edit your question and put your code in. if you made it messy I would fix it :) – Shakiba Moshiri Feb 12 '17 at 16:59
  • edited my question. please check. thanks for your help. – nazmulwanted Feb 12 '17 at 17:40
  • @nazmulwanted/ almost I find it you need to put `-lncurses` in the compiler – Shakiba Moshiri Feb 12 '17 at 17:44
  • i have to put -lncurses in which tab or option in the compiler settings? – nazmulwanted Feb 12 '17 at 17:55
  • @nazmulwanted See the new picture that I have put or follow **setting** and tab 2 **other complier option** – Shakiba Moshiri Feb 12 '17 at 17:57
  • Thanks a lot! It Works! Still small problem- getch() function is not working the way i wanted... if you check my code you will understand. My program supposed to take input from my keyboard and show ASCII value of it and still getting -1. Which is wrong. Any idea? – nazmulwanted Feb 12 '17 at 18:09
  • @nazmulwanted I am not familiar with `ncurses.h` but why do not you use `scanf` function for reading a char? like: `scanf( &ch );` – Shakiba Moshiri Feb 12 '17 at 18:17
  • i was just experimenting my code. I know about scanf, but i needed getch/getche function feature which takes character input and then my program goes immediately to next instruction without pressing enter key. for that I used conio.h in windows machine. But in linux gcc doest not provide conio.h. Btw Thanks a lot. You really helped me. :) – nazmulwanted Feb 12 '17 at 18:29
  • @nazmulwanted May help you [link](http://stackoverflow.com/questions/8792317/why-cant-i-find-conio-h-on-linux) ------- or this [link](https://github.com/nowres/conio-for-linux) – Shakiba Moshiri Feb 12 '17 at 19:18