How to use Math functions with MCUXpresso?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to use Math functions with MCUXpresso?

2,354 Views
mjbcswitzerland
Specialist V

Hi All

Any ideas as to how to use math functions in MCUXpresso?

The project in hand can't link due to missing math library, eg
FLASH_target/../Applications/Project/time.c:213: undefined reference to `sin'
FLASH_target/../Applications/Project/calculations.c:219: undefined reference to `tan'
FLASH_target/../Applications/Project/calculations.c:220: undefined reference to `atan'
... etc.

Normally I add -lm to link in the GCC math libraries but this has no effect here.

In the project properties there is a tab to add libraries but which one is needed, or which path?

pastedImage_1.png


Searing in the IDE user's guide the only reference too math is with respect to Redlib extension:

pastedImage_4.png

Thanks in advance for any help.

regrds

Mark

P.S Also the same problem when trying with KDS....

Labels (1)
0 Kudos
5 Replies

1,663 Views
lpcxpresso_supp
NXP Employee
NXP Employee

The "system" libraries are found in the appropriate directory based on the build options used (cpu/fpu/etc) using GCC's multilib support. You can generate a list of directories vs options by invoking :

arm-none-eabi-gcc -print-multi-lib

But this does raise the question of : what are you actually trying to do and why?

Regards,

MCUXpresso IDE Support

0 Kudos

1,663 Views
BlackNight
NXP Employee
NXP Employee

Hi Mark,

it works for me out of the box, both for MCUXpresso IDE (RedLib) and KDS (newlib-nano). I think you have your compiler and/or linker switches wrong. Here is what I have as reference (I cut down the list of includes and object files as this is not of interest here):

KDS:

Building file: ../Sources/Application.c
Invoking: Cross ARM GNU C Compiler
arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O3 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections  -g3 -I"........" -std=c99 -MMD -MP -MF"Sources/Application.d" -MT"Sources/Application.o" -c -o "Sources/Application.o" "../Sources/Application.c"
Finished building: ../Sources/Application.c
 
Building target: FRDM-K64F_Adafruit_SSD1351.elf
Invoking: Cross ARM GNU C++ Linker
arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O3 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections  -g3 -T "C:/Users/Erich Styger/Data/GitRepos/McuOnEclipse/Examples/KDS/FRDM-K64F120M/FRDM-K64F_Adafruit_SSD1351/Project_Settings/Linker_Files/ProcessorExpert.ld" -Xlinker --gc-sections -L"C:/Users/Erich Styger/Data/GitRepos/McuOnEclipse/Examples/KDS/FRDM-K64F120M/FRDM-K64F_Adafruit_SSD1351/Project_Settings/Linker_Files" -Wl,-Map,"FRDM-K64F_Adafruit_SSD1351.map" -specs=nano.specs -specs=nosys.specs -o "FRDM-K64F_Adafruit_SSD1351.elf"  ./Static_Code/System/CPU_Init.o .......  
Finished building target: FRDM-K64F_Adafruit_SSD1351.elf

MCUXpresso IDE:

Building file: ../source/main.c
Invoking: MCU C Compiler
arm-none-eabi-gcc -D__REDLIB__ -DFSL_RTOS_BM -DSDK_OS_BAREMETAL -DSDK_DEBUGCONSOLE=0 -DCR_INTEGER_PRINTF -DPRINTF_FLOAT_ENABLE=0 -DCPU_MK64FN1M0VLL12 -DCPU_MK64FN1M0VLL12_cm4 -D__MCUXPRESSO -DDEBUG -I...... -O0 -fno-common -g3 -Wall -c -ffunction-sections -fdata-sections -ffreestanding -fno-builtin -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -D__REDLIB__ -specs=redlib.specs -MMD -MP -MF"source/main.d" -MT"source/main.o" -MT"source/main.d" -o "source/main.o" "../source/main.c"
Finished building: ../source/main.c
 
Building target: FRDM-K64F_Adafruit_SSD1351.axf
Invoking: MCU Linker
arm-none-eabi-gcc -nostdlib -Xlinker -Map="FRDM-K64F_Adafruit_SSD1351.map" -Xlinker --gc-sections -Xlinker -print-memory-usage -Xlinker --sort-section=alignment -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -T FRDM-K64F_Adafruit_SSD1351_Debug.ld -o "FRDM-K64F_Adafruit_SSD1351.axf"  ./startup/startup_mk64f12.o  ./source/Application.o .....

I hope this helps,

Erich

0 Kudos

1,663 Views
mjbcswitzerland
Specialist V

Hi Eric

I don't think there are any problems with linker switches and such.
What I find is that any project created in the IDEs (eg. MCUXpressor) adds an include linker script to the linker script with a group specifying the libraries to be used.

Eg.

/*
 * GENERATED FILE - DO NOT EDIT
 * (c) Code Red Technologies Ltd, 2008-2013
 * (c) NXP Semiconductors 2013-2018
 * Generated linker script file for MK64FN1M0xxx12
 * Created from linkscript.ldt by FMCreateLinkLibraries
 * Using Freemarker v2.3.23
 * MCUXpresso IDE v10.0.0 [Build 344] [2017-03-21]  on 28-Aug-2018 03:26:43
 */

INCLUDE "frdmk64f_driver_examples_rtc_Debug_library.ld"
INCLUDE "frdmk64f_driver_examples_rtc_Debug_memory.ld"

ENTRY(ResetISR)

SECTIONS
{
    /* MAIN TEXT SECTION */
    .text : ALIGN(4)
...

and then in one of the include files there is a list of libraries - eg.

/*
 * GENERATED FILE - DO NOT EDIT
 * (c) Code Red Technologies Ltd, 2008-2013
 * (c) NXP Semiconductors 2013-2018
 * Generated linker script file for MK64FN1M0xxx12
 * Created from library.ldt by FMCreateLinkLibraries
 * Using Freemarker v2.3.23
 * MCUXpresso IDE v10.0.0 [Build 344] [2017-03-21]  on 28-Aug-2018 03:26:43
 */

GROUP (
  libcr_nohost.a
  libcr_c.a
  libcr_eabihelpers.a
)

It is the libcr_c.a library that is needed (if you delete it from the included linker script file group you will find that projects will fail with non-found symbols).

What I am look for is the method to add this library in a way that it doesn't need special linker scripts and also find the correct version of the library. At the moment I can get it to build by pointing it to the correct version (there are 21 available but I can only find it by trial and error since most directory names don't make much sense to me - however it breaks if the processor type is changed (core type and with/without FPU ).

Using the -l entry (also with -L to give it a path) it always fails because it can find the file (?) whereby I assume that it would then (somehow) take the correct one (?)

Regards

Mark

0 Kudos

1,663 Views
mjbcswitzerland
Specialist V

Hi All

As follow-up:


I note that Redlib, Newlib or NewlibNano can be used when the setting is to use managed linker script mechanism, which look to include libm.a.

Since managed linker script is not being used the advice is to modify the linker script to add entries before the SECTION line, whereby if I have understood correctly it needs
"GROUP (libgcc.a libc.a libm.a libcr_newlib_none.a)"
to be added.

This hasn't been tried (yet) because the linker script file is shared with other GCC projects and the preferred method would be to add the appropriate library in an IDE setting as shown in the first image. Since there are 21 occurrences of libm.a in the MCUXpresso directories the question is how to chose the correct one for a particular processor and keep it compatible when changing between M0+ and M4 cores with and without FPU (which seems the big advantage of the -lm option that is not working)?

Regards

Mark

0 Kudos

1,663 Views
BlackNight
NXP Employee
NXP Employee

Mark,

you don't need to add the libraries manually, as the compiler/linker driver will pick the ones matching to your application/linker settings. So I think you might have missed options like specifying your FPU?

I recommend you create a project in the IDE and check what it sets up for you?

I hope this helps,

Erich

0 Kudos