Undefined reference

W

wdh3rd

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?
 
W

wdh3rd

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) );
 
I

Ian Collins

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

wdh3rd

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.

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

Ian Collins

*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.
 
W

wdh3rd

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

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

Barry Schwarz

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 */

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
 
D

Daniel Rudy

At about the time of 4/5/2007 4:09 PM, (e-mail address 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
 
S

Stephen Sprunk

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. said:
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
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top