LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-25-2010, 03:12 PM   #1
moonmoth
Member
 
Registered: Nov 2004
Location: Tallahassee, FL
Distribution: Fedora Core 15
Posts: 95

Rep: Reputation: 17
gcc does not show undefined reference error


When the following little C program is compiled with gcc it does not get a compile error even though the function atof is undefined. If I change it to xatof then it goes get an "undefined reference" error. (I know that atof is a library function, and I know that it resides in stdlib.h. But since I did not include stdlib.h I would think that it would give me an error). What am I missing here???

main()
{
double f;
char s[] = "1.0";
f = atof(s);
}
 
Old 07-25-2010, 03:19 PM   #2
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
The compiler doesn't know about libraries linked later, so the only thing it could issue is a warning. Implicit declaration is allowed in C. But, try compiling with "-Wall", i'm quite sure you will get your implicit declaration warning
 
Old 07-25-2010, 03:49 PM   #3
selfprogrammed
Member
 
Registered: Jan 2010
Location: Minnesota, USA
Distribution: Slackware 13.37, 14.2, 15.0
Posts: 635

Rep: Reputation: 154Reputation: 154
I have learned to never use a name from the common C libraries.
There are too many paths that the compiler can get to them.
Some of the headers call each other so deep, that I cannot track down
which names might be safe to use.

I suspect the GNU compiler knows about the name even it is not defined or declared. It has many non-standard enhancements. Try to find it in the "info GCC", it might be mentioned.
Test this again with some other stdlib names. I suspect it will work with some and not others. If the compiler is getting to the stdlib header by some sneak path then all the names will behave the same.

Last edited by selfprogrammed; 07-25-2010 at 03:56 PM.
 
Old 07-25-2010, 03:52 PM   #4
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
well, right, but the OP just misses an error message. the explanation for this is that the linker obviously finds a symbol "atof" somewhere in the standard libs that are linked by default, so it doesn't complain. And, as strange as this may sound, "implicit declaration" (by JUST using a function without declaring it first) is allowed in ISO C.

But, compilers can (and should) issue warnings about implicit declarations because they are a common source of errors ....
 
Old 07-25-2010, 04:14 PM   #5
moonmoth
Member
 
Registered: Nov 2004
Location: Tallahassee, FL
Distribution: Fedora Core 15
Posts: 95

Original Poster
Rep: Reputation: 17
Using gcc -Wall does give me an error message, and I thank Zirias for that info. I will use that from now on. I might add, parenthetically, that whatever atof it did come up with does not work. If it had worked, I would have never even realized the error. (The error being not to use #include <stdlib.h>)
 
Old 07-25-2010, 04:26 PM   #6
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
That's good practice. I use "-std=c89 -pedantic-errors -Wall -Werror" on any ISO C code i plan to release to the public Sometimes this complains about things I actually wanted that way, but there's always a way around
 
Old 07-25-2010, 04:29 PM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by moonmoth View Post
I might add, parenthetically, that whatever atof it did come up with does not work.
Implicit declaration makes the compiler assume the function returns an int. So it called the correct atof (which actually returns a double) then it converted the returned value from int to double. When the returned value wasn't actually an int, converting it from int to double gives you garbage.
 
1 members found this post helpful.
Old 07-25-2010, 04:32 PM   #8
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
Hey, thanks for this interesting fact! As I generally avoid implicit declarations, I wasn't aware of that.
 
Old 07-25-2010, 04:33 PM   #9
moonmoth
Member
 
Registered: Nov 2004
Location: Tallahassee, FL
Distribution: Fedora Core 15
Posts: 95

Original Poster
Rep: Reputation: 17
Yeah, I thought it might be something like that. Live and learn. (Die and forget it all).
 
  


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 to main error in winavr gcc plugin in avr studio zack670303 Programming 1 02-26-2009 12:52 PM
gcc make fails with undefined reference to `gcc_gettext_width' fsa Linux - Software 4 04-30-2007 06:01 AM
gcc linking: undefined reference to just about everything shabbychef Programming 7 09-09-2005 08:42 PM
gcc ld error. Undefined reference redness Linux - Software 4 02-08-2005 02:01 AM
Undefined reference error using gcc with Redhat Linux 9.0 armesk Programming 2 08-28-2003 06:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:21 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