Problem z kompilacją programu używającego_getch() i _kbhit() z biblioteki "conio"

0

Próbuję stworzyć prostą gierkę Snake, w której wczytywanie znaków z klawiatury wygląda tak:

 void kierunek()
{
    if(kbhit())
    {
        switch(getchar())
        {
                case 'w':
                    zwrot = GORA;
                    break;
                case 's':
                    zwrot = DOL;
                    break;
                case 'a':
                    zwrot = LEWO;
                    break;
                case 'd':
                    zwrot = PRAWO;
                    break;
                case 'x':
                    zwrot = STOP;
                    break;
        }                       
    }
}

Przy próbie kompilacji otrzymuję błąd:

 cd 'C:\Users\norbs\Documents\NetBeansProjects\snejk'
C:\cygwin64\bin\make.exe -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/c/Users/norbs/Documents/NetBeansProjects/snejk'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/snejk.exe
make[2]: Entering directory '/cygdrive/c/Users/norbs/Documents/NetBeansProjects/snejk'
mkdir -p dist/Debug/Cygwin-Windows
g++     -o dist/Debug/Cygwin-Windows/snejk build/Debug/Cygwin-Windows/main.o -L/cygdrive/D/Downloads/curses -L/cygdrive/D/Downloads/conio
build/Debug/Cygwin-Windows/main.o: In function `kierunek()':
/cygdrive/c/Users/norbs/Documents/NetBeansProjects/snejk/main.cpp:67: undefined reference to `__imp_kbhit'
/cygdrive/c/Users/norbs/Documents/NetBeansProjects/snejk/main.cpp:67:(.text+0x21d): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `__imp_kbhit'
collect2: error: ld returned 1 exit status
nbproject/Makefile-Debug.mk:62: recipe for target 'dist/Debug/Cygwin-Windows/snejk.exe' failed
make[2]: *** [dist/Debug/Cygwin-Windows/snejk.exe] Error 1
make[2]: Leaving directory '/cygdrive/c/Users/norbs/Documents/NetBeansProjects/snejk'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/cygdrive/c/Users/norbs/Documents/NetBeansProjects/snejk'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 13s)

Co dokładnie oznacza:

 relocation truncated to fit: R_X86_64_PC32 against undefined symbol `__imp_kbhit'?

Wiem, że biblioteka "conio" pochodzi z języka C. Czy jestem w stanie ją jakoś zastąpić?

8

conio nie "pochodzi" z języka C, tylko jest pozostałością po MS-DOS. Próbujesz kompilować pod linuksem (cygwin go próbuje udawać) biblitekę używającą funkcjonalności MS-DOS ;​)

Jako alternatywy użyj curses.

0

getch() z "curses" też zwraca błąd:

 cd 'C:\Users\norbs\Documents\NetBeansProjects\snejk'
C:\cygwin64\bin\make.exe -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/c/Users/norbs/Documents/NetBeansProjects/snejk'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/snejk.exe
make[2]: Entering directory '/cygdrive/c/Users/norbs/Documents/NetBeansProjects/snejk'
mkdir -p build/Debug/Cygwin-Windows
rm -f "build/Debug/Cygwin-Windows/main.o.d"
g++    -c -g -I/cygdrive/D/Downloads/curses -I/cygdrive/D/Downloads/conio -std=c++14 -MMD -MP -MF "build/Debug/Cygwin-Windows/main.o.d" -o build/Debug/Cygwin-Windows/main.o main.cpp
mkdir -p dist/Debug/Cygwin-Windows
g++     -o dist/Debug/Cygwin-Windows/snejk build/Debug/Cygwin-Windows/main.o -L/cygdrive/D/Downloads/curses -L/cygdrive/D/Downloads/conio
build/Debug/Cygwin-Windows/main.o: In function `kierunek()':
/cygdrive/c/Users/norbs/Documents/NetBeansProjects/snejk/main.cpp:69: undefined reference to `wgetch'
/cygdrive/c/Users/norbs/Documents/NetBeansProjects/snejk/main.cpp:69:(.text+0x228): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `wgetch'
build/Debug/Cygwin-Windows/main.o:main.cpp:(.rdata$.refptr.stdscr[.refptr.stdscr]+0x0): undefined reference to `stdscr'
collect2: error: ld returned 1 exit status
nbproject/Makefile-Debug.mk:62: recipe for target 'dist/Debug/Cygwin-Windows/snejk.exe' failed
make[2]: *** [dist/Debug/Cygwin-Windows/snejk.exe] Error 1
make[2]: Leaving directory '/cygdrive/c/Users/norbs/Documents/NetBeansProjects/snejk'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/cygdrive/c/Users/norbs/Documents/NetBeansProjects/snejk'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 14s)

I jeszcze pytanie: w jaki sposób mogę zastąpić kbhit()?

0

getchar z stdio.h
gg.
Jezu.. conio.. LOL

0

@wujnia: getchar() wymaga wciśnięcia enter po wprowadzeniu znaku, więc odpada. I co jest nie tak z conio?
A tak z ciekawości, dlaczego w takim razie conio działa w Visual Studio normalnie, a w NetBeans już nie?

2

A tak z ciekawości, dlaczego w takim razie conio działa w Visual Studio normalnie, a w NetBeans już nie?

Bo w Visualu jest, a w NetBeans go nie ma.

co jest nie tak z conio?

No właśnie to.

0

Ale przecież nawet jeżeli dodam "conio.h" jak normalną bibliotekę w projekcie NetBeans, to i tak program się nie zbuduje.

0

Jeśli program ma działać pod windows to użyj (SHORT WINAPI GetAsyncKeyState(int key)&0x8000) z biblioteki <windows.h>

1 użytkowników online, w tym zalogowanych: 0, gości: 1