【问题标题】:undefined reference to `pow' in contiki application在 Contiki 应用程序中未定义对“pow”的引用
【发布时间】:2019-04-15 07:43:20
【问题描述】:

是否可以在 contiki-cooja 模拟器中使用 math.h 库? 我在 ubuntu 18.04 LTS 上使用 contiki 3.0

我尝试在 hello-world 应用程序的 makefile 中添加 LDFLAGS += -lm。此外,我还尝试在 Makefile.include 文件中添加 -lm 。事情行不通。添加-lm的正确位置是什么。

你好世界.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();
}
/---------------------------------------------------------------------------/

生成文件

CONTIKI_PROJECT = hello-world
all: $(CONTIKI_PROJECT)

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

【问题讨论】:

  • 您能否准确显示运行make 时得到的输出?这将显示真正使用了哪些编译器选项和链接器选项。
  • 添加了输出截图

标签: contiki cooja


【解决方案1】:

首先,您可以通过以下方式将外部库添加到 Contiki:

TARGET_LIBFILES = -lm

确保在 include $(CONTIKI)/Makefile.include 行之前执行此操作,而不是之后!

其次,你是为哪个平台编译的? msp430 平台在数学库中没有 pow 函数。它们只有powf 函数对单精度浮点数进行操作,而内置(内在)函数pow 对整数进行操作。

如果要对浮点数进行操作,请将代码更改为:

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

到这里

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

【讨论】:

  • 我正在为天空平台编译程序。
  • 您的解决方案有效。但是仍然存在一些问题,例如内存溢出。以下是说明。
最近更新 更多