0

I'm currently learning how to use c++ and ncurses. Following a tutorial on Youtube I couldn't kepp up because of an error caused when I'm trying to compile my cpp file. The name of the file is Select.cpp so I'm using the command line gcc -o SELECT Select.cpp -lncurses to compile it but I keep getting the same error that is:

/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

I have realized that the problem is whenever I'm using string even tho I have included <string> at the beggining.

The code I'm working is:

#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;
}

Thanks for readying and if you can help me I would appreciate it.

payo
  • 4,501
  • 1
  • 24
  • 32
  • 3
    Use `g++` instead of `gcc` to compile and link C++ code. Don't rely on a "tutorial" on "Youtube" to learn C++. [Get a good book, instead](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Feb 13 '20 at 02:01
  • undefined reference to the string methods means you're not linking the libc++ with your build, which should happen FOR you, if you were using g++ instead of gcc -- as @SamVarshavchik advised – payo Feb 13 '20 at 02:06
  • When I use the g++ intead of gcc it works, the problem is that g++ won't compile ncurses and that would be another issue, I have read online and most people seem to solve the problem with #include but I can't understand why mines not working. Thanks for the advice on the book:) – Guillerrmo-O-C Feb 13 '20 at 02:25
  • see my answer, i don't know why you think g++ can't compile code that includes ncurses – payo Feb 13 '20 at 23:38

1 Answers1

1

The first step when asking for help in any forum, not just stackoverflow, is to reduce your problem to the minimal steps required to reproduce the problem. In your case, you have provided too much code and too little information regarding the steps you took. This would have been better:


(example question) I am trying to build a program with ncurses and c++ std::string. This is my code

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

I am using ubuntu 18.04, with gcc version 9.0.1 This is my build command

gcc main.cpp

Which fails (truncated errors for brevity)

/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

Your comments suggested I use g++ instead. But this fails as well

g++ main.cpp

/tmp/ccWWu0ZW.o: In function `main':
main.cpp:(.text+0x2d): undefined reference to `initscr'
...

At this point, we would be better prepared to help you. I would point out that you need to link ncurses libraries in your build step, as such:

g++ main.cpp -lcurses

And this would work. Please learn that when you say "it doesn't work", we have almost no idea what you're talking about. Give error messages, be specific, and show specifically what you've tried. Maybe you had this error:

main.cpp:1:10: fatal error: ncurses.h: No such file or directory
    1 | #include <ncurses.h>

In which case we'd tell you to install the ncurses development pkg for your distro (for me on ubuntu, that would be apt-get install libncurses5-dev). But without specifics we'll likely ignore your question, or make unhelpful guesses.

payo
  • 4,501
  • 1
  • 24
  • 32
  • Thanks for the example, I will take notice of the recommendations you all gave me. And thanks for showing me how to make a better post post my problems or doubs. – Guillerrmo-O-C Feb 14 '20 at 02:23