curl 使用

54 篇文章 0 订阅
50 篇文章 0 订阅
1.编写源文件 curl.c
#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
     CURL *curl; //定义CURL类型的指针
     CURLcode res; //定义CURLcode类型的变量

     if(argc!=2)
     {
         printf("Usage : file <url>;\n");
         exit(1);
     }

     curl = curl_easy_init(); //初始化一个CURL类型的指针
     if(curl!=NULL)
     {
         //设置curl选项. 其中CURLOPT_URL是让用户指定url. argv[1]中存放的命令行传进来的网址
         curl_easy_setopt(curl, CURLOPT_URL, argv[1]);

         //调用curl_easy_perform 执行我们的设置.并进行相关的操作. 在这里只在屏幕上显示出来.
         res = curl_easy_perform(curl);

         //清除curl操作.
         curl_easy_cleanup(curl);
     }
     return 0;
}
 
2.编写makefile
CC = mips-linux-gnu-gcc
CFLAG = -EL
SRC = curl.c
TAG = curl
INC += -I/xxx_sdk/sysroot/usr/include
LIB += -L/xxx_sdk/sysroot/usr/lib -lcurl --sysroot=/xxx_sdk/sysroot/

$(TAG):$(SRC)
    $(CC) $(INC) $(CFLAG) $(SRC) -o $(TAG) $(LIB)
clean:
     rm -rf *.o $(TAG)
    
3.运行
./root/curl/curl www.baidu.com  抓取baidu主页网页代码
./root/curl/curl http://61.135.88.138:33300/jsp/URL  抓取http://61.135.88.138:33300/jsp/URL代码

ps:
Makefile 添加LIB += -L/xxx_sdk/sysroot/usr/lib -lcurl后,必须指定--sysroot=/xxx_sdk/sysroot/,否则提示ld: skipping incompatible /lib/libc.so.6 when searching for /lib/libc.so.6错误.
man gcc 查看 sysroot, 说明如下:
--sysroot=dir
   Use dir as the logical root directory for headers and libraries.  For example, if
   the compiler would normally search for headers in /usr/include and libraries in
   /usr/lib, it will instead search dir/usr/include and dir/usr/lib.
   
   If you use both this option and the -isysroot option, then the --sysroot option
   will apply to libraries, but the -isysroot option will apply to header files.
   
   The GNU linker (beginning with version 2.16) has the necessary support for this
   option.  If your linker does not support this option, the header file aspect of
   --sysroot will still work, but the library aspect will not.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值