Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Undefined reference

Reply
Thread Tools

Undefined reference

 
 
wdh3rd@gmail.com
Guest
Posts: n/a
 
      04-05-2007
I'm still new at C and can't solve this problem. I've looked through
the FAQ and on the Web, but am not having luck.

I'm getting an "undefined reference" error as well as a "Id returned 1
exit status" error.

I've pared down the code to a simple example:

---------------------------------square.c
---------------------------------------------------

#include "square.h"

main() {

static int square (int a)
{
return a * a;
}

int sq_plus (int a, int b)
{
return square(a) - b;
}

} /* end main */

-----------------------------------------------------
square.h---------------------------------

#include <stdio.h>

int sq_plus(int a, int b);

----------------------------------------
main.c----------------------------------------------------
#include <stdio.h>
#include "square.h"

int main()
{
int a,b;

printf( "Enter two digits: " );
scanf( "%d%d", &a, &b );
printf( "Given %d and %d, squarePlus is %d", a, b,
square(a,b) );
return 0;
}

------------------------------------------------------------------------------------------------

Some questions:

(1) I thought that main() was only supposed to be in the main function
file, but if I don't have a main() in square.c, I get errors.

(2) I don't understand why I'm getting the undefined reference error.
I'm using the Dev compiler and it seems that it is ANSI-compatible.
There were supposedly issues with non-ANSI-compatible compilers, but
that doesn't seem to be the issue. The problem may be as described in
this FAS:

"In the general case of calling code in an external library, using
#include to pull in the right header file(s) is only half of the
story; you also have to tell the linker to search the external library
itself. The declarations in the header file only tell the compiler how
to call the external functions; the header file doesn't supply the
definitions of the external functions, or tell the compiler/linker
where to find those definitions.

In some cases (especially if the functions are nonstandard) obtaining
those definitions may require explicitly asking for the correct
libraries to be searched when you link the program. (Some systems may
be able to arrange that whenever you #include a header, its associated
library, if nonstandard, is automatically requested at link time, but
such a facility is not widespread.)"

If that is the solution to my problem, I'm still confused on how to
"tell the linker to search the external library itself. "

(3) Does the "ld returned 1 exit status" error go away when the
undefined reference error is solved as I'm assuing it does?

 
Reply With Quote
 
 
 
 
wdh3rd@gmail.com
Guest
Posts: n/a
 
      04-05-2007

The line in main.c should actually read:

printf( "Given %d and %d, squarePlus is %d", a, b,
sq_plus(a,b) );

instead of

printf( "Given %d and %d, squarePlus is %d", a, b,
square(a,b) );

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-05-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I'm still new at C and can't solve this problem. I've looked through
> the FAQ and on the Web, but am not having luck.
>
> I'm getting an "undefined reference" error as well as a "Id returned 1
> exit status" error.
>

You'll have to read your compiler documentation to see how to compile
more than one source module into a single executable. Generally,

$CC=your compiler

$CC main.c square.c

should be enough to get you going.

--
Ian Collins.
 
Reply With Quote
 
wdh3rd@gmail.com
Guest
Posts: n/a
 
      04-06-2007
On Apr 5, 6:26 pm, Ian Collins <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > I'm still new at C and can't solve this problem. I've looked through
> > the FAQ and on the Web, but am not having luck.

>
> > I'm getting an "undefined reference" error as well as a "Id returned 1
> > exit status" error.

>
> You'll have to read your compiler documentation to see how to compile
> more than one source module into a single executable. Generally,
>
> $CC=your compiler
>
> $CC main.c square.c
>
> should be enough to get you going.
>
> --
> Ian Collins.


I'm using the Dev-C++ IDE. I'm just pressing the "Compile" button on
there. Shouldn't the IDE know what to do if it's compiling a Main
file?

Sice it's not command line compilation, I thought it would know what
to do.


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-06-2007
(E-Mail Removed) wrote:
> On Apr 5, 6:26 pm, Ian Collins <(E-Mail Removed)> wrote:
>
>>(E-Mail Removed) wrote:
>>
>>>I'm still new at C and can't solve this problem. I've looked through
>>>the FAQ and on the Web, but am not having luck.

>>
>>>I'm getting an "undefined reference" error as well as a "Id returned 1
>>>exit status" error.

>>
>>You'll have to read your compiler documentation to see how to compile
>>more than one source module into a single executable. Generally,
>>
>>$CC=your compiler
>>
>>$CC main.c square.c
>>
>>should be enough to get you going.
>>

*Please don't quote signatures.
>
> I'm using the Dev-C++ IDE. I'm just pressing the "Compile" button on
> there. Shouldn't the IDE know what to do if it's compiling a Main
> file?
>

Can't help you there, you had better ask somewhere where the IDE (or
your platform) is topical.

--
Ian Collins.
 
Reply With Quote
 
wdh3rd@gmail.com
Guest
Posts: n/a
 
      04-06-2007

> Can't help you there, you had better ask somewhere where the IDE (or
> your platform) is topical.
>
> --
> Ian Collins.


I had gcc'd through telnet and gotten the same error, so I figured it
was an error in what I'd coded in general, and not a compiler-specific
issue.

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      04-06-2007
On 5 Apr 2007 16:09:42 -0700, (E-Mail Removed) wrote:

>I'm still new at C and can't solve this problem. I've looked through
>the FAQ and on the Web, but am not having luck.
>
>I'm getting an "undefined reference" error as well as a "Id returned 1
>exit status" error.
>
>I've pared down the code to a simple example:
>
>---------------------------------square.c
>---------------------------------------------------
>
>#include "square.h"
>
> main() {


Delete this and its corresponding } because ...

>
> static int square (int a)
> {


You are not allowed to define one function within another. Did your
compiler not issue a diagnostic here.

> return a * a;
> }
>
> int sq_plus (int a, int b)
> {
> return square(a) - b;
> }
>
> } /* end main */
>
>-----------------------------------------------------
>square.h---------------------------------
>
>#include <stdio.h>


Why? Neither function in square.c needs it and you manually include
it in main.c

>
>int sq_plus(int a, int b);
>
>----------------------------------------
>main.c----------------------------------------------------
>#include <stdio.h>
> #include "square.h"
>
> int main()


int main(void) is more complete.

> {
> int a,b;
>
> printf( "Enter two digits: " );
> scanf( "%d%d", &a, &b );
> printf( "Given %d and %d, squarePlus is %d", a, b,
>square(a,b) );


square is an unknown identifier at this point. square.h declares
sq_plus but not square.

> return 0;
>}
>
>------------------------------------------------------------------------------------------------
>
>Some questions:
>
>(1) I thought that main() was only supposed to be in the main function
>file, but if I don't have a main() in square.c, I get errors.


It doesn't matter where it is defined but there should be only one
main.

What errors? Show the exact code and diagnostic.

>
>(2) I don't understand why I'm getting the undefined reference error.
>I'm using the Dev compiler and it seems that it is ANSI-compatible.
>There were supposedly issues with non-ANSI-compatible compilers, but
>that doesn't seem to be the issue. The problem may be as described in
>this FAS:
>
>"In the general case of calling code in an external library, using
>#include to pull in the right header file(s) is only half of the
>story; you also have to tell the linker to search the external library
>itself. The declarations in the header file only tell the compiler how
>to call the external functions; the header file doesn't supply the
>definitions of the external functions, or tell the compiler/linker
>where to find those definitions.
>
>In some cases (especially if the functions are nonstandard) obtaining
>those definitions may require explicitly asking for the correct
>libraries to be searched when you link the program. (Some systems may
>be able to arrange that whenever you #include a header, its associated
>library, if nonstandard, is automatically requested at link time, but
>such a facility is not widespread.)"
>
>If that is the solution to my problem, I'm still confused on how to
>"tell the linker to search the external library itself. "


Since your square.c has major errors, there is no telling what is
actually in the object file the compiler builds from it.

How you tell your linker to find private object code is a detail of
your implementation. Check your documentation, help file, man page,
whatever.

>
>(3) Does the "ld returned 1 exit status" error go away when the
>undefined reference error is solved as I'm assuing it does?


Another detail of your implementation.


Remove del for email
 
Reply With Quote
 
Daniel Rudy
Guest
Posts: n/a
 
      04-06-2007
At about the time of 4/5/2007 4:09 PM, (E-Mail Removed) stated the
following:
> I'm still new at C and can't solve this problem. I've looked through
> the FAQ and on the Web, but am not having luck.
>
> I'm getting an "undefined reference" error as well as a "Id returned 1
> exit status" error.
>
> I've pared down the code to a simple example:
>
> ---------------------------------square.c
> ---------------------------------------------------
>
> #include "square.h"
>
> main() {
>
> static int square (int a)
> {
> return a * a;
> }
>
> int sq_plus (int a, int b)
> {
> return square(a) - b;
> }
>
> } /* end main */
>
> -----------------------------------------------------
> square.h---------------------------------
>
> #include <stdio.h>
>
> int sq_plus(int a, int b);
>
> ----------------------------------------
> main.c----------------------------------------------------
> #include <stdio.h>
> #include "square.h"
>
> int main()
> {
> int a,b;
>
> printf( "Enter two digits: " );
> scanf( "%d%d", &a, &b );
> printf( "Given %d and %d, squarePlus is %d", a, b,
> square(a,b) );
> return 0;
> }
>


Try this:

#include <stdio.h>

int square(int a);
int sq_plus(int a, int b);


int main(void)
{
int a, b;

printf("Enter two digits: ");
scanf("%d%d", &a, &b);
printf("Given %d and %d, squarePlus is %d\n", a, b, sq_plus(a, b));
return(0);
}

int square(int a)
{
return(a * a);
}

int sq_plus(int a, int b)
{
return(square(a) - b);
}

As for your specific environment, you will need to ask in a
platform/compiler specific forum.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      04-06-2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> I'm still new at C and can't solve this problem. I've looked through
> the FAQ and on the Web, but am not having luck.
>
> I'm getting an "undefined reference" error as well as a "Id returned 1
> exit status" error.
>
> I've pared down the code to a simple example:


That example is very, very broken. Try this:

/* square.h */
int sq_plus (int a, int b);

/* square.c */
#include "square.h"

static int square (int a) {
return a * a;
}

int sq_plus (int a, int b) {
return square(a) - b;
}

/* main.c */

#include <stdio.h>
#include "square.h"

int main() {
int a,b;

printf( "Enter two digits: " );
scanf( "%d%d", &a, &b );
printf( "Given %d and %d, squarePlus is %d", a, b, sq_plus(a,b) );
return 0;
}

/* end source files */

How you compile these files together is platform-specific.

<OT>If you're using GCC it'll go something like this:

gcc -ansi -pedantic -W -Wall -c square.c
gcc -ansi -pedantic -W -Wall -c main.c
gcc square.o main.o -o square

The -c option tells GCC not to link yet because you're compiling multiple
source files; it will produce a .o file for the .c file it's given. The
last line actually links the various .o files together (by calling ld,
usually); since the default output file is "a.out" for historical reasons,
you need the -o option to give the program a sensible name.</OT>

> Some questions:
>
> (1) I thought that main() was only supposed to be in the main function
> file, but if I don't have a main() in square.c, I get errors.
>
> (2) I don't understand why I'm getting the undefined reference error.
> I'm using the Dev compiler and it seems that it is ANSI-compatible.


This has nothing to do with ANSI; your code is broken, and on top of that
you're not compiling/linking it correctly.

> (3) Does the "ld returned 1 exit status" error go away when the
> undefined reference error is solved as I'm assuing it does?


Yes.

BTW, why do you call the function "square plus" if you're _subtracting_ the
second argument? Shouldn't it be "square minus"?

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov


--
Posted via a free Usenet account from http://www.teranews.com

 
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
typeof x == 'undefined' or x == undefined? -Lost Javascript 13 01-31-2007 12:04 AM
undefined vs. undefined (was: new Array() vs []) VK Javascript 45 09-12-2006 05:26 PM
'Undefined' Client-Side Object Reference Felipe ASP .Net 3 07-16-2004 04:01 AM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM
Error: 'undefined reference' in g++ but gcc succeeded Lu C++ 1 07-10-2003 12:55 PM



Advertisments