cancel
Showing results for 
Search instead for 
Did you mean: 

sqrtf() from math.h problem

matthias23
Associate II
Posted on January 27, 2014 at 18:00

Hi everyboy,

I have a problem (Unresolved inclusion: <math.h>) with using sqrtf() on STM32 board from OLIMEX. I’m using the free tool chain provided by OLIMEX - Eclipse Helios - OpenOCD - YAGARTO here is the Makefile provided by OLIMEX I tried to add libm and libc but then I get a lot of missing references (__aeabi_.....) I tried to copy math.h,reent.h,ieeefp.h and_ansi.h to my lib folder (still undefined reference to `sqrtf'). can somebody tell me where my problem is (or better how to solve it) because I have no idea left.

# Makefile for compiling the Getting Started project
#-------------------------------------------------------------------------------
# User-modifiable options
#-------------------------------------------------------------------------------
# Trace level used for compilation
# (can be overriden by adding TRACE_LEVEL=#number to the command-line)
# TRACE_LEVEL_DEBUG 5
# TRACE_LEVEL_INFO 4
# TRACE_LEVEL_WARNING 3
# TRACE_LEVEL_ERROR 2
# TRACE_LEVEL_FATAL 1
# TRACE_LEVEL_NO_TRACE 0
TRACE_LEVEL = 4
# Optimization level
OPTIMIZATION = -O0
# Output file basename
OUTPUT = main
# Output directories
BIN = .
OBJ = obj
# library dirs
LIBRARYSRC = ./lib/src
STARTUPFILE = ./lib/startup_stm32f10x_cl.s
#-------------------------------------------------------------------------------
# Tools
#-------------------------------------------------------------------------------
# Tool suffix when cross-compiling
CROSS_COMPILE = arm-none-eabi-
CC = $(CROSS_COMPILE)gcc
SIZE = $(CROSS_COMPILE)size
STRIP = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
LD = $(CROSS_COMPILE)ld
AS = $(CROSS_COMPILE)as
#-------------------------------------------------------------------------------
# Files
#-------------------------------------------------------------------------------
# include folders
INCLUDES = -I./
INCLUDES += -I./lib/
INCLUDES += -I./lib/inc/
INCLUDES += -I./lib/inc/../../
# Objects built from C source files
C_OBJECTS = $(OBJ)/main.o
C_OBJECTS += $(OBJ)/system_stm32f10x.o
C_OBJECTS += $(OBJ)/stm32f10x_gpio.o
C_OBJECTS += $(OBJ)/stm32f10x_rcc.o
C_OBJECTS += $(OBJ)/stm32f10x_it.o
# Objects built from Assembly source files
ASM_OBJECTS = $(OBJ)/startup_stm32f10x_cl.o
LINKER_SCRIPT = ./lib/stm32_flash.ld
#LINKER_SCRIPT = ./lib/stm32_ram.ld
# Append OBJ and BIN directories to output filename
OUTPUT := $(BIN)/$(OUTPUT)
#-------------------------------------------------------------------------------
# Rules
#-------------------------------------------------------------------------------
# Flags
CFLAGS = -Wall -fno-common -c -g -mcpu=cortex-m3 -mthumb
CFLAGS += -g $(OPTIMIZATION) $(INCLUDES) -DTRACE_LEVEL=$(TRACE_LEVEL)
ASFLAGS = -g -mapcs-32
LDFLAGS = -g -v -nostartfiles
OBJCOPYFLAGS = -O binary
OBJDUMPFLAGS = -x --syms -S
all: $(BIN) $(OBJ) $(OUTPUT).out
$(BIN) $(OBJ):
mkdir $@
$(OUTPUT).out: $(C_OBJECTS) $(ASM_OBJECTS) $(LINKER_SCRIPT)
@ echo ''..linking''
$(LD) $(LDFLAGS) -Map $(OUTPUT).map -T$(LINKER_SCRIPT) -o $(OUTPUT).out $(C_OBJECTS) $(ASM_OBJECTS) libgcc.a
$(OBJCOPY) $(OBJCOPYFLAGS) $(OUTPUT).out $(OUTPUT).bin
# $(OBJDUMP) $(OBJDUMPFLAGS) $(OUTPUT).out > $(OUTPUT).lss
@ echo ''...completed.''
$(C_OBJECTS): main.c system_stm32f10x.c
@ echo ''.compiling''
$(CC) $(CFLAGS) -o $(OBJ)/main.o main.c
$(CC) $(CFLAGS) -o $(OBJ)/system_stm32f10x.o system_stm32f10x.c
$(CC) $(CFLAGS) -o $(OBJ)/stm32f10x_it.o stm32f10x_it.c
@ echo ''.compiling libraries''
$(CC) $(CFLAGS) -o $(OBJ)/stm32f10x_gpio.o $(LIBRARYSRC)/stm32f10x_gpio.c 
$(CC) $(CFLAGS) -o $(OBJ)/stm32f10x_rcc.o $(LIBRARYSRC)/stm32f10x_rcc.c 
$(ASM_OBJECTS): $(STARTUPFILE)
@ echo ''.assembling''
$(AS) $(ASFLAGS) -o $(OBJ)/startup_stm32f10x_cl.o $(STARTUPFILE)
clean:
-rm -f $(OBJ)/*.o $(BIN)/*.out $(BIN)/*.bin $(BIN)/*.dmp $(BIN)/*.map $(BIN)/*.lss

#sqrtf-math.h
9 REPLIES 9
chen
Associate II
Posted on January 27, 2014 at 18:23

Hi

''I tried to add libm and libc but then I get a lot of missing references (__aeabi_.....)''

What do you mean you have tried to add libm and libc?

Were you trying to do #include or trying to add them to the linker script?

matthias23
Associate II
Posted on January 27, 2014 at 18:35

$(OUTPUT).out: $(C_OBJECTS) $(ASM_OBJECTS) $(LINKER_SCRIPT)
@ echo ''..linking''
$(LD) $(LDFLAGS) -Map $(OUTPUT).map -T$(LINKER_SCRIPT) -o $(OUTPUT).out $(C_OBJECTS) $(ASM_OBJECTS) libgcc.a 

libm.a libc.a
$(OBJCOPY) $(OBJCOPYFLAGS) $(OUTPUT).out $(OUTPUT).bin
# $(OBJDUMP) $(OBJDUMPFLAGS) $(OUTPUT).out > $(OUTPUT).lss
@ echo ''...completed.''

and copy them to my project folder. But as mentioned, then I get a lot of missing references. So this seems not to be the solution.
Posted on January 27, 2014 at 18:44

The actual errors and command line might be more useful. But sounds like you need to get a handle on the libraries you're feeding it, and if they match the architecture you're building.

There also seems to be a confusion about include files, and libraries. Include files have syntax information, the library provides the body of code required, and it is the latter that sounds like it's missing.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 27, 2014 at 18:57

\yagarto472\arm-none-eabi\lib\thumb\v7m\libm.a

I don't have libgcc.a, rather libg.a

You should review the .MAP file, perhaps starting with something that is working.

I don't have a GCC floating point example to hand, but have built one before, will need to dig it up.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
matthias23
Associate II
Posted on January 27, 2014 at 19:16

Hi Clive,

first of all thanks for your help!

\yagarto\arm-none-eabi\lib\thumb\v7m\libm.a is the one i used.

the project is working. its the blinky example from the OLIMEX website 

https://www.olimex.com/Products/ARM/JTAG/_resources/OpenOCD/

but now I want to add math in this testproject.

.MAP file? I have never seen! should I?

If you have any example, they may help me I would be very happy!

Posted on January 27, 2014 at 23:37

Not that it helps too much for the STM32F1, but this is my FPU build for the STM32F401C-DISCO

https://drive.google.com/file/d/0B7OY5pub_GfIUW9WNHdGUkhBSEE/edit?usp=sharing

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
frankmeyer9
Associate II
Posted on January 28, 2014 at 08:52

Just to clarify - ''

undefined reference to ...

'' is a linker error, and not associated with the header.

And regarding your make file - have you tried to add ''-lm'' to your linker flags (i.e. LDFLAGS) ?

matthias23
Associate II
Posted on January 28, 2014 at 09:15

@fm: I tried to use the -lm flag but it’s not found, I could not figure out why.

arm-none-eabi-ld: cannot find -lm

make: *** [main.out] Error 1

that’s why I tried to use libm and libc what should be the same!

And now I found the solution as always with free toolchains they are free but sometimes you have to think yourself.

I just switched order of the libs from ''libgcc.a libm.a libc.a'' to ''libm.a libc.a  libgcc.a'' and now it works.

@clive1: as I want to switch to the 

STM32F405 because of the FPU (and the great

 cost effectiveness 

of the 

https://www.olimex.com/Products/ARM/ST/STM32-P405/

board

) your code may help with other questions regarding FPU. 

I think I will meet you here with my next strange question on STM32 problems 😉

frankmeyer9
Associate II
Posted on January 28, 2014 at 10:24

I tried to use the -lm flag but it’s not found, I could not figure out why.

To cite clive1 from one of his former posts in this thread:

But sounds like you need to get a handle on the libraries you're feeding it, and if they match the architecture you're building.

Assuming the architecture is correct, you need to tell your linker the location of the libraries.

This is usually done with the ''-L'' flag. Just consult the gcc manual pages, or example make files around (not necessarily only for Cortex M).

Make files are not a trivial topic, the syntax is, say, peculiar ...