Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > No compile error for undefined function

Reply
Thread Tools

No compile error for undefined function

 
 
sam.s.kong@gmail.com
Guest
Posts: n/a
 
      12-22-2005
Hello!

I have a very basic question.

a.c:
<code>

#include <stdio.h>

main()
{
printf("%d\n", foo(3));
}

</code>

I compiled it with
gcc -c -o a.o a.c

I expected some compile error but it passed.
If I do like 'gcc a.c', it gives an error.
undefined reference to 'foo'.

Can it be compiled but not linked?

TIA.

Sam

 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      12-22-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> ...
> I compiled it with
> gcc -c -o a.o a.c
> ...
> Can it be compiled but not linked?
> ...


Yes. That's exactly what '-c' option does for GCC.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      12-22-2005
(E-Mail Removed) wrote:

> Hello!
>
> I have a very basic question.
>
> a.c:
> <code>
>
> #include <stdio.h>
>
> main()
> {
> printf("%d\n", foo(3));
> }
>
> </code>
>
> I compiled it with
> gcc -c -o a.o a.c
>
> I expected some compile error but it passed.
> If I do like 'gcc a.c', it gives an error.
> undefined reference to 'foo'.
>
> Can it be compiled but not linked?


Right. It assumes a declaration for foo(), as you didn't provide one.
Naturally there isn't one defined anywhere, so the link fails.



Brian

 
Reply With Quote
 
sam.s.kong@gmail.com
Guest
Posts: n/a
 
      12-22-2005
Thanks for the reply.

> Right. It assumes a declaration for foo(), as you didn't provide one.
> Naturally there isn't one defined anywhere, so the link fails.


I have an additional question.
If declaration is not needed for compilation, what are header files
for?
I thought that header files provide declarations of external functions
and data types.
Could you teach me about that?

Thanks.

Sam

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-22-2005
(E-Mail Removed) wrote:
>
> Thanks for the reply.
>
> > Right. It assumes a declaration for foo(), as you didn't provide one.
> > Naturally there isn't one defined anywhere, so the link fails.

>
> I have an additional question.
> If declaration is not needed for compilation, what are header files
> for?
> I thought that header files provide declarations of external functions
> and data types.
> Could you teach me about that?


In C99 a declaration *is* required.
In C89, if no declaration is provided,
then the complier acts as if the function has been declared
as returning type int.
Something that typically happens with C89 compilers
when malloc is used without #include <stdlib.h>,
is that a warning will appear,
telling the programmer to cast the return value of malloc,
because without the proper declaration in scope,
the compiler thinks that malloc returns type int,
instead of type pointer to void.

--
pete
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      12-22-2005
(E-Mail Removed) wrote:
>
>>Right. It assumes a declaration for foo(), as you didn't provide one.
>>Naturally there isn't one defined anywhere, so the link fails.

>
> I have an additional question.
> If declaration is not needed for compilation, what are header files
> for?
> I thought that header files provide declarations of external functions
> and data types.
> Could you teach me about that?


For one thing, the compiler spontaneously makes up the function
signature and assumes that the function returns int -- and the
compiler may not guess right.

If you provide a prototype(*) then the compiler knows the parameter
types and the return type and can issue diagnostic messages if
something is wrong and it can convert the arguments in a function
call correctly to the parameter types.

Have you read the FAQ? It may have to say something which further
enlightens you.

Example, based on your own:
$ cat fooT.c
#include <stdio.h>

main()
{
printf("%d\n", foo(3));
}
$ cat foo.c
double foo (float bar)
{
return 5000.0/(1.0 + (double)bar*bar);
}
$ gcc -std=c89 -pedantic -c -o fooT.o fooT.c
$ gcc -std=c89 -pedantic -c -o foo.o foo.c
$ gcc -o foo fooT.o foo.o
$ ./foo
0

Somewhat unexpected, eh?
FWIW:
$ gcc -std=c89 -pedantic -Wall -O -c -o fooT.o fooT.c
fooT.c:4: warning: return type defaults to `int'
fooT.c: In function `main':
fooT.c:5: warning: implicit declaration of function `foo'
fooT.c:6: warning: control reaches end of non-void function



Cheers
Michael
____
(*) Note: The ... part of a variable argument list function behaves
a little bit different (in fact, it behaves quite like a function
call without prototype would behave).
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-22-2005
Michael Mair <(E-Mail Removed)> wrote:

> For one thing, the compiler spontaneously makes up the function
> signature and assumes that the function returns int -- and the
> compiler may not guess right.


In C89. (We know that, but OP might not.)

> $ gcc -std=c89 -pedantic -Wall -O -c -o fooT.o fooT.c


<ot>
My gcc man page says -std=c89 is identical to -ansi - is there a
particular reason you chose the former?
</ot>

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Targeur fou
Guest
Posts: n/a
 
      12-22-2005

Christopher Benson-Manica wrote:
> Michael Mair <(E-Mail Removed)> wrote:


Hello,

> > For one thing, the compiler spontaneously makes up the function
> > signature and assumes that the function returns int -- and the
> > compiler may not guess right.

>
> In C89. (We know that, but OP might not.)
>
> > $ gcc -std=c89 -pedantic -Wall -O -c -o fooT.o fooT.c

>
> <ot>
> My gcc man page says -std=c89 is identical to -ansi - is there a
> particular reason you chose the former?
> </ot>


<ot reply>

Yes 'c89' is one letter shorter than 'ansi' and perhaps much
clearer regarding the intent of making a compilation that complies with
an international standard.

</ot>

A+
Regis

 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      12-22-2005
On 2005-12-22 15:21:21 +0100, Christopher Benson-Manica
<(E-Mail Removed)> said:

> Michael Mair <(E-Mail Removed)> wrote:
>
>> For one thing, the compiler spontaneously makes up the function
>> signature and assumes that the function returns int -- and the
>> compiler may not guess right.

>
> In C89. (We know that, but OP might not.)
>
>> $ gcc -std=c89 -pedantic -Wall -O -c -o fooT.o fooT.c

>
> <ot>
> My gcc man page says -std=c89 is identical to -ansi - is there a
> particular reason you chose the former?
> </ot>


<also ot>
I'd imagine that it is likely that in some future version of gcc,
"-ansi" may equate to "-std=c99", instead of "-std=c89" (i.e. when C99
becomes the "standard" standard version of C).
</also ot>

--
Clark S. Cox, III
(E-Mail Removed)

 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      01-03-2006
Christopher Benson-Manica wrote:
> Michael Mair <(E-Mail Removed)> wrote:
>
>>For one thing, the compiler spontaneously makes up the function
>>signature and assumes that the function returns int -- and the
>>compiler may not guess right.

>
> In C89. (We know that, but OP might not.)
>
>>$ gcc -std=c89 -pedantic -Wall -O -c -o fooT.o fooT.c

>
> <ot>
> My gcc man page says -std=c89 is identical to -ansi - is there a
> particular reason you chose the former?
> </ot>


Sorry for taking so long to answer (had some days off):
-std=c89 instead of -ansi is IMO more concise and certainly
more consistent as I also use -std=c99 (or, way OT, -std=c++9.
Essentially, it comes down to personal preference.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
undefined reference(compile error), plz help eric C++ 4 06-09-2011 08:23 PM
How to compile the following source code in VC6// I have error inVC++6 but compile ok in GCC fAnSKyer C++ 2 06-07-2009 07:57 AM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
compile error with Boost Function object and templated function (Intel C++ compiler) Faheem Mitha C++ 2 08-26-2006 06:16 PM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments