简体   繁体   English

在 contiki os 中对clock_gettime 的未定义引用

[英]undefined reference to clock_gettime in contiki os

I need to generate a random number in C and I did it using srand() and rand() functions.我需要在 C 中生成一个随机数,我使用 srand() 和 rand() 函数做到了。 It worked fine when I ran it in the normal compiler, but when I am trying to compile the file in the Contiki OS, it's throwing an 'undefined reference to clock_gettime' error.当我在普通编译器中运行它时它运行良好,但是当我尝试在 Contiki OS 中编译文件时,它抛出了一个“未定义的对clock_gettime的引用”错误。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int a;
srand(time(NULL));
a=rand() % 10000 + 2;
char *str = (char*)malloc(a);
int i=0;
for(i=0; i<a-1;i++){
  str[i]='s';
}
str[i] = '\0';
strcat(str,"Hello %d from the client ");
sprintf(buf,str, seq_id);

这是错误图片

Can someone help me with this?有人可以帮我弄这个吗?

By "normal compiler" I presume you mean the native compiler for your host system?我认为“普通编译器”是指主机系统的本机编译器? In that case the development host is standard hardware with an operating system that provides "wall-clock" time through operating services that ultimately get time from the RTC or NTP.在这种情况下,开发主机是带有操作系统的标准硬件,该操作系统通过最终从 RTC 或 NTP 获取时间的操作服务提供“挂钟”时间。

In embedded systems the standard library is generic and needs to be retargeted to your platform.在嵌入式系统中,标准库是通用的,需要重新定位到您的平台。 Typically I/O, heap management and time services need retargeting.通常 I/O、堆管理和时间服务需要重定向。 In this case you need to define clock_gettime() to resolve the link (or simply redefine an override for time() - as described here ).在这种情况下,你需要定义clock_gettime()来解决链接(或者干脆重新定义超越了time() -描述在这里)。 How you implement it will depend on your specific hardware.您如何实现它取决于您的特定硬件。 Typically you would get time from the RTC, but if your hardware lacks an RTC crystal and a battery backup thereof, it will serve little purpose.通常,您会从 RTC 获得时间,但如果您的硬件缺少 RTC 晶振及其备用电池,那么它就没什么用了。 Time has to come from somewhere, and the library cannot determine where for your specific hardware.时间必须来自某个地方,库无法确定您的特定硬件的位置。

If you actually never need wall-clock time and are simply following the common idiom of using time(NULL) as a random number generator seed, then rather then fully supporting a time() function you don't need;如果您实际上从不需要挂钟时间并且只是遵循使用time(NULL)作为随机数生成器种子的常见习惯用法,那么与其完全支持您不需要的time()函数; consider an alternative means of generating a seed such as that described in this Application Note which uses two independent clock sources (VLO and DCO) to generate a random bit sequence.考虑使用两个独立时钟源(VLO 和 DCO)生成随机位序列的替代方法,例如本应用笔记中所述的生成种子的方法。 The method is time consuming, so you would normally do it once, then use that as a seed for the standard PRNG via srand() .该方法非常耗时,因此您通常会执行一次,然后通过srand()将其用作标准 PRNG 的种子。

There is an implementation of the App Note algorithm at https://github.com/0/msp430-rng/blob/master/rand.c , but it is implemented as an override for the standard rand() .https://github.com/0/msp430-rng/blob/master/rand.c有一个 App Note 算法的实现,但它是作为标准rand()的替代实现的。 I recommend you rename to something like generate_seed() , then call:我建议您重命名为generate_seed() ,然后调用:

srand( generate_seed() ) ;

My suggestion is to use the chip unique values from the TLV memory that is pre-programmed at the factory.我的建议是使用在工厂预编程的 TLV 存储器中的芯片唯一值。 Each chip will contain unique values (such as calibration data), so just hash all TLV memory to arrive at a seed value, then plug that into the rand() function.每个芯片都将包含唯一值(例如校准数据),因此只需散列所有 TLV 内存即可得出种子值,然后将其插入rand()函数中。 If you don't have a hash function handy, then just use any CRC32 calculation over all the bytes.如果您手边没有散列函数,那么只需对所有字节使用任何 CRC32 计算。

See this code for example how to read from the TLV memory.有关如何从 TLV 内存读取的示例,请参见此代码 Keep in mind that every MSP430 family is different, so consult the device datasheet for the exact memory locations.请记住,每个 MSP430 系列都是不同的,因此请查阅器件数据表以了解确切的存储器位置。

If your application initializes the Real Time Clock (RTC) hardware inside every MSP430 chip (and updates it through power cycles) then you could add that to the hash result.如果您的应用程序初始化每个 MSP430 芯片内的实时时钟 (RTC) 硬件(并通过电源循环更新它),那么您可以将其添加到散列结果中。 Just continue the hash (or CRC32) using the RTC date and time registers for your seed value.只需使用您的种子值的 RTC 日期和时间寄存器继续哈希(或 CRC32)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM