1

I'm trying to add the math.h library inside a C project on Eclipse IDE for C/C++. I am currently working on windows.

Whenever I try to compile, I get the error, undefined reference to 'floor' and undefined reference to 'pow'.

I tried looking up on solutions which tells me to go to Project -> Properties -> C/C++ Build -> Settings.

But from there, I do not have any tool tab or a linker option. I only have "ToolChains", "Devices", "Binary Parsers" and "Error Parsers" tab.

So where and how do I link the -lm option in order to make the project compile ?

Update:

The Makefile is as follows:

PROJECT_NAME := blinky_blank_pca10036

export OUTPUT_FILENAME
#MAKEFILE_NAME := $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
MAKEFILE_NAME := $(MAKEFILE_LIST)
MAKEFILE_DIR := $(dir $(MAKEFILE_NAME) ) 

TEMPLATE_PATH = ../../../../../../components/toolchain/gcc
ifeq ($(OS),Windows_NT)
include $(TEMPLATE_PATH)/Makefile.windows
else
include $(TEMPLATE_PATH)/Makefile.posix
endif

MK := mkdir
RM := rm -rf

#echo suspend
ifeq ("$(VERBOSE)","1")
NO_ECHO := 
else
NO_ECHO := @
endif

# Toolchain commands
CC              := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-gcc'
AS              := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-as'
AR              := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-ar' -r
LD              := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-ld'
NM              := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-nm'
OBJDUMP         := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-objdump'
OBJCOPY         := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-objcopy'
SIZE            := '$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-size'

#function for removing duplicates in a list
remduplicates = $(strip $(if $1,$(firstword $1) $(call remduplicates,$(filter-out $(firstword $1),$1))))

#source common to all targets
C_SOURCE_FILES += \
../../../../../../components/toolchain/system_nrf52.c \
../../../main.c \
../../../../../../components/drivers_nrf/delay/nrf_delay.c \

#assembly files common to all targets
ASM_SOURCE_FILES  = ../../../../../../components/toolchain/gcc/gcc_startup_nrf52.s

#includes common to all targets
INC_PATHS  = -I../../../../../../components/toolchain/gcc
INC_PATHS += -I../../../../../../components/toolchain
INC_PATHS += -I../../..
INC_PATHS += -I../../../../../bsp
INC_PATHS += -I../../../../../../components/device
INC_PATHS += -I../../../../../../components/drivers_nrf/delay
INC_PATHS += -I../../../../../../components/drivers_nrf/hal

OBJECT_DIRECTORY = _build
LISTING_DIRECTORY = $(OBJECT_DIRECTORY)
OUTPUT_BINARY_DIRECTORY = $(OBJECT_DIRECTORY)

# Sorting removes duplicates
BUILD_DIRECTORIES := $(sort $(OBJECT_DIRECTORY) $(OUTPUT_BINARY_DIRECTORY) $(LISTING_DIRECTORY) )

#flags common to all targets
CFLAGS  = -DCONFIG_GPIO_AS_PINRESET
CFLAGS += -DBOARD_PCA10036
CFLAGS += -DNRF52
CFLAGS += -DBSP_DEFINES_ONLY
CFLAGS += -mcpu=cortex-m4
CFLAGS += -mthumb -mabi=aapcs --std=gnu99
CFLAGS += -Wall -Werror -O0 -g3
CFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16
# keep every function in separate section. This will allow linker to dump unused functions
CFLAGS += -ffunction-sections -fdata-sections -fno-strict-aliasing
CFLAGS += -fno-builtin --short-enums

# keep every function in separate section. This will allow linker to dump unused functions
LDFLAGS += -Xlinker -Map=$(LISTING_DIRECTORY)/$(OUTPUT_FILENAME).map
LDFLAGS += -mthumb -mabi=aapcs -L $(TEMPLATE_PATH) -T$(LINKER_SCRIPT)
LDFLAGS += -mcpu=cortex-m4
LDFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16
# let linker to dump unused sections
LDFLAGS += -Wl,--gc-sections
# use newlib in nano version
LDFLAGS += --specs=nano.specs -lc -lnosys

# Assembler flags
ASMFLAGS += -x assembler-with-cpp
ASMFLAGS += -DCONFIG_GPIO_AS_PINRESET
ASMFLAGS += -DBOARD_PCA10036
ASMFLAGS += -DNRF52
ASMFLAGS += -DBSP_DEFINES_ONLY
#default target - first one defined
default: clean nrf52832_xxaa

#building all targets
all: clean
    $(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e cleanobj
    $(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e nrf52832_xxaa 

#target for printing all targets
help:
    @echo following targets are available:
    @echo   nrf52832_xxaa


C_SOURCE_FILE_NAMES = $(notdir $(C_SOURCE_FILES))
C_PATHS = $(call remduplicates, $(dir $(C_SOURCE_FILES) ) )
C_OBJECTS = $(addprefix $(OBJECT_DIRECTORY)/, $(C_SOURCE_FILE_NAMES:.c=.o) )

ASM_SOURCE_FILE_NAMES = $(notdir $(ASM_SOURCE_FILES))
ASM_PATHS = $(call remduplicates, $(dir $(ASM_SOURCE_FILES) ))
ASM_OBJECTS = $(addprefix $(OBJECT_DIRECTORY)/, $(ASM_SOURCE_FILE_NAMES:.s=.o) )

vpath %.c $(C_PATHS)
vpath %.s $(ASM_PATHS)

OBJECTS = $(C_OBJECTS) $(ASM_OBJECTS)

nrf52832_xxaa: OUTPUT_FILENAME := nrf52832_xxaa
nrf52832_xxaa: LINKER_SCRIPT=blinky_gcc_nrf52.ld
nrf52832_xxaa: $(BUILD_DIRECTORIES) $(OBJECTS)
    @echo Linking target: $(OUTPUT_FILENAME).out
    $(NO_ECHO)$(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
    $(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e finalize

## Create build directories
$(BUILD_DIRECTORIES):
    echo $(MAKEFILE_NAME)
    $(MK) $@

# Create objects from C SRC files
$(OBJECT_DIRECTORY)/%.o: %.c
    @echo Compiling file: $(notdir $<)
    $(NO_ECHO)$(CC) $(CFLAGS) $(INC_PATHS) -c -o $@ $<

# Assemble files
$(OBJECT_DIRECTORY)/%.o: %.s
    @echo Compiling file: $(notdir $<)
    $(NO_ECHO)$(CC) $(ASMFLAGS) $(INC_PATHS) -c -o $@ $<


# Link
$(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out: $(BUILD_DIRECTORIES) $(OBJECTS)
    @echo Linking target: $(OUTPUT_FILENAME).out
    $(NO_ECHO)$(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out


## Create binary .bin file from the .out file
$(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin: $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
    @echo Preparing: $(OUTPUT_FILENAME).bin
    $(NO_ECHO)$(OBJCOPY) -O binary $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin

## Create binary .hex file from the .out file
$(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex: $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
    @echo Preparing: $(OUTPUT_FILENAME).hex
    $(NO_ECHO)$(OBJCOPY) -O ihex $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex

finalize: genbin genhex echosize

genbin:
    @echo Preparing: $(OUTPUT_FILENAME).bin
    $(NO_ECHO)$(OBJCOPY) -O binary $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin

## Create binary .hex file from the .out file
genhex: 
    @echo Preparing: $(OUTPUT_FILENAME).hex
    $(NO_ECHO)$(OBJCOPY) -O ihex $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex

echosize:
    -@echo ''
    $(NO_ECHO)$(SIZE) $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
    -@echo ''

clean:
    $(RM) $(BUILD_DIRECTORIES)

cleanobj:
    $(RM) $(BUILD_DIRECTORIES)/*.o

flash: $(MAKECMDGOALS)
    @echo Flashing: $(OUTPUT_BINARY_DIRECTORY)/$<.hex
    nrfjprog --erasepage 0x0-0x80000 -f nrf52
    nrfjprog --program $(OUTPUT_BINARY_DIRECTORY)/$<.hex -f nrf52
    nrfjprog --reset

## Flash softdevice
Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
Ahmad Shah
  • 199
  • 4
  • 14

3 Answers3

2

in Eclipse:

click menu item: `Project'
    (opens pull down menu)

click pulldown item: `Properties`
   (opens `Properties` window)

If `Properties` is grayed, then make trivial edit to program, like adding a space

in `Properties` window:
    click left tab: `C/C++Build` 
        (opens sub tabs)

    click sub tab: `Settings`
        (opens `Settings` window)

in `Settings` window:
    (if not using gcc, the following may be slightly different)
    click `GCC CLinker` 
        (opens sub tabs)
        click sub tab: `Libraries`

in third column (Libraries)
    click the left most icon `add` 
        (located to the right of the text: `Libraries (-l)`
        (opens `Enter Value` window)

In the `Enter Value` window 
    type `m`
    click `ok`

 if, below the text: `Library search path (-L)` 
 the path `/usr/lib` is NOT listed, then:

     click the left most icon (add) 
     (located to the right of the text: `Library search path (-L)`)
     (opens the `Add Directory Path` window)

in the `Add directory path` window 
    (under the text `Directory:`)
    type in: `/usr/lib`
    click `ok`

click `ok`

to test:
    in Eclipse:   
    click `Project`
        (opens a pull down menu)
        click `Build All`

caveat: I'm running Eclipse in Linux, so the details may be slightly different if your running MAC-X or Windows or using a different compiler/linker

user3629249
  • 16,402
  • 1
  • 16
  • 17
  • In the Setings tab, there is no option for GCC CLinker. – Ahmad Shah Feb 19 '16 at 23:35
  • I'm sure I mentioned, in my answer, that if using a different compiler that the tabs would be different, If no `CLinker` tab, then may have to place the library path and library name items in the – user3629249 Feb 20 '16 at 03:29
  • in Eclipse, in the `project/properties` window in the left tab `C/C++ Build` (when opened) is the tab: `Tool Chain Editor` click it. in the Tool Chain Editor sub window below the line: `Display compatible toolchains only` you can select which tool chain (I have 'Linux GCC') What is your selection? and what is selected in the following: 'Current builder:' line? – user3629249 Feb 20 '16 at 03:33
  • The Current toolchain is Cross ARM GCC with the Gnu Make Builler. – Ahmad Shah Feb 20 '16 at 16:30
1

It sounds like you are using Makefiles, if so you need to edit the Makefile to add -lm to the linker command line. It is important that this be placed after any objects that use math functions.

With updated question including Makefile contents I can suggest you change:

LDFLAGS += --specs=nano.specs -lc -lnosys

to

LDFLAGS += --specs=nano.specs -lc -lnosys -lm
LIBS += -lc -lnosys -lm
Jonah Graham
  • 7,890
  • 23
  • 55
  • I have the following MakeFile. Can you please tell me as to where I have to add the -lm option. – Ahmad Shah Feb 20 '16 at 18:00
  • @AhmadShah I can't see any makefile, can you add it to your question? Remember to check formatting so that it looks correct on screen in the question. – Jonah Graham Feb 20 '16 at 18:33
  • Sorry but changing the LDFLAGS lines ( as mentioned above ) has no effect. It still says undefined reference to pow/floor. P.S: I have included the math.h library in the main file so there is no issue there. – Ahmad Shah Feb 22 '16 at 12:35
  • Further edit, the existing LDFLAGS setting is suspicious with regards to -lc use, but may work fine for you. Try adding -lm to LIBS instead/in addition. Also, please post the command line and output as used to link (as resolved) if you have further errors (with no echo suppression, i.e. VERBOSE=1 on command line to make!). – Jonah Graham Feb 22 '16 at 12:48
1

If using Eclipse CDT GUI:

  1. Project Properties
  2. C/C++ Build/Settings
  3. Tool Settings/GCC C Linker/Libraries
  4. Icon: Add...
  5. m enter image description here
Danijel
  • 8,198
  • 18
  • 69
  • 133