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