473,320 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

No compile error for undefined function

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
9 2254
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
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
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
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
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: skubik | last post by:
Anyone else have this problem compiling PHP 4.3.3 from source under Linux (I'm using Slackware 9.0)? Under PHP 4.3.2 I had no compilation problems at all, but now all of the sudden, compiling...
0
by: Adam McCarthy | last post by:
I'm trying to get a cross compiler working for arm-wince-pe. This is the output for the primes Pyrex example. If I compile simple Hello, World's etc, it works fine, but for some reason Python...
2
by: superprad | last post by:
Hi I am trying to write a python wrapper for a C code I have using swig. when i try to compile the _wrap.c i get a bunch of these warnings and errors can anyone help Steps I followed : $swig...
25
by: JKop | last post by:
Using MSWindows as an example: On MSWindows, there's a thing called the System Registry, which is a really big database that holds all the settings of the OS. There's API's for working with the...
5
by: Carmine Cairo | last post by:
Hi, I'm working on a project and today I've note a little problem during the compile fase. Here a little piece of code: // 1st version welldone = 0; size = p->getSize(); backbone = new...
0
by: Jérôme Le Bougeant | last post by:
Hello (and sorry for my English), I downloaded the VideoCapture module on the http://videocapture.sourceforge.net/ site. I tested it with a webcam and that functions. Now I want to...
1
by: å¼ æ²ˆé¹ | last post by:
How to compile the HelloWorld of boost.asio? Maybe this is a stupid problem , but I really don't konw how to find the right way. My compile environment is WinXP, Msys , MinGw , G++ 3.4.2,...
1
by: twelvetone | last post by:
Can anyone explain why this file won't compile? The errors I'm getting in VS.Net are below. ============================ c:\sri\sw\scs\SCS\src\TaskPlans\main.cpp(24): error C3861: 'str':...
0
by: Benjamin Grieshaber | last post by:
Hi, I´m on SuSE 9.3 with xmlrpc-c and xmlrpc-c-devel installed (ver. 0.9.10) I tried to compile php with xmlrpc support and got the following errors: ...
2
by: akhilesh.noida | last post by:
I am trying to compile glibc-2.5 for ARM based board. But I am getting errors while configuring it. Please check and give your inputs for resolving this. configure command : $...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.