【BearPi-HM Nano,玩转鸿蒙“碰一碰”】-5-编译异常疑问①
本帖最后由 sun63312 于 2021-7-31 15:51 编辑<p style="text-align: center; margin-top: 0; margin-bottom: 0;"><span style="font-family: '宋体'; font-size: 18pt; font-weight: bold;">小熊派</span><span style="font-family: '宋体'; font-size: 18pt; font-weight: bold;">HarmonyOS</span><span style="font-family: '宋体'; font-size: 18pt; font-weight: bold;">编译疑问</span><span style="font-family: '宋体'; font-size: 18pt; font-weight: bold;">-</span><span style="font-family: '宋体'; font-size: 18pt; font-weight: bold;">1</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">我想基于D</span><span style="font-family: '宋体';">4_</span><span style="font-family: '宋体';">iot_</span><span style="font-family: '宋体';">tcp</span><span style="font-family: '宋体';">_</span><span style="font-family: '宋体';">sever</span><span style="font-family: '宋体';">这个例程,实现</span><span style="font-family: '宋体';">Webserver</span><span style="font-family: '宋体';">的功能。然后尝试移植一些代码进去,编译时报错找不到我需要调用的函数,情况如下:</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">1、close(</span><span style="font-family: '宋体';">)</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">源码加在while循环最后,编译能够正常通过</span></p>
<p class="imagemiddle" style="text-align: center;"></p>
<p class="imagemiddle" style="text-align: center;"> </p>
<p class="imagemiddle" style="text-align: center;"></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">但是把close(</span><span style="font-family: '宋体';">)</span><span style="font-family: '宋体';">加在while编译就会报错</span><span style="font-family: '宋体';">【</span><span style="font-family: '宋体';">undefined reference to `close'</span><span style="font-family: '宋体';">】</span></p>
<p class="imagemiddle" style="text-align: center;"></p>
<p class="imagemiddle" style="text-align: center;"> </p>
<p class="imagemiddle" style="text-align: center;"></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">2、</span><span style="font-family: '宋体';">其次我还想调用read(</span><span style="font-family: '宋体';">)</span><span style="font-family: '宋体';">和write(</span><span style="font-family: '宋体';">)</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">与clsoe</span><span style="font-family: '宋体';">()</span><span style="font-family: '宋体';">一样都应该包含在下面这个路径</span><span style="font-family: '宋体';">sockets.h</span><span style="font-family: '宋体';">中,但是编译后也是和close(</span><span style="font-family: '宋体';">)</span><span style="font-family: '宋体';">一样报错</span></p>
<p style="margin-left: 40px;"><span style="font-family: '宋体';">【</span><span style="font-family: '宋体';">vendor\hisi\hi3861\hi3861\third_party\lwip_sack\include\lwip\sockets.h</span><span style="font-family: '宋体';">】</span></p>
<p> </p>
<p style="margin-left: 40px;"><strong><span style="font-family: '宋体';">上面两问题,有大佬能解答一下我程序哪里不对?</span><span style="font-family: '宋体';">怎么能调用到</span><span style="font-family: '宋体';">sockets.h</span><span style="font-family: '宋体';"> 下的close、read、write。</span></strong></p>
<p style="margin-left: 40px;"> </p>
<p style="margin-left: 40px;"><strong><span style="font-family: '宋体';">2021-07-31</span></strong></p>
<p style="margin-left: 40px;"><strong><span style="font-family: '宋体';">看Hi3861的lwip开发指南看到这个须知,记录一下</span></strong></p>
<p style="margin-left: 40px;"> </p>
<p>close() 放在 while(1){ ... } 死循环后面,不可能被调用,于是编译器优化代码就给扔掉了。</p>
<p>报错的原因是你项目用的库文件里面不包含 close 这个函数,自然无法链接。</p>
<p>楼上正解啊。</p>
cruelfox 发表于 2021-7-21 09:35
close() 放在 while(1){ ... } 死循环后面,不可能被调用,于是编译器优化代码就给扔掉了。
报错的原因 ...
<p>感谢解答,终于知道为啥close()放在while后面会编译通过了。感谢大佬,现在都换成lwip_close()、lwip_recv()、lwip_write()了。这样也不用改头文件,就能被调用到了<img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/congra.gif" width="48" /></p>
页:
[1]