LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Several "undefined reference to" error when compiling a C programm (http://www.linuxquestions.org/questions/programming-9/several-undefined-reference-to-error-when-compiling-a-c-programm-186781/)

acer_peri 05-28-2004 12:27 AM

Several "undefined reference to" error when compiling a C programm
 
the program is from Linux.org
Code:

#include <ncurses.h>

int main()
{        int ch;

        initscr();                        /* Start curses mode                */
        raw();                                /* Line buffering disabled        */
        keypad(stdscr, TRUE);                /* We get F1, F2 etc..                */
        noecho();                        /* Don't echo() while we do getch */

            printw("Type any character to see it in bold\n");
        ch = getch();                        /* If raw() hadn't been called
                                        * we have to press enter before it
                                        * gets to the program                */
        if(ch == KEY_F(1))                /* Without keypad enabled this will */
                printw("F1 Key pressed");/*  not get to us either        */
                                        /* Without noecho() some ugly escape
                                        * charachters might have been printed
                                        * on screen                        */
        else
        {        printw("The pressed key is ");
                attron(A_BOLD);
                printw("%c", ch);
                attroff(A_BOLD);
        }
        refresh();                        /* Print it on to the real screen */
            getch();                        /* Wait for user input */
        endwin();                        /* End curses mode                  */

        return 0;
}

on compiling the program, the shell displays the following error:

[ryland@localhost compiler]$ gcc -o testncurses.o testncurses.c
/tmp/cceWHfdn.o(.text+0x11): In function `main':
: undefined reference to `initscr'
/tmp/cceWHfdn.o(.text+0x16): In function `main':
: undefined reference to `raw'
/tmp/cceWHfdn.o(.text+0x21): In function `main':
: undefined reference to `stdscr'
/tmp/cceWHfdn.o(.text+0x26): In function `main':
: undefined reference to `keypad'
/tmp/cceWHfdn.o(.text+0x2e): In function `main':
: undefined reference to `noecho'
/tmp/cceWHfdn.o(.text+0x3b): In function `main':
: undefined reference to `printw'
/tmp/cceWHfdn.o(.text+0x47): In function `main':
: undefined reference to `stdscr'
/tmp/cceWHfdn.o(.text+0x4c): In function `main':
: undefined reference to `wgetch'
/tmp/cceWHfdn.o(.text+0x68): In function `main':
: undefined reference to `printw'
/tmp/cceWHfdn.o(.text+0x7a): In function `main':
: undefined reference to `printw'
/tmp/cceWHfdn.o(.text+0x8d): In function `main':
: undefined reference to `stdscr'
/tmp/cceWHfdn.o(.text+0x92): In function `main':
: undefined reference to `wattr_on'
/tmp/cceWHfdn.o(.text+0xa5): In function `main':
: undefined reference to `printw'
/tmp/cceWHfdn.o(.text+0xb8): In function `main':
: undefined reference to `stdscr'
/tmp/cceWHfdn.o(.text+0xbd): In function `main':
: undefined reference to `wattr_off'
/tmp/cceWHfdn.o(.text+0xc9): In function `main':
: undefined reference to `stdscr'
/tmp/cceWHfdn.o(.text+0xce): In function `main':
: undefined reference to `wrefresh'
/tmp/cceWHfdn.o(.text+0xda): In function `main':
: undefined reference to `stdscr'
/tmp/cceWHfdn.o(.text+0xdf): In function `main':
: undefined reference to `wgetch'
/tmp/cceWHfdn.o(.text+0xe7): In function `main':
: undefined reference to `endwin'
collect2: ld returned 1 exit status
[ryland@localhost compiler]$

but I indeed include the ncurses library. How to solve this problem?thanks.

herbc 05-28-2004 12:46 AM

Check your include PATH, or

#include </usr/lib/ncurses.h>

for Slackware anyway. Enter your distro in your profile. Some distros may place the header files elsewhere.

Dark_Helmet 05-28-2004 12:55 AM

You'll also need to tell the compiler to link in the ncurses library. Your compilation command should be something like:

$ gcc -o testncurses.o -lncurses testncurses.c

acer_peri 05-28-2004 12:57 AM

my distro is Fedora core 2
herbc,
when I do as you mentioned, the following errors come out:
testncurses.c:2:30: /usr/lib/ncurses.h: no such file or directory
testncurses.c: In function `main':
testncurses.c:9: error: `stdscr' undeclared (first use in this function)
testncurses.c:9: error: (Each undeclared identifier is reported only once
testncurses.c:9: error: for each function it appears in.)
testncurses.c:9: error: `TRUE' undeclared (first use in this function)
testncurses.c:23: error: `A_BOLD' undeclared (first use in this function)
[ryland@localhost compiler]$

acer_peri 05-28-2004 01:00 AM

Quote:

Originally posted by Dark_Helmet
You'll also need to tell the compiler to link in the ncurses library. Your compilation command should be something like:

$ gcc -o testncurses.o -lncurses testncurses.c


thanks, Dark_Helmet! It works!no problem!

but some library like <stdlib.h> needn't I list in the compiling line.
can you tell me which libraries must specify when compiling?
Why must do the extra work to specify the library in gcc, cosidering that I'm already include it in the source file?

Dark_Helmet 05-28-2004 01:07 AM

Header like stdio.h, stdlib.h, string.h, and others are collectively included in glibc (the main bulk of standard C funtions). glibc is automatically linked since pretty much every program will access *some* of it's functions (printf, scanf, etc.).

When starting off, the rule of thumb is, if you're using anything "special" (e.g. you had to go look for documentation that you wouldn't find in a "programming C" reference), then you'll probably have to tell the compiler to link in that library explicitly.

All rules have exceptions, and the one for this rule is the math library. That one has bitten me a few times. You have to do "-lm" to access functions like sqrt().

Also, the "undefined reference" is your big clue from the linker. That error means it saw the function was defined (in the header), but couldn't locate the actual code to execute it. That is almost always because a library wasn't linked in (or you haven't written the function yet :) )

skelly 06-25-2004 02:20 PM

Quote:

Originally posted by Dark_Helmet
$ gcc -o testncurses.o -lncurses testncurses.c
Very helpful, thankyou! I was surprised to find that curses was not included in glibc so didn't realize that it would have to be specifically linked.

frazneo 04-27-2008 07:36 PM

Thank you, Dark_Helmet. It has helped me very much.

buzzhost 12-11-2008 06:45 AM

Thank you!

I've spent about a week trying to figure out why I could not get anything useful out of curses/ncurses. I've been trying to compile the perl module but it refuses to play nice. In the end I have resorted to trying to test it with c (something I promises myself I would try to learn) and with the help of this post solved my issue.

I just wanted to say 'thanks'. Without boards like this finding quality information would be so hit & miss. Thanks again.


All times are GMT -5. The time now is 09:03 PM.