Undefined reference when using ncurses on linux

前端 未结 4 562
温柔的废话
温柔的废话 2020-12-30 08:57

I\'m trying to start developing a program using ncurses on Linux. I can\'t even get the Hello World example to compile. Here\'s the code:

#include 

        
4条回答
  •  伪装坚强ぢ
    2020-12-30 09:32

    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.

提交回复
热议问题