By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,302 Members | 1,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,302 IT Pros & Developers. It's quick & easy.

No compile error for undefined function

P: n/a
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

Dec 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
sa********@gmail.com 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
Dec 22 '05 #2

P: n/a
sa********@gmail.com 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

Dec 22 '05 #3

P: n/a
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

Dec 22 '05 #4

P: n/a
sa********@gmail.com 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
Dec 22 '05 #5

P: n/a
sa********@gmail.com 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.
Dec 22 '05 #6

P: n/a
Michael Mair <Mi**********@invalid.invalid> 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.
Dec 22 '05 #7

P: n/a

Christopher Benson-Manica wrote:
Michael Mair <Mi**********@invalid.invalid> 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

Dec 22 '05 #8

P: n/a
On 2005-12-22 15:21:21 +0100, Christopher Benson-Manica
<at***@nospam.cyberspace.org> said:
Michael Mair <Mi**********@invalid.invalid> 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
cl*******@gmail.com

Dec 22 '05 #9

P: n/a
Christopher Benson-Manica wrote:
Michael Mair <Mi**********@invalid.invalid> 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++98).
Essentially, it comes down to personal preference.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jan 3 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.