LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-27-2004, 11:27 PM   #1
acer_peri
Member
 
Registered: Nov 2003
Location: Beijing, China
Distribution: Tettnang
Posts: 38

Rep: Reputation: 15
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.
 
Old 05-27-2004, 11:46 PM   #2
herbc
Member
 
Registered: May 2004
Location: United States
Distribution: Slackware 10.0
Posts: 72

Rep: Reputation: 15
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.
 
Old 05-27-2004, 11:55 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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
 
Old 05-27-2004, 11:57 PM   #4
acer_peri
Member
 
Registered: Nov 2003
Location: Beijing, China
Distribution: Tettnang
Posts: 38

Original Poster
Rep: Reputation: 15
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]$
 
Old 05-28-2004, 12:00 AM   #5
acer_peri
Member
 
Registered: Nov 2003
Location: Beijing, China
Distribution: Tettnang
Posts: 38

Original Poster
Rep: Reputation: 15
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?

Last edited by acer_peri; 05-28-2004 at 12:03 AM.
 
Old 05-28-2004, 12:07 AM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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 )
 
Old 06-25-2004, 01:20 PM   #7
skelly
LQ Newbie
 
Registered: Nov 2003
Location: Silicon Valley
Posts: 10

Rep: Reputation: 0
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.
 
Old 04-27-2008, 06:36 PM   #8
frazneo
LQ Newbie
 
Registered: Apr 2008
Location: Penza, Russian Federation
Distribution: Fedora 8
Posts: 1

Rep: Reputation: 0
Thank you, Dark_Helmet. It has helped me very much.
 
Old 12-11-2008, 05:45 AM   #9
buzzhost
LQ Newbie
 
Registered: Dec 2008
Posts: 1

Rep: Reputation: 0
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"undefined reference"linker error for static field w/ C++ astorm Programming 5 08-27-2008 03:00 AM
Undefined Screen " XFree86" reference by serverLayout "XFree86 Configured" comox *BSD 7 01-17-2005 05:47 PM
Kernel make bzImage error "undefined reference" w/ Prism2.5 USB driver under 2.6 joachimvb Mandriva 1 09-24-2004 07:43 PM
G++ Linking Error "undefined reference" djjumper9 Programming 2 04-13-2004 09:36 AM
ncurses compile error: "undefined reference" Mr. Eek Linux From Scratch 1 05-12-2003 04:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration