<> wrote in message
news: 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