当前位置: 技术问答>linux和unix
【求助】linux 交叉编译 找不到库文件 cannot find -lcurl
来源: 互联网 发布时间:2017-05-22
本文导语: 请教大侠们个问题,我用gcc和arm-linux-gcc编译hello world程序都没有问题,但是编译带有库libcurl的上网程序的时候,gcc没问题,但是用arm-linux-gcc就出错了, 我是在虚拟机中的linux系统中运行的 ----------------------------------...
请教大侠们个问题,我用gcc和arm-linux-gcc编译hello world程序都没有问题,但是编译带有库libcurl的上网程序的时候,gcc没问题,但是用arm-linux-gcc就出错了,
我是在虚拟机中的linux系统中运行的
------------------------------------------------------这是交叉编译
[root@fyfyall curl-2]# make
arm-linux-gcc -DDEBUG -c -o getinmemory.o getinmemory.c
arm-linux-gcc getinmemory.o -o getin -L/usr/lib/libcurl.so -lcurl
/usr/local/arm/4.3.2/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.2/../../../../arm-none-linux-gnueabi/bin/ld: warning: library search path "/usr/lib/libcurl.so" is unsafe for cross-compilation
/usr/local/arm/4.3.2/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.2/../../../../arm-none-linux-gnueabi/bin/ld: skipping incompatible /usr/local/arm/4.3.2/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.2/../../../../arm-none-linux-gnueabi/lib/libcurl.so when searching for -lcurl
/usr/local/arm/4.3.2/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.2/../../../../arm-none-linux-gnueabi/bin/ld: skipping incompatible /usr/local/arm/4.3.2/arm-none-linux-gnueabi/bin/../lib/libcurl.so when searching for -lcurl
/usr/local/arm/4.3.2/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.2/../../../../arm-none-linux-gnueabi/bin/ld: cannot find -lcurl
collect2: ld returned 1 exit status
------------------------------------------------------这是gcc 编译及运行
[root@fyfyall curl-2]# make
gcc -DDEBUG -c -o getinmemory.o getinmemory.c
gcc getinmemory.o -o getin -L/usr/lib/libcurl.so -lcurl
[root@fyfyall curl-2]# ./getin
10437 bytes retrieved
运行也没问题
就是交叉编译就不行了,交叉编译如果就一句hello world 也是没问题的
但是有库就不行了
不知道有库时候要注意什么
------------------------------------------------------
[root@fyfyall curl-2]# ls /usr/lib | grep libcurl*
libcurl.so
libcurl.so.4
libcurl.so.4.1.1
------------------------------------------------------这是源文件
#include
#include
#include
#include
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)n");
return 0;
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
int main(void)
{
CURL *curl_handle;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = (char *)malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.baidu.com/");
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
/* get it! */
res = curl_easy_perform(curl_handle);
/* check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %sn",
curl_easy_strerror(res));
}
else {
/*
* Now, our chunk.memory points to a memory block that is chunk.size
* bytes big and contains the remote file.
*
* Do something nice with it!
*
* You should be aware of the fact that at this point we might have an
* allocated data block, and nothing has yet deallocated that data. So when
* you're done with it, you should free() it as a nice application.
*/
printf("%lu bytes retrievedn", (long)chunk.size);
//printf("%s",chunk.memory);
//printf("百度推广您的产品n");
}
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
if(chunk.memory)
free(chunk.memory);
/* we're done with libcurl, so clean it up */
curl_global_cleanup();
return 0;
}
------------------------------------------------------这是makefile文件
OBJ=getinmemory.o
#CC=arm-linux-gcc
CC=gcc
CFLAGS= -DDEBUG
all: getin
getin: $(OBJ)
$(CC) $^ -o $@ -L/usr/lib/libcurl.so -lcurl
clean:
$(RM) *.o getin
------------------------------------------------------
我是在虚拟机中的linux系统中运行的
------------------------------------------------------这是交叉编译
[root@fyfyall curl-2]# make
arm-linux-gcc -DDEBUG -c -o getinmemory.o getinmemory.c
arm-linux-gcc getinmemory.o -o getin -L/usr/lib/libcurl.so -lcurl
/usr/local/arm/4.3.2/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.2/../../../../arm-none-linux-gnueabi/bin/ld: warning: library search path "/usr/lib/libcurl.so" is unsafe for cross-compilation
/usr/local/arm/4.3.2/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.2/../../../../arm-none-linux-gnueabi/bin/ld: skipping incompatible /usr/local/arm/4.3.2/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.2/../../../../arm-none-linux-gnueabi/lib/libcurl.so when searching for -lcurl
/usr/local/arm/4.3.2/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.2/../../../../arm-none-linux-gnueabi/bin/ld: skipping incompatible /usr/local/arm/4.3.2/arm-none-linux-gnueabi/bin/../lib/libcurl.so when searching for -lcurl
/usr/local/arm/4.3.2/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.2/../../../../arm-none-linux-gnueabi/bin/ld: cannot find -lcurl
collect2: ld returned 1 exit status
------------------------------------------------------这是gcc 编译及运行
[root@fyfyall curl-2]# make
gcc -DDEBUG -c -o getinmemory.o getinmemory.c
gcc getinmemory.o -o getin -L/usr/lib/libcurl.so -lcurl
[root@fyfyall curl-2]# ./getin
10437 bytes retrieved
运行也没问题
就是交叉编译就不行了,交叉编译如果就一句hello world 也是没问题的
但是有库就不行了
不知道有库时候要注意什么
------------------------------------------------------
[root@fyfyall curl-2]# ls /usr/lib | grep libcurl*
libcurl.so
libcurl.so.4
libcurl.so.4.1.1
------------------------------------------------------这是源文件
#include
#include
#include
#include
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)n");
return 0;
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
int main(void)
{
CURL *curl_handle;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = (char *)malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.baidu.com/");
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
/* get it! */
res = curl_easy_perform(curl_handle);
/* check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %sn",
curl_easy_strerror(res));
}
else {
/*
* Now, our chunk.memory points to a memory block that is chunk.size
* bytes big and contains the remote file.
*
* Do something nice with it!
*
* You should be aware of the fact that at this point we might have an
* allocated data block, and nothing has yet deallocated that data. So when
* you're done with it, you should free() it as a nice application.
*/
printf("%lu bytes retrievedn", (long)chunk.size);
//printf("%s",chunk.memory);
//printf("百度推广您的产品n");
}
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
if(chunk.memory)
free(chunk.memory);
/* we're done with libcurl, so clean it up */
curl_global_cleanup();
return 0;
}
------------------------------------------------------这是makefile文件
OBJ=getinmemory.o
#CC=arm-linux-gcc
CC=gcc
CFLAGS= -DDEBUG
all: getin
getin: $(OBJ)
$(CC) $^ -o $@ -L/usr/lib/libcurl.so -lcurl
clean:
$(RM) *.o getin
------------------------------------------------------
|
先用交叉编译环境编译一下libcurl,然后编译你的程序的时候修改一下makefile,注意给链接器的参数-L./path_to_cross_libcurl将-L参数的数值修改为刚才生成的libcurl.so或者libcurl.a的路径。
|
那个库应该用arm-linux-gcc编译出来的动态库吧,你这里用电脑的怎么行呢