Omg I might be really dumb but I can't wrap my head around how am I supposed to properly link <curses.h> to my file. I've searched a lot but I don't seem to find information that helps. Here's my code, a really basic one:
#include <iostream>
#include <curses.h>
#include <panel.h>
using namespace std;
int main()
{
initscr();
WINDOW *p = newwin(0, 0, LINES / 2, COLS / 2);
PANEL *p_panel = new_panel(p);
waddstr(p, "that's what i'm talking about!");
update_panels();
doupdate();
endwin();
return 0;
}
I installed ncurses library with brew through terminal and i have an updated version of clang. Im trying to run an app in VSCode terminal. Here's the error i'm getting:
Undefined symbols for architecture x86_64:
"_COLS", referenced from:
_main in lab8-80f309.o
"_LINES", referenced from:
_main in lab8-80f309.o
"_doupdate", referenced from:
_main in lab8-80f309.o
"_endwin", referenced from:
_main in lab8-80f309.o
"_initscr", referenced from:
_main in lab8-80f309.o
"_new_panel", referenced from:
_main in lab8-80f309.o
"_newwin", referenced from:
_main in lab8-80f309.o
"_update_panels", referenced from:
_main in lab8-80f309.o
"_waddnstr", referenced from:
_main in lab8-80f309.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I tried to link it manually using gcc, I used this command:
clang++ lab8.cpp -lpanel -lncurses
and same with g++.
It executed succesfully, file "a.out" was created but did no difference
I've also seen somewhere that I might need to link this library in tasks.json but I haven't found any examples and I have no idea. Any help could be great
It's always a good idea to consult the documentation. Looking at the Section 2.1 shows that -lncurses
is required for a very basic ncurses program, but there was nothing about panels there. Scrolling down the TOC to find panels leads to section 16.2 where it clearly shows that -lpanel -lncurses
is required.
gcc and g++ are just aliases to LLVM on a Mac by default. clang++ lab8.cpp -lpanel -lncurses
should do the trick just fine.
With a Mac and a brew installed ncurses, it again helps to read the documentation. In this case, you just need brew info ncurses
. It clearly states:
For compilers to find ncurses you may need to set:
set -gx LDFLAGS "-L/usr/local/opt/ncurses/lib"
set -gx CPPFLAGS "-I/usr/local/opt/ncurses/include"
Running those commands should allow you to compile your brew ncurses. Conversely, the quoted strings can be added to your compile command.
clang++ lab8.cpp "-I/usr/local/opt/ncurses/include" "-L/usr/local/opt/ncurses/lib" -lpanel -lncurses
The above command was tested using the basic panels code from the documentation link above, and it works on my MacBook Pro using ncurses installed from brew.
This is also where I recommend a build system to automate this for you. Depending on how trivial your program is, a homespun makefile might be sufficient, but this seems like a great time to learn cmake.
It's worth pointing out that your code will do nothing. It makes the panel, then the program immediately ends. Whatever you drew will not simply stay on the screen.
Again, referencing the documentation shows that the line getch();
before endwin();
will 'freeze' the program so that you can see your text. Pressing any key should then end your program.
I should note that I made one other change to your example code, which may or may not apply to your overall project. If you are including panel.h
, you don't need to include ncurses.h
, and you never needed iostream
in the example. So I only had #include <panel.h>
.