No compile error for undefined function

S

sam.s.kong

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
 
D

Default User

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
 
S

sam.s.kong

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
 
P

pete

Thanks for the reply.


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.
 
M

Michael Mair

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).
 
C

Christopher Benson-Manica

Michael Mair said:
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>
 
T

Targeur fou

Christopher said:
Hello,


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


<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
 
C

Clark S. Cox III

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


<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>
 
M

Michael Mair

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


<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++98).
Essentially, it comes down to personal preference.

Cheers
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Can't compile lib tiff 4
Undefined variable error in array 3
Compile error 12
Need Help with error 0
compile error 30
undefined reference to `link' 21
Why no compilation error. 14
Undefined virtual Function Table 4

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top