Using Conio.h and Curses.h error

Hello, if I try to compile a cpp file using both conio.h and curses.h in it. If I try to run it with conio.h before curses.h I get an error in the main.cpp.
main.cpp|13|undefined reference to `_imp__stdscr'|

If I put curses first then the conio.h opens in codeblocks and I get an error there.

conio.h|38|error: macro "getch" passed 1 arguments, but takes just 0|
Best advice is to drop them all: no getch, no curses, no conio. Write in standard C++.

If you really need to compile the same source against two different C libraries, then you'll have to use conditional compilation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifdef I_AM_A_TIME_TRAVELER_FROM_1990
#include <conio.h>
#elif I_GOT_LINUX_OR_MAC_BUT_LIKE_TO_PRETEND_ITS_MSDOS
#include <curses.h>
#else
#error Pick one
#endif
#include <iostream>
int main()
{
#ifdef I_GOT_LINUX_OR_MAC_BUT_LIKE_TO_PRETEND_ITS_MSDOS
   initscr();
#endif
   int ch = getch();
#ifdef I_GOT_LINUX_OR_MAC_BUT_LIKE_TO_PRETEND_ITS_MSDOS
   endwin();
#endif
   std::cout << "got " << ch << '\n';
}

to compile on Linux:
g++ -o test test.cc -DI_GOT_LINUX_OR_MAC_BUT_LIKE_TO_PRETEND_ITS_MSDOS -lcurses -ltinfo

to compile on Windows:
Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions -> Edit... -> I_AM_A_TIME_TRAVELER_FROM_1990
Ctrl-F5



to compile on *Visual Studio ;)
I just really want to use curses.h that's the thing. Then after start learning actual graphics. How could I get getch(); to work with curses? Btw I am trying to compile in Codeblocks with the default compiler but I wouldn't mind using Visual Studio but I don't know how to use C++ with it
Last edited on
How could I get getch(); to work with curses?

as above:
1. Compile on Linux or Mac (or another Unix). Curses is a Unix library.
2. Call initscr() before calling getch()
3. link against libcurses and libtinfo.

It is really not about making getch available though. If you need a non-echoing input, Unix systems have more direct solutions.
Last edited on
Registered users can post here. Sign in or register to post.