undefined reference to 'getch' error 해결법좀 부탁드립니다 ㅜㅜ

kby2194의 이미지

우분투로 리눅스 공부하고 있는 초짜 입니다.
대학 프로젝트 때문에 make를 이용해 컴파일을 하는데 undefined reference to 에러때매
정말 미치겠네요; 구글링을 해도 잘 모르겠고..직접 물어보려고요

코드내용:

1.전체파일(ls)

gotoxy_solution.h
screen_clear.h
kbhit.h
getch.h
solution_snake_game.h
main.c

*main.c를 제외한 모든 헤더엔 #ifndef #define #endif 가 포함되어있습니다(자기자신)

2.음.. 헤더에 포함된내용?

(1)solution_snake_game.h안에 gotoxy_solution.h , kbhit.h , getch.h 3개의 헤더가 include

(2)main.c 안에 solution_snake_game.h , gotoxy_solution.h , screen_clear.h , getch.h 4개의 헤더가 include

(*나머지 헤더에는 포함된 사용자헤더가 없습니다)

3.Makefile의 코드내용

test : main.o

gcc -o test main.o

main.o : solution_snake_game.h gotoxy_solution.h screen_clear.h getch.h main.c

gcc -c main.c

clean :

rm -f test

(*줄 공백은 보기 편하게 하기위해 한것입니다.)

이렇게 되있는데 뭐가 문제일까요
해결법좀 부탁드립니다ㅜㅠ

jick의 이미지

gcc 명령 뒤에 -Wall을 붙이시고 make를 다시 해보신 다음 화면에 뜨는 경고를 다 잡아주시면 아마 해결되지 싶습니다.

..라고 썼다가 다시 보니까, kbhit.h ???

도대체 어디서 무슨 파일을 가져오신 건가요? -_- 도스에서 돌아가는 헤더 파일이라고 리눅스로 들고 오면 십중팔구 컴파일 안됩니다.
리눅스에는 getch와 비슷한 함수를 기본적으로 지원하지 않는 것으로 알고 있습니다. (구글에 linux getch라고 쳐보니까 이것저것 많이 나오기는 하는군요.)

kby2194의 이미지

gcc 명령뒤에 -Wall을 붙인다는게 정확히 어떻게 하는건지 말씀해주실수있나요?
그리구 kbhit같은 경우는 키보드 입력을 받은걸 확인해주는 기능으로 알구 있구요 linux kbhit로 구글 검색해서 찾아 가져온 파일입니다.

http://cboard.cprogramming.com/c-programming/63166-kbhit-linux.html

위 주소에서 퍼온 거구요

getch같은 경우는 kbhit으로 키보드 입력 받은걸 확인하면 입력받은 내용이 뭔지를 확인하는 기능이구 이 역시 linux getch 등으로 검색해서 찾아낸 겁니다.
visual studio로 먼저 만들구 옮기는 형태라 이것저것 찾아서 오류를 해결했거덩요 ㅠ

qiiiiiiiip의 이미지

getch.c, kbhit.c 가 있어야할 것 같은데요,

일단 코드를 보여주셔야할 듯요..

최소한 getch.* kbhit.* 파일정도는 있어야 답변을 드릴 수 있겠네요.

kby2194의 이미지

왼쪽사진이 getch.h헤더 구요 오른쪽이 kbhit.h 헤더입니다.

---------------------------------------
지금막 getch.h 헤더의 #ifndef #define #endif 없앴더니 실행이되네요;;
이게 무슨 관련이 있는건가요 컴파일에?

댓글 첨부 파일: 
첨부파일 크기
Image icon 1.png84.86 KB
ymir의 이미지

undefined reference to 'XXX' 에러는 컴파일된 오브젝트를 링크할 때, XXX 라는 함수의 definition 을 찾을 수 없는 경우에 발생합니다.
소스 코드 어딘가에서 XXX 라는 함수를 가져다 썼는데, 실제로 그 XXX 라는 함수가 구현되어 있는 오브젝트 파일 또는 라이브러리가 없는 경우에 발생하죠.

man XXX 로 해당 함수에 대한 man page 참조해서 해당 라이브러리를 링크해 주시거나..
아니면 내 소스에 XXX 라는 함수가 구현되어 있는지, 오브젝트 링크할 때 누락되어 있는지 살펴서 보시고 넣어 주시면 됩니다.

질문으로 돌아가서 보면, getch 는 linux 표준 함수가 아닙니다.
근데 이 함수를 그냥 가져다 썼으니, undefined reference to 'getch' .. 링커 에러가 뜨는거죠.
linux 에는 getch 와 같은 기능을 하는 함수가 없으니, 직접 구현하셔야 합니다. (linux getch 로 검색)

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

kby2194의 이미지

위에 첨부해논 사진에서요 kgetch.h 헤더의 #ifndef #define #endif를 제거했더니 실행이 되는군요
#ifndef #define #endif를 추가하는게 어떤 문제를 발생시키는지 혹시 아시나요?

ymir의 이미지

왼쪽 헤더 파일(getch.h 로 추정) 를 보시면

#ifdef GETCH_H
#define GETCH_H
 
...
 
#endif

로 되어 있죠.. 처음 두 줄을 보시고 뭐가 이상한지 다시 한 번 생각해 보세요.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

kby2194의 이미지

ifndef 를 ifdef로 썼군요...
하 이것때매 6시간을 날렸네요..
지적 감사합니다 ㅠㅠ

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.