undefined reference to `timersub‘ 错误处理

将paho mqtt C移植到IMX6上去,编译时报错

Scanning dependencies of target test95
[ 51%] Building C object test/CMakeFiles/test95.dir/test95.c.o
/home/gateway/Eden/paho.mqtt.c/test/test95.c: In function ‘MyLog’:
/home/gateway/Eden/paho.mqtt.c/test/test95.c:124:2: warning: implicit declaration of function ‘localtime_r’; did you mean ‘localtime’? [-Wimplicit-function-declaration]
  localtime_r(&ts.tv_sec, &timeinfo);
  ^~~~~~~~~~~
  localtime
/home/gateway/Eden/paho.mqtt.c/test/test95.c: In function ‘MySleep’:
/home/gateway/Eden/paho.mqtt.c/test/test95.c:147:2: warning: implicit declaration of function ‘usleep’; did you mean ‘sleep’? [-Wimplicit-function-declaration]
  usleep(milliseconds*1000);
  ^~~~~~
  sleep
/home/gateway/Eden/paho.mqtt.c/test/test95.c: In function ‘elapsed’:
/home/gateway/Eden/paho.mqtt.c/test/test95.c:198:2: warning: implicit declaration of function ‘timersub’; did you mean ‘utimes’? [-Wimplicit-function-declaration]
  timersub(&now, &start_time, &res);
  ^~~~~~~~
  utimes
[ 53%] Linking C executable test95
CMakeFiles/test95.dir/test95.c.o: In function `elapsed':
test95.c:(.text+0x3e4): undefined reference to `timersub'
collect2: error: ld returned 1 exit status
test/CMakeFiles/test95.dir/build.make:95: recipe for target 'test/test95' failed
make[2]: *** [test/test95] Error 1
CMakeFiles/Makefile2:352: recipe for target 'test/CMakeFiles/test95.dir/all' failed
make[1]: *** [test/CMakeFiles/test95.dir/all] Error 2
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2

原因是我写的cmake配置文件(CMAKE_TOOLCHAIN_FILE)中

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -mfloat-abi=hard ....")

有-std=c99,这个是之前其他项目中保留的,将-std=c99删除,再cmake生成MAKEFILE,然后再make,编译成功


 

undefined reference to错误通常是在编译过程中出现的错误,它表示在链接阶段找不到对应的函数或变量的定义。这种错误通常发生在使用外部库或模块时,编译器无法找到对应的函数或变量的定义。 解决这个错误的方法有以下几种: 1. 确保正确链接库文件:检查你的代码中是否正确引入了需要的库文件,并且在编译时是否链接了这些库文件。可以通过在编译命令中添加对应的库文件路径和库文件名来解决这个问题。 2. 检查函数或变量的定义:确保你的代码中有对应函数或变量的定义。如果是使用外部库或模块,需要确保你的代码中包含了对应的头文件,并且在链接时能够找到对应的库文件。 3. 检查函数或变量的命名:有时候undefined reference to错误是由于函数或变量的命名错误导致的。检查你的代码中函数或变量的命名是否正确,包括大小写和拼写。 4. 检查编译选项:有时候undefined reference to错误是由于编译选项设置不正确导致的。检查你的编译命令中是否包含了正确的编译选项,例如是否开启了对应的优化选项或调试选项。 5. 检查库文件版本:有时候undefined reference to错误是由于库文件版本不兼容导致的。检查你的代码中使用的库文件版本是否与编译环境兼容,如果不兼容,可以尝试更新库文件或使用兼容的版本。 6. 检查编译器版本:有时候undefined reference to错误是由于编译器版本不兼容导致的。检查你的编译器版本是否与代码中使用的语法和特性兼容,如果不兼容,可以尝试更新编译器或修改代码以适应当前的编译器版本。 以上是解决undefined reference to错误的一些常见方法,希望对你有帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值