Undefined reference - Why?

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi.

I have some problems with this litle code-piece

---- start test.c ----

#include

void a_function(int tall);

int main(void){
int hoyde;
double trykk;
hoyde = pow(2,2);
a_function(hoyde);
// trykk = hoyde * 1.234;

return 0;
} /* main */

void a_function(int tall){
}

---- end test.c ----

This is what happends:

M:\prosjekt\tuger\hw\firmware>make
make: *** Warning: File `tuger.c' has modification time in the future(2001-03-04 15:58:04 > 2001-03-04 15:55:54)
avr-gcc -c -g -O3 -Wall -Wstrict-prototypes -Wa,-ahlms=tuger.lst
-mmcu=at90s4433
-I. tuger.c -o tuger.o
tuger.c: In function `main':
tuger.c:8: warning: unused variable `trykk'
avr-gcc tuger.o -Wl,-Map=tuger.map,--cref -mmcu=at90s4433 -o tuger.elf
tuger.o: In function `main':
M:\prosjekt\tuger\hw\firmware/tuger.c:10: undefined reference to `pow'
make: *** [tuger.elf] Error 1

But if I change this:

a_function(hoyde);
// trykk = hoyde * 1.234;
To this:

// a_function(hoyde);
trykk = hoyde * 1.234;

It compiles without any errormessages...

Version:
M:\prosjekt\tuger\hw\firmware>avr-gcc --version
2.97

Installed on a Win2000 5.00.2195
Installed using the file
http://www.avrfreaks.com/AVRGCC/...
(Gunnar Henne's distibution).

Any suggestions?

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Math operations such as multiplication, division, power etc. are not built in to the AVR, and are implemented using software (assembly) routines. These routines are not compiled by default if your program doesn't use any mathematical operations. When you add the multiplication statement, the compiler looks into the math library and finds the pow() function too. If you just want pow(), I think you should modify the makefile tou call the linker with the -lm option which links the math library - at least this was the solution offered at a previous forum posting (I haven't tried it myself).

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you want to use any of the functions that are in math.h, you need to add the -lm to your linker options, like the following
#linker flags
LDFLAGS = -Wl,-Map=$(TRG).map,--cref -lm

the reason you don't get an error when you compile with
// a_function(hoyde);
is that the compiler is optimizing your code and since you aren't doing anything with any of your variables, it simply removes those lines of code. If you look at the assemble code, you will see that main doesn't do anything at all, the entire thing is optimized out. (this sometimes makes it difficult to write a little test program to run in the simulator, because if you don't do something with the data, it removes it)
When you uncomment the function call, it has to calculate hoyde to pass it to the function, so it needs pow().

p.s.- you don't need to add -lm to use multiplication or division, only the functions in math.h.

-jeff

admin's test signature