Undefined reference when using ncurses on linux

谁说我不能喝 提交于 2019-11-30 07:20:26

Have you used the -lcurses option when linking?

Including the header files let the code compile (because the compiler knows what the function call looks like from the .h file), but the linker needs the library file to find the actual code to link into your program.

Brian Campbell

As Greg Hewgill said, you need to pass in -lcurses or -lncurses to link to the curses library.

gcc -o hello hello.c -lncurses

You also probably mean to use initscr() and getch(). Once I make those substitutions, the above compiles for me.

For anyone having similar problems: -lx arguments, where x is your library, should always follow the source and object files.

I was having a similar issue and found a solution which helped me, but was slightly different from the other answers posted here. I was trying to use the panels library with curses and my compile command was:

$ gcc -o hello hello.c -lncurses -lpanel

when I read the other answers, I was baffled because I was including the -lncurses flag, but it still was not compiling, and with similar errors to what you were getting:

$ gcc -o hello hello.c -lncurses -lpanel
/usr/lib/gcc/i686-linux-gnu/4.7/../../../../lib/libpanel.a(p_new.o): In function `new_panel':
p_new.c:(.text+0x18): undefined reference to `_nc_panelhook'

I finally found my answer in the tldp:

"To use panels library functions, you have to include panel.h and to link the program with panels library the flag -lpanel should be added along with -lncurses in that order."

So, it appears that order is important when using the compile flags! I tried switching the order:

gcc -o hello hello.c -lpanel -lncurses

This allowed it to compile successfully. I know you already have your answer, so I hope this helps someone.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!