c++ - 无法同时编译ncurses和字符串

标签 c++ string compiler-errors ncurses

我目前正在学习如何使用c++和ncurses。遵循有关YouTube的教程之后,由于尝试编译cpp文件时出现错误,我无法继续工作。
该文件的名称为Select.cpp,所以我使用命令行gcc -o SELECT Select.cpp -lncurses对其进行编译,但我不断收到相同的错误消息:

/tmp/ccirp2Pk.o: En la función `main':
Select.cpp:(.text+0x10e): referencia a `std::allocator<char>::allocator()' sin definir
Select.cpp:(.text+0x127): referencia a `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)' sin definir
Select.cpp:(.text+0x136): referencia a `std::allocator<char>::~allocator()' sin definir
Select.cpp:(.text+0x14d): referencia a `std::allocator<char>::allocator()' sin definir
Select.cpp:(.text+0x166): referencia a `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)' sin definir
Select.cpp:(.text+0x175): referencia a `std::allocator<char>::~allocator()' sin definir
Select.cpp:(.text+0x18c): referencia a `std::allocator<char>::allocator()' sin definir
Select.cpp:(.text+0x1a5): referencia a `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)' sin definir
Select.cpp:(.text+0x1b4): referencia a `std::allocator<char>::~allocator()' sin definir
Select.cpp:(.text+0x217): referencia a `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const' sin definir
Select.cpp:(.text+0x2cb): referencia a `std::allocator<char>::~allocator()' sin definir
Select.cpp:(.text+0x2df): referencia a `std::allocator<char>::~allocator()' sin definir
Select.cpp:(.text+0x2f3): referencia a `std::allocator<char>::~allocator()' sin definir
Select.cpp:(.text+0x319): referencia a `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()' sin definir
Select.cpp:(.text+0x34c): referencia a `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()' sin definir
/tmp/ccirp2Pk.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): referencia a `__gxx_personality_v0' sin definir
collect2: error: ld returned 1 exit status

我已经意识到问题出在我使用字符串时,甚至我在开始时就加入了<string>

我正在工作的代码是:

#include <ncurses.h>
using namespace std;
#include <string>

int main(int argc, char ** argv){
    //Ncurses Starts
    initscr();
    noecho();
    cbreak();

    //get screen size
    int yMax, xMax;
    getmaxyx(stdscr,yMax, xMax);

    //create a window for our menu
    WINDOW * menuwin = newwin(6, xMax-12, yMax-8, 5);
    box(menuwin, 0, 0);
    refresh();
    wrefresh(menuwin);

    //makes it so we can use arrow keys
    keypad(menuwin, TRUE);
    string choices[3] = {"walk", "jog", "run"};
    int choice;
    int highlight =0;

    while(1){
      for(int i=0;i<3;i++){
        if(i == highlight){
            wattron(menuwin, A_REVERSE);
            mvwprintw(menuwin, i+1, 1, choices[i].c_str());
            wattroff(menuwin, A_REVERSE);
        }
        choice = wgetch(menuwin);

        switch(choice){
            case KEY_UP:
                highlight--;
                break;
            case KEY_DOWN:
                highlight++;
                break;
            default:
                break;
        }
        if(choice=10){
            break;
        }
      }
    }


    //make sure program waits before exiting
    getch();
    endwin();
    //Ncurses ends

    return 0;
}

感谢您的准备,如果您能帮助我,我将不胜感激。

最佳答案

在任何论坛上寻求帮助的第一步,而不仅仅是stackoverflow,都是将您的问题减少到再现问题所需的最少步骤。就您而言,您提供的代码太多,而有关步骤的信息也很少。这样会更好:

(示例问题)
我正在尝试使用ncurses和c++ std::string构建程序。这是我的代码

    #include <ncurses.h>
    #include <string>
    int main()
    {
      std::string s;
      initscr();
      endwin();
      return 0;
    }

我正在使用gcc版本9.0.1的ubuntu 18.04
这是我的构建命令
gcc main.cpp
失败(为简洁起见,将其截短)
/tmp/ccJyXNFX.o: In function `main':
main.cpp:(.text+0x2d): undefined reference to `initscr'
...
main.cpp:(.text+0x10e): undefined reference to `std::allocator<char>::allocator()'
main.cpp:(.text+0x127): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
...
undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status

您的评论建议我改用g++。但这也失败了
g++ main.cpp
/tmp/ccWWu0ZW.o: In function `main':
main.cpp:(.text+0x2d): undefined reference to `initscr'
...

在这一点上,我们将更好地为您提供帮助。我要指出的是,您需要在构建步骤中链接ncurses库,如下所示:
g++ main.cpp -lcurses
这会起作用。请了解,当您说“它不起作用”时,我们几乎不知道您在说什么。提供错误消息,具体说明并具体显示您尝试过的内容。也许你有这个错误:
main.cpp:1:10: fatal error: ncurses.h: No such file or directory
    1 | #include <ncurses.h>

在这种情况下,我们会告诉您为发行版安装ncurses开发pkg(对我而言,在ubuntu上,这将是apt-get install libncurses5-dev)。但是如果没有具体信息,我们可能会忽略您的问题,或者做出无益的猜测。

关于c++ - 无法同时编译ncurses和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60199409/

相关文章:

c++ - 虚拟构造函数

python - 名称错误 : name 'book' is not defined

c++ - 将 `hana::string` 转换为 `constexpr const char (&)[]`

java - 如何以编程方式运行 Java 编译器时添加依赖项

c++ - 实现类类型 vector 的元素

c++ - 内部编译器错误消息是什么意思,我该怎么办?

c++ - “make_error_code”未在此范围内声明,并且在实例化时通过参数相关查找未找到任何声明

xcode - Xcode无法识别C++语法

c++ - 反向字符串没有循环

python - 将列表中的每个字符串拆分为数字和非数字部分