简体   繁体   English

在C ++应用程序中无法构建/编译libcurl

[英]Trouble building/compiling libcurl in C++ Application

I am new to C++ and am interested in writing a simple app to download an HTTP file from the internet and process the contents of it). 我是C ++的新手,有兴趣编写一个简单的应用程序以从Internet下载HTTP文件并处理其内容)。 Anyway, I found a library called libcurl online. 无论如何,我在线上找到了一个名为libcurl的库。 I went to the following website: 我去了以下网站:

http://curl.haxx.se/download.html http://curl.haxx.se/download.html

  1. Should I download the source archive or the binary package (Win64 MinGW64)? 我应该下载源归档文件还是二进制软件包(Win64 MinGW64)? What is the difference and pros/cons between using source archive vs binary pkg? 使用源归档和二进制pkg有什么区别和优点/缺点?
  2. Should I use the curlcpp binding since I am writing a C++ app? 自编写C ++应用以来,是否应该使用curlcpp绑定? How can I incorporate the curlcpp binding with this? 如何将curlcpp绑定与此合并?

Anyway, I downloaded curl-7.34.0.zip (Source Archive) and inside, it contains many folders. 无论如何,我下载了curl-7.34.0.zip(源档案),并且在里面,它包含许多文件夹。 I suspect that the folders that I would need to include are: 我怀疑我需要包含的文件夹是:

  • include 包括
  • lib LIB
  • src SRC

I used CodeBlocks IDE. 我使用了CodeBlocks IDE。 I "recursively added" the folder "include/curl" into my project. 我“递归地”将文件夹“ include / curl”添加到我的项目中。 It looks like this: 看起来像这样:

在此处输入图片说明

My main.cpp contains the following code (taken from the sample app on the website): 我的main.cpp包含以下代码(取自网站上的示例应用程序):

#include <iostream>
#include <stdio.h>
#include "curl/curl.h"
using namespace std;

int main()
{
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
        /* example.com is redirected, so we tell libcurl to follow redirection */
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);

        /* Check for errors */
        if(res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

        /* always cleanup */
        curl_easy_cleanup(curl);

    }
    return 0;
}

I received the following error message when trying to build/compile. 尝试构建/编译时,我收到以下错误消息。

undefined reference to '_imp__curl_easy_init'
undefined reference to '_imp__curl_easy_setopt'
undefined reference to '_imp__curl_easy_perform'
undefined reference to '_imp__curl_easy_strerror'
undefined reference to '_imp__curl_easy_cleanup'

I am not sure what I am missing or if my folder directory is being used incorrectly. 我不确定丢失了什么或者我的文件夹目录使用不正确。 Do you have any suggestion to help me build this via Source Archive or Binary Package? 您是否有任何建议可以帮助我通过Source Archive或Binary Package建立此文件? Thanks. 谢谢。

There are several problems here: 这里有几个问题:

  1. Never. 决不。 ABSOLUTELY never add headers from library directly to your project. 绝对不要将库头直接添加到您的项目中。 Copy them to your include catalog. 将它们复制到您的包含目录。
  2. #include <curl.h> : you use <> which means - search in include catalog. #include <curl.h> :您使用<>表示-在包含目录中搜索。 Try replacing it with: #include "curl.h" - which means search through project and include catalog. 尝试将其替换为: #include "curl.h" -表示搜索项目并包含目录。

But you should do nr. 但是你应该这样做。 1 -as this code is not part of your project. 1-因为此代码不属于您的项目。 It is part of curl project. 它是curl项目的一部分。

EDIT As posted in comment - it depends where you actually put the headers. 编辑如评论中所述-它取决于您实际放置标题的位置。 If you put them in include\\curl directory, then use #include "curl/curl.h" . 如果将它们放在include\\curl目录中,请使用#include "curl/curl.h"

EDIT AFTER QUESTION EDIT You need to specify .lib references. 问题编辑后,您需要指定.lib引用。 When you use curl you don't compile it. 使用curl时,您不会对其进行编译。 It is already compiled by some genius guys from curl to the form of .dll s. 一些天才的家伙已经将它编译为从curl到.dll的形式。 Your program doesn't know how to use them unless you tell it how to. 除非您告诉程序如何使用,否则您的程序不知道如何使用它们。 To do that you must change your linker options - by providing so called lib files. 为此,您必须更改链接器选项-通过提供所谓的lib文件。 lib files are there to tell linker which .dll s it should use. lib文件可以告诉链接器应使用哪个.dll

I assume you have your curl catalog. 我假设您有卷曲目录。 Apart from include there should also be lib . 除了include之外, include应该有lib Now what you need to do is to tell linker that you need libcurl.a from lib catalog of curl. 现在您需要做的就是告诉链接器您需要curl的lib目录中的libcurl.a Go ahead: Project build options | 继续: 项目构建选项| Linker settings . 链接器设置 In Link libraries section specify the path to libcurl.a . 在“ Link libraries部分中,指定libcurl.a的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM