1

In my VSCode environment with working msys2 / mingW64 /gcc compilers i'm now trying to use ncurses / curses.

msys is in the default installpath, so gcc / g++ /gdb are in C:\msys64\mingw64\bin

As i understand it, the headerfiles being used by directives are the ones in C:\msys64\mingw64\include. So in general i think this is a question how to properly include and use any of the stuff in the subdirectories of C:\msys64\mingw64\include

For my case with ncurses, in include directory there are two subdirectories with names ncurses and ncursesw with identical content.

To begin, i try with a very simple `helloCurses.cpp file

#include <ncurses/curses.h>
using namespace std;

int main(int argc, char ** argv)
{
    // init screen and sets up screen
    initscr();

    // print to screen
    printw("Hello World");

    // refreshes the screen
    refresh();

    // pause the screen output
    getch();

    // deallocates memory and ends ncurses
    endwin();
    return 0;
}

obviously the compiler can find the ncurses/curses.h file. But it still doesn't compile.

I'm getting the error messages:

Executing task: C/C++: MingW g++.exe build active file 

Starting build...
C:/msys64/mingw64/bin/g++.exe -fdiagnostics-color=always -g D:\GitHub\Cpp-Code-priv\ncurses\helloCurses.cpp -o D:\GitHub\Cpp-Code-priv\ncurses\helloCurses.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Mathias\AppData\Local\Temp\cc44BFtA.o: in function `main':
D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:7: undefined reference to `__imp_initscr'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:10: undefined reference to `__imp_printw'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:13: undefined reference to `__imp_refresh'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:16: undefined reference to `__imp_stdscr'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:16: undefined reference to `__imp_wgetch'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:19: undefined reference to `__imp_endwin'
collect2.exe: error: ld returned 1 exit status

Build finished with error(s).

As far as I understand the linker will not find the ncurses library. This should be libncurses.a or libncurses.dll.

These files are in the directory C:\msys64\mingw64\lib.

In the end i like to compile with VSCode tasks.json, but to begin with, the commandline

gcc -I C:\msys64\mingw64\include\ncurses -L C:\msys64\mingw64\lib -lncurses helloCurses.cpp -o helloCurses.exe

didn't work either and still gives me the same errormessage.

I have been pointed to Why does the order in which libraries are linked sometimes cause errors in GCC?

I'm sure this will at some point become valuable information, but right now this isn't about the order of a multitude of probably codependent libraris. This is just about how to include a very first additional library not being basic enough to be includd by a simple #include <stdio.h>, but still basic enough to be in the full toolchain being installed bypacman -S --needed base-devel mingw-w64-x86_64-toolchain

maddes8cht
  • 569
  • 3
  • 16
  • 2
    `undefined reference` indicates missing `-l`. `-l` must be to the right of `.cpp`, otherwise it has no effect. I suggest not adding redundant `-I` and `-L`, yours should be unnecessary. – HolyBlackCat Jan 15 '23 at 15:47
  • Does this answer your question? [Why does the order in which libraries are linked sometimes cause errors in GCC?](https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc) – drescherjm Jan 15 '23 at 16:57
  • @HolyBlackCat I tried g++ helloCurses.cpp -l -o helloCurses.exe || g++ -l helloCurses.cpp -o helloCurses.exe || g++ helloCurses.cpp -o helloCurses.exe -l || and the same with -lncurses. it doesn't work for ma, and I'm not even aware of the meaning of -l I don't find it in g++ --help – maddes8cht Jan 15 '23 at 19:18
  • @drescherjm I' trying to read trough it and I'm sure at some point it may become a valuable information for me, but rigth now it isn'T. Right now my problem is not with multiple libraries. M problem is how to get the very first additional library included hat is not in the most basic include path, being included with a simple #include or so. And i find it astonishing hard to find an aswer for that. – maddes8cht Jan 15 '23 at 19:22
  • Is this supposed to be `c++` or `c`? If `c++` it should be `g++ helloCurses.cpp -lncurses -o helloCurses.exe` – drescherjm Jan 15 '23 at 19:31
  • ***MingW g++.exe build active file*** You did not edit your `tasks.json` and add the `"-lncurses,"` after "${file}," in the args: – drescherjm Jan 15 '23 at 19:34
  • it's supposed to be c++ – maddes8cht Jan 15 '23 at 19:35
  • i see i accidently wrote gcc in one of the comments which might be confusing. It should be g++, and with g++ it leads to the same errors as in the main text – maddes8cht Jan 15 '23 at 19:37
  • @drescherjm `g++ helloCurses.cpp -lncurses -o helloCurses.exe` still gives me the same errors – maddes8cht Jan 15 '23 at 19:46
  • i mean, it's a verry simple and short file, and it's just a standardinstallation of msys2 with mingw64. Does it work for you with the mentioned line? – maddes8cht Jan 15 '23 at 19:52
  • I just tested and had the same errors. Then I tested on WSL / ubuntu and I had similar errors for the same code and fixed them but I have not fixed msys2 yet. – drescherjm Jan 15 '23 at 22:20
  • This got me past the linker errors on msys2: [https://stackoverflow.com/questions/67656787/cannot-static-link-ncurses-with-mingw](https://stackoverflow.com/questions/67656787/cannot-static-link-ncurses-with-mingw) – drescherjm Jan 15 '23 at 22:32

0 Answers0