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.
#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.