链接curl库问题 [英] Linking curl library problems

查看:2944
本文介绍了链接curl库问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立从ftp下载文件的curl库示例程序。我使用 Eclipse IDE,OS - Ubuntu



我已经安装curl使用命令:
apt-get install libcurl4-gnutls-dev



我看到 / usr / include / curl 中的头文件(我不知道c文件在哪里)



看起来Eclipse是快乐与#include< curl / curl.h> ,bu所有在程序中使用的curl函数使用'undefined reference'



编译失败,出现链接错误:

  ****构建配置项目的调试updDown **** 

make all
构建目标:updDown
调用:GCC C链接器
gcc - oupdDown./src/updDown.o
./src/updDown.o:在函数main中:
/home/g/proj/updDown/Debug/../src/updDown .c:45:未定义引用`curl_global_init'
/home/g/proj/updDown/Debug/../src/updDown.c:47:未定义引用`curl_easy_init'
/ home / g / proj / updDown / Debug /../ src / updDown.c:52:未定义的引用`curl_easy_setopt'
/home/g/proj/updDown/Debug/../src/updDown.c:55 :未定义的引用`curl_easy_setopt'
/home/g/proj/updDown/Debug/../src/updDown.c:57:未定义对`curl_easy_setopt'的引用
/ home / g / proj / updDown / Debug /../ src / updDown.c:60:未定义引用`curl_easy_setopt'
/home/g/proj/updDown/Debug/../src/updDown.c:62:未定义引用`curl_easy_perform'
/home/g/proj/updDown/Debug/../src/updDown.c:65:未定义引用`curl_easy_cleanup'
/ home / g / proj / updDown / Debug / ../src/updDown.c:76:未定义的引用`curl_global_cleanup'
collect2:错误:ld返回1退出状态
make:*** [updDown]错误1

**** Build Finished ****

如何解决这个问题?



Hwole代码:

  / * 
===== ================================================== =====================
名称:updDown.c
作者:
版本:
版权:您的版权notice
描述:Hello World in C,Ansi-style
=============================== ===========================================
* /

#include< stdio.h>
#include< stdlib.h>
#include< curl / curl.h>

struct FtpFile {
const char * filename;
FILE * stream;
};

static size_t my_fwrite(void * buffer,size_t size,size_t nmemb,void * stream)
{
struct FtpFile * out =(struct FtpFile *)stream;
if(out&&!out-> stream){
/ *打开写入文件* /
out-> stream = fopen(out-> filename, wb);
if(!out-> stream)
return -1; / *失败,无法打开文件写* /
}
return fwrite(buffer,size,nmemb,out-> stream);
}


int main(void)
{

puts(starting);

CURL * curl;
CURLcode res;
struct FtpFile ftpfile = {
curl.tar.gz,/ *用于存储文件的名称,如果成功* /
NULLs
};

curl_global_init(CURL_GLOBAL_DEFAULT);

curl = curl_easy_init();
if(curl){
/ *
*您最好将URL替换为有效的URL!
* /
curl_easy_setopt(curl,CURLOPT_URL,
ftp://ftp.example.com/pub/www/utilities/curl/curl-7.9.2.tar.gz) ;
/ *定义我们的回调,当有数据要写时调用* /
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,my_fwrite);
/ *设置指向我们的struct的指针传递给回调* /
curl_easy_setopt(curl,CURLOPT_WRITEDATA,& ftpfile);

/ *打开完整的协议/调试输出* /
curl_easy_setopt(curl,CURLOPT_VERBOSE,1L);

res = curl_easy_perform(curl);

/ * always cleanup * /
curl_easy_cleanup(curl);

if(CURLE_OK!= res){
/ *我们失败了* /
fprintf(stderr,curl告诉我们%d \\\
,res);
}
}

if(ftpfile.stream)
fclose(ftpfile.stream); / *关闭本地文件* /

curl_global_cleanup();

return 0;
}


解决方案

-lcurl ,位于 C / C ++ build - >设置 - > GCC C Linker - >


$ b

>让我们更详细一些。


我看到头文件在/ usr / include / curl中(我不知道c文件在哪里)


通常这类软件包不包含源代码,而是提供了预编译的目标文件libraries.bu所使用的所有curl函数in a program with'undefined reference'。


看起来Eclipse很高兴#include




这是真的,因为路径 / usr / include / 通常在include路径文件),所以你不需要为此设置任何东西。


bu程序中使用的所有curl函数都使用未定义的引用。


在代码中,你使用像 curl_global_init 这样的函数,而不是自己实现它们,这意味着这些函数被视为外部函数,进口。你将告诉链接器在哪里找到这些函数(更准确地说,这些符号)。这是通过使用选项 -l <​​/ code>后跟库名称来完成的。要指定库的路径,请使用 -L



有关详细信息, GCC选项摘要中的链接器选项


I'm trying to build curl library sample program that downloads file from ftp. I'm using Eclipse IDE , OS -Ubuntu.

I have installed curl using command: apt-get install libcurl4-gnutls-dev

I see heades files in /usr/include/curl (I don't know where are c files)

Looks Eclipse is happy with #include <curl/curl.h> , bu all curl functions used in program are maked with 'undefined reference'.

Compilation failes with linking errors:

**** Build of configuration Debug for project updDown ****

make all 
Building target: updDown
Invoking: GCC C Linker
gcc  -o "updDown"  ./src/updDown.o   
./src/updDown.o: In function `main':
/home/g/proj/updDown/Debug/../src/updDown.c:45: undefined reference to `curl_global_init'
/home/g/proj/updDown/Debug/../src/updDown.c:47: undefined reference to `curl_easy_init'
/home/g/proj/updDown/Debug/../src/updDown.c:52: undefined reference to `curl_easy_setopt'
/home/g/proj/updDown/Debug/../src/updDown.c:55: undefined reference to `curl_easy_setopt'
/home/g/proj/updDown/Debug/../src/updDown.c:57: undefined reference to `curl_easy_setopt'
/home/g/proj/updDown/Debug/../src/updDown.c:60: undefined reference to `curl_easy_setopt'
/home/g/proj/updDown/Debug/../src/updDown.c:62: undefined reference to `curl_easy_perform'
/home/g/proj/updDown/Debug/../src/updDown.c:65: undefined reference to `curl_easy_cleanup'
/home/g/proj/updDown/Debug/../src/updDown.c:76: undefined reference to `curl_global_cleanup'
collect2: error: ld returned 1 exit status
make: *** [updDown] Error 1

**** Build Finished ****

How to solve this problem?

Hwole code:

/*
 ============================================================================
 Name        : updDown.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>

struct FtpFile {
  const char *filename;
  FILE *stream;
};

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
  struct FtpFile *out=(struct FtpFile *)stream;
  if(out && !out->stream) {
    /* open file for writing */
    out->stream=fopen(out->filename, "wb");
    if(!out->stream)
      return -1; /* failure, can't open file to write */
  }
  return fwrite(buffer, size, nmemb, out->stream);
}


int main(void)
{

    puts("starting");

  CURL *curl;
  CURLcode res;
  struct FtpFile ftpfile={
    "curl.tar.gz", /* name to store the file as if succesful */
    NULLs
  };

  curl_global_init(CURL_GLOBAL_DEFAULT);

  curl = curl_easy_init();
  if(curl) {
    /*
     * You better replace the URL with one that works!
     */
    curl_easy_setopt(curl, CURLOPT_URL,
                     "ftp://ftp.example.com/pub/www/utilities/curl/curl-7.9.2.tar.gz");
    /* Define our callback to get called when there's data to be written */
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
    /* Set a pointer to our struct to pass to the callback */
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);

    /* Switch on full protocol/debug output */
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    res = curl_easy_perform(curl);

    /* always cleanup */
    curl_easy_cleanup(curl);

    if(CURLE_OK != res) {
      /* we failed */
      fprintf(stderr, "curl told us %d\n", res);
    }
  }

  if(ftpfile.stream)
    fclose(ftpfile.stream); /* close the local file */

  curl_global_cleanup();

  return 0;
}

解决方案

You shall specify the linker option of -lcurl, at the location of C/C++ build -> Settings -> GCC C Linker -> Libraries in project properties.

EDITED:

Let's be more detailed.

I see heades files in /usr/include/curl (I don't know where are c files)

Typically such packages don't consist of source codes, instead they provide pre-compiled object files called libraries.bu all curl functions used in program are maked with 'undefined reference'.

Looks Eclipse is happy with #include

That's true since the path /usr/include/ is typically inside the include path (where the compiler tries to find the header files), so you don't need to setup anything for this.

bu all curl functions used in program are maked with 'undefined reference'.

In the code you use functions like curl_global_init without implementing them yourself, which means those functions are treated as external functions that are expected to be imported. You shall "tell" the linker where to find these functions (to be more exact, these symbols). That's done by using the option -l followed by the library name. And for specifying the paths of libraries, use -L.

For further information, you could see the section of linker options in Option Sammary of GCC

这篇关于链接curl库问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆