-1

I am using g++ compiler. I am getting the errors mentioned below. Kindly let me know what my mistakes are, or any other function to get hold on position of the cursor. Here's my code:

#include <ncurses.h>            /* ncurses.h includes stdio.h */  
#include <string.h>
#include<iostream>
using namespace std;
 
int main()
{
 char mesg[]="Just a string";       /* message to be appeared on the screen */
 int row,col;               /* to store the number of rows and *
                     * the number of colums of the screen */
 initscr();             /* start the curses mode */
 getmaxyx(stdscr,row,col);      /* get the number of rows and columns */
 mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg);
                                    /* print the message at the center of the screen */
 mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col);
 printw("Try resizing your window(if possible) and then run this program again");
 refresh();
 getch();
 endwin();

 return 0;
}```

/*Here are the errors;

/tmp/ccJV4hnK.o: In function `main':
try9.cpp:(.text+0x34): undefined reference to `initscr'
try9.cpp:(.text+0x3b): undefined reference to `stdscr'
try9.cpp:(.text+0x47): undefined reference to `stdscr'
try9.cpp:(.text+0x60): undefined reference to `stdscr'
try9.cpp:(.text+0x6c): undefined reference to `stdscr'
try9.cpp:(.text+0xbf): undefined reference to `mvprintw'
try9.cpp:(.text+0xe4): undefined reference to `mvprintw'
try9.cpp:(.text+0xf3): undefined reference to `printw'
try9.cpp:(.text+0xf8): undefined reference to `refresh'
try9.cpp:(.text+0xff): undefined reference to `stdscr'
try9.cpp:(.text+0x107): undefined reference to `wgetch'
try9.cpp:(.text+0x10c): undefined reference to `endwin'
collect2: error: ld returned 1 exit status*/
Attock
  • 11
  • 1

3 Answers3

1

I ran into the same problem.. You just have to add -lncurses behind the compilation command..

So for example:

clang test.c -lncurses

gcc test.c -lncurses

Noname
  • 69
  • 7
0

Simply add

#include <curses.h>
Eljas Hyyrynen
  • 233
  • 1
  • 11
  • i added that now. It is giving same errors. – Attock Dec 14 '20 at 16:24
  • A little googling got me this https://stackoverflow.com/questions/35216344/make-cant-find-curses-h Could you try `sudo apt-get install libncurses5-dev libncursesw5-dev` if you're using ubuntu? – Eljas Hyyrynen Dec 15 '20 at 10:01
  • @Attock Could you please edit your original post to include your makefile and build command? Maybe that's the key in this issue, not the code itself. – Eljas Hyyrynen Dec 15 '20 at 10:02
  • @Elijas Hyyrynen i have already installed ncurses in ubuntu. and im using ncurses as well as curses.h. it is not working. Can you please tell me some other function that i can use to get cursor control? – Attock Dec 16 '20 at 11:23
  • https://stackoverflow.com/questions/54250401/how-to-control-a-cursor-position-in-c-console-application https://stackoverflow.com/questions/38770996/linux-moving-the-console-cursor-visual https://stackoverflow.com/questions/26423537/how-to-position-the-input-text-cursor-in-c – Eljas Hyyrynen Dec 18 '20 at 07:25
  • Do you want to move the text cursor or mouse cursor? Can you edit your original post and show your makefile? The problem is probably there – Eljas Hyyrynen Dec 18 '20 at 07:25
  • @Elijas Hyyrynen thank you very much. I've found my mistake. May you be blessed. – Attock Dec 19 '20 at 08:05
  • @Attock can you please post the steps to solve your problem here so somebody else having the same problem could profit from that? Anyway good to hear you solved it :) – Eljas Hyyrynen Dec 19 '20 at 17:49
  • @Elijas Hyyrynen my compiler required to be upgraded I guess. I uninstalled and installed it again.. fortunately it worked out somehow!! – Attock Dec 20 '20 at 18:20
-1

An “Undefined Reference” error occurs when we have a reference to object name in our code and the linker is not being able to find the definition when it tries to search for your functions initscr, stdscr, mvprintw, etc in all the linked object files and libraries. You must have missed including the appropriate header files.

#include <curses.h>  
Maharshi
  • 106
  • 1
  • 9