Question:
Undefined reference?
Bob R.
2008-06-13 22:26:31 UTC
Okay, I'm a beginner at C and I'm writing this program:

#include
#include
#include "genlib.h"
#include "simpio.h"

main()
{
double n1, n2, total;
printf("The first number is \n");
n1 = GetReal();
printf("The second number is \n");
n2 = GetReal();
total = n1 + n2;
printf("The total is %g. /n", total);
}

When I compile, it says [Linker error] undefined reference to 'GetReal()' and 'Id returned 1 exit status' (the function GetReal is defined in simpio, i believe)

how can I fix these errors? thanks!
Three answers:
gene_frequency
2008-06-14 15:26:14 UTC
There is no function GetReal() defined in this code. Please note that main is defined by everything between it's curly braces:



main()

{

.... stuff here defines main()...

.....

double n1, n2, total;

printf("The first number is \n");

....

....

}



Now, we should also see,



GetReal()

{

....with stuff here...

....

}



...but there isn't any GetReal() function defined here ...that's why. Thus, GetReal() is 'undefined'
☮ Vašek
2008-06-14 06:24:48 UTC
Your use of GetReal passed through the compiler but the linker did not understand it.

The simpio.h file only tells the compiler which way to call the function, but the linker complains it does not know its definition – what should the function actually do.

If you found the simpio library somewhere on the Internet, it should have come either with its source (.c) or compiled in some form (.obj or .so or something other looking weird). If you have the .c, try adding it to your project. If you have the compiled file, you have to tell the linker about it, which usually is somewhere in your Project Options.



Example in command line:

gcc program.c simpio.c

OR

gcc program.c -lsimpio
Craig R
2008-06-14 06:20:50 UTC
You need to include the definition for GetReal() and make sure you're calling it right.



You may need to return a value at the end of main(). As I recall, main returns an int.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...