t113-c-curl使用
先复制文件到要用到的项目中
注意:复制.so文件在linux中不可以直接复制粘贴,必须要在linux命令行输入cp到指定的路径才可以,不然只会复制到软链接
改一下cmake
cmake_minimum_required(VERSION 3.12.4)
SET(CMAKE_C_COMPILER /home/momo/Tina-Linux/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-musl/toolchain/bin/arm-openwrt-linux-muslgnueabi-gcc)
SET(CMAKE_CXX_COMPILER /home/momo/Tina-Linux/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-musl/toolchain/bin/arm-openwrt-linux-muslgnueabi-g++)
project(lcd_lvgl VERSION 1.0)
set(CMAKE_INSTALL_PREFIX /home/momo/Tina-Linux/exsample/youchat/build)
#set(LINK_LIBRARIES "${LINK_LIBRARIES} -lcurl" )
#file(GLOB curlso /home/momo/Tina-Linux/exsample/lcd_lvgl/curl/ *.so)
#包含lvgl的子库
include_directories(/home/momo/Tina-Linux/exsample/youchat/curl)
#创建exe的可执行程序
add_executable(youchat you.c)
#链接所需要的库
set(LINK_DIR /home/momo/Tina-Linux/exsample/youchat/curl)
add_library(curll SHARED IMPORTED)
set_target_properties(
curll
PROPERTIES IMPORTED_LOCATION
${LINK_DIR}/libcurl.so
)
#link_directories(${LINK_DIR})
target_link_libraries(youchat curll)
这样的写法是这篇文章写的,导入第三方库的教程
https://blog.csdn.net/lftaoyuan/article/details/107057699
cmake_minimum_required(VERSION 3.12.4)
SET(CMAKE_C_COMPILER /home/momo/Tina-Linux/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-musl/toolchain/bin/arm-openwrt-linux-muslgnueabi-gcc)
SET(CMAKE_CXX_COMPILER /home/momo/Tina-Linux/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-musl/toolchain/bin/arm-openwrt-linux-muslgnueabi-g++)
project(lcd_lvgl VERSION 1.0)
set(CMAKE_INSTALL_PREFIX /home/momo/Tina-Linux/exsample/youchat/build)
#set(LINK_LIBRARIES "${LINK_LIBRARIES} -lcurl" )
#file(GLOB curlso /home/momo/Tina-Linux/exsample/lcd_lvgl/curl/ *.so)
#包含lvgl的子库
include_directories(/home/momo/Tina-Linux/exsample/youchat/curl)
#创建exe的可执行程序
add_executable(youchat you.c)
#链接所需要的库
set(LINK_DIR /home/momo/Tina-Linux/exsample/youchat/curl)
#add_library(curll SHARED IMPORTED)
#set_target_properties(
# curll
# PROPERTIES IMPORTED_LOCATION
# ${LINK_DIR}/libcurl.so
#)
link_directories(${LINK_DIR})
target_link_libraries(youchat libcurl.so)
而这种写法则是在这篇文章学的,但是并没有编译成功,不知道为什么,我觉得应该是cmake没有对库进行解码缓存什么的,那link_directories又是干啥的,这个是在自己做的库中用的但是既然都有缓存了为什么还要这个函数
https://www.jianshu.com/p/354c64b0e26b
最后找到了原因:
cmake_minimum_required(VERSION 3.12.4)
SET(CMAKE_C_COMPILER /home/momo/Tina-Linux/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-musl/toolchain/bin/arm-openwrt-linux-muslgnueabi-gcc)
SET(CMAKE_CXX_COMPILER /home/momo/Tina-Linux/prebuilt/gcc/linux-x86/arm/toolchain-sunxi-musl/toolchain/bin/arm-openwrt-linux-muslgnueabi-g++)
SET(LINK_DIR /home/momo/Tina-Linux/exsample/youchat/curl)
project(lcd_lvgl VERSION 1.0)
set(CMAKE_INSTALL_PREFIX /home/momo/Tina-Linux/exsample/youchat/build)
#set(LINK_LIBRARIES "${LINK_LIBRARIES} -lcurl" )
#file(GLOB curlso /home/momo/Tina-Linux/exsample/lcd_lvgl/curl/ *.so)
#包含lvgl的子库
link_directories(${LINK_DIR})
include_directories(/home/momo/Tina-Linux/exsample/youchat/curl)
#创建exe的可执行程序
add_executable(youchat you.c)
#链接所需要的库
#add_library(curll SHARED IMPORTED)
#set_target_properties(
# curll
# PROPERTIES IMPORTED_LOCATION
# ${LINK_DIR}/libcurl.so
#)
target_link_libraries(youchat PUBLIC curl)
不知道为什么库文件路径设置必须要在生成可执行文件之前,卡了我半天
那么就可以直接使用了
编程
流程如下:
调用curl_global_init()初始化libcurl,(就像初始化套接字)
调用curl_easy_init()函数得到 easy interface型指针(这个指针用来各种配置)
调用curl_easy_setopt()设置传输选项(通过调用curl_easy_setopt这个函数来对指针进行各种配置,比如:请求的方式)
根据curl_easy_setopt()设置的传输选项,实现回调函数以完成用户特定任务
调用curl_easy_perform()函数进行访问请求
调用curl_easy_cleanup()释放内存
需要注意的的是
那么我们直接上手
写一个简单的流程,这个流程未指定输出的时候好像是默认输出到tty的那个上面去的也就是端口,
这个网站我找了半天好不容易才找到个能用的api网站,就不展示出来了要不然又不能用了
结果
如何链接库文件?
https://blog.csdn.net/KYJL888/article/details/85109782
如何转换所得的数据成string
添加funtions之后perferom就行了
https://qastack.cn/programming/2329571/c-libcurl-get-output-into-a-string
使用curl_easy_perform后卡死
我的情况有点特殊,是成功后卡死的,很奇怪,网上找到说是速度的问题,所以设置了速度;
https://blog.csdn.net/xiaoyilong2007101095/article/details/53256966
仍然不行
后面又设置了timeout等都不行,这个好像不关curl的事情但是确实是关掉perform就不见卡死的情况
再不行试一下mutiperfom
找到原因了,之前我一直用的是信号捕获的函数来作为定时器给lvgl计时的,但是这个方法是有风险的,因为这个定时器貌似只能定一个那么如果其他的函数用来结束后关掉就用不了了,当然curl就是这么做的。所以如果想用curl那就必须用tickless模式
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 2025成都.NET开发者Connect圆满结束
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析