av92
av92

Reputation: 130

undefined reference to `pow' in contiki application

enter image description hereIs it possible to work with math.h library in contiki-cooja simulator ?. I am using contiki 3.0 on ubuntu 18.04 LTS

I tried adding LDFLAGS += -lm in makefile of hello-world application. Moreover, i also tried adding -lm in Makefile.include file. Things don't work. What is the correct place to add -lm.

hello-world.c

#include "contiki.h"

#include <stdio.h> /* For printf() /
#include <math.h>
#define DEBUG DEBUG_PRINT
static float i;
/---------------------------------------------------------------------------/
PROCESS(hello_world_process, "Hello world process");
AUTOSTART_PROCESSES(&hello_world_process);
/---------------------------------------------------------------------------/
PROCESS_THREAD(hello_world_process, ev, data)
{
PROCESS_BEGIN();
i = 2.1;
printf("Hello, world\n");
printf("%i\n", (int)pow(10,i));
printf("%i\n", (int)(M_LOG2Ei));
PROCESS_END();
}
/---------------------------------------------------------------------------/

Makefile

CONTIKI_PROJECT = hello-world
all: $(CONTIKI_PROJECT)

CONTIKI = ../..
include $(CONTIKI)/Makefile.include
LDFLAGS += -lm

Upvotes: 0

Views: 879

Answers (1)

kfx
kfx

Reputation: 8537

First, you can added external libraries to Contiki with:

TARGET_LIBFILES = -lm

Make sure you do this before the include $(CONTIKI)/Makefile.include line, not after!

Second, which platform are you compiling for? The msp430 platforms do not have the pow function in the math library. They only have the powf function operating on single-precision floating point numbers, and the built-in (intrinsic) function pow operating on integers.

If you want to operate on floating point numbers, change your code to this:

float f = 2.1;
pow(10, f);

to this

float f = 2.1;
powf(10, f);

Upvotes: 0

Related Questions