为什么我在C++中的ncurse.h中出现这些错误



我使用的是g++编译器。我收到下面提到的错误。请让我知道我的错误是什么,或者任何其他函数来保持光标的位置。这是我的代码:

#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 columnsn",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*/

我遇到了同样的问题。。您只需要在编译命令后面添加-lncurses即可。。

例如:

clang test.c-lncurses

gcc test.c-lncurses

只需添加

#include <curses.h>

当我们的代码中有对对象名称的引用,并且链接器试图在所有链接的对象文件和库中搜索函数initscr、stdscr、mvprintw等时,无法找到定义时,就会发生"未定义引用"错误。您一定错过了包含适当的头文件。

#include <curses.h>  

相关内容

  • 没有找到相关文章

最新更新