【问题标题】:Why am I getting these errors in ncurses.h, in C++?为什么我在 C++ 中的 ncurses.h 中出现这些错误?
【发布时间】:2021-03-24 21:52:33
【问题描述】:

我正在使用 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 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*/

【问题讨论】:

  • 您是否忘记包含 ncurses 标头?
  • 似乎是因为您没有链接到 ncurses 库。您需要将-lncurses 添加到您的编译选项中。
  • 添加了您建议的库,但问题未解决

标签: c++ linux ubuntu g++


【解决方案1】:

简单添加

#include <curses.h>

【讨论】:

【解决方案2】:

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

例如:

clang test.c -lncurses

gcc test.c -lncurses

【讨论】:

    【解决方案3】:

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

    #include <curses.h>  
    

    【讨论】:

    • 添加了 但错误没有解决..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 2022-06-22
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    相关资源
    最近更新 更多