在项目中包含libcurl

5

我从curl网站下载了zip压缩文件。我将带有所有头文件的目录复制到我的include目录中。包括curl.h没有问题,但当我尝试调用函数时,我的C++应用程序突然无法编译。

以下是我收到的错误消息:

 [Linker error] undefined reference to
 `curl_easy_init'

这是代码:

这里是代码:

#define CURL_STATICLIB
#include <curl/curl.h>
#include <string>
#include <iostream>
using namespace std;

int main() {
      string url = "http://www.google.com";
      cout << "Retrieving " << url << endl;

      // Our curl objects
      CURL *curl;
      CURLcode result;

      // Create our curl handle  
      curl = curl_easy_init();  

    system("pause");

    return 0;
}

如果我注释掉curl=curl_easy_init()这一行,它就能正常工作。

根据文档,应该可以正常工作,在这里可以看到。

有什么想法吗?


请发布您收到的错误。很可能这是一个链接问题... - ephemient
修改过了...真不敢相信我居然错过了那个。 - Cory Dee
发布您的链接器命令行参数。您可能忘记告诉链接器您需要curl。它无法猜测哪个数百个可用库包含您所需的符号。 - user3458
我没有在命令行中做任何事情,这是在Windows上编译/运行。 - Cory Dee
2
那么你应该在项目选项中有一个添加库的位置。你添加了curl吗? - user3458
2个回答

5

您需要将程序链接到curl库

-L/path/of/curl/lib/libcurl.a (g++)

或者在你的解决方案中添加curl。
Solution->properties->Link(ing) and add libcurl.lib

没有 .a 或 .so 文件,但有 libcurl.imp、.plist、.rc 和 .vcproj。 - Cory Dee
你编译过它吗? ./configure make make install 或者你是用 Visual Studio 构建的吗? - Nadir SOUALEM
不,我原以为只要包含头文件就可以了,然后它就能正常工作了? - Cory Dee

1
..\src\curl-7.71.1\winbuild 目录下,您可以找到 "BUILD.WINDOWS.txt" 文件。在该文件中,他们解释了如何编译该库。
Building with Visual C++, prerequisites
=======================================

   This document describes how to compile, build and install curl and libcurl
   from sources using the Visual C++ build tool. To build with VC++, you will
   of course have to first install VC++. The minimum required version of
   VC is 6 (part of Visual Studio 6). However using a more recent version is
   strongly recommended.

   VC++ is also part of the Windows Platform SDK. You do not have to install
   the full Visual Studio or Visual C++ if all you want is to build curl.

   The latest Platform SDK can be downloaded freely from:

    https://developer.microsoft.com/en-us/windows/downloads/sdk-archive

   If you are building with VC6 then you will also need the February 2003
   Edition of the Platform SDK which can be downloaded from:

    https://www.microsoft.com/en-us/download/details.aspx?id=12261

   If you wish to support zlib, openssl, c-ares, ssh2, you will have to download
   them separately and copy them to the deps directory as shown below:

   somedirectory\
    |_curl-src
    | |_winbuild
    |
    |_deps
      |_ lib
      |_ include
      |_ bin

   It is also possible to create the deps directory in some other random
   places and tell the Makefile its location using the WITH_DEVEL option.

Building straight from git
==========================

 When you check out code git and build it, as opposed from a released source
 code archive, you need to first run the "buildconf.bat" batch file (present
 in the source code root directory) to set things up.

Building with Visual C++
========================

Open a Visual Studio Command prompt:

     Using the 'Developer Command Prompt for VS <version>' menu entry:
       where version is the Visual Studio version. The developer prompt at default
       uses the x86 mode. It is required to call Vcvarsall.bat to setup the prompt
       for the machine type you want, using Vcvarsall.bat.
       This type of command prompt may not exist in all Visual Studio versions.

       For more information, check:
         https://learn.microsoft.com/en-us/dotnet/framework/tools/developer-command-prompt-for-vs
         https://learn.microsoft.com/en-us/cpp/build/how-to-enable-a-64-bit-visual-cpp-toolset-on-the-command-line

     Using the 'VS <version> <platform> <type> Command Prompt' menu entry:
       where version is the Visual Studio version, platform is e.g. x64
       and type Native of Cross platform build.  This type of command prompt
       may not exist in all Visual Studio versions.

       See also:
         https://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx

Once you are in the console, go to the winbuild directory in the Curl
sources:
    cd curl-src\winbuild

Then you can call nmake /f Makefile.vc with the desired options (see below).
The builds will be in the top src directory, builds\ directory, in
a directory named using the options given to the nmake call.

nmake /f Makefile.vc mode=<static or dll> <options>

where <options> is one or many of:
  VC=<6,7,8,9,10,11,12,14,15>    - VC versions
  WITH_DEVEL=<path>              - Paths for the development files (SSL, zlib, etc.)
                                   Defaults to sibbling directory deps: ../deps
                                   Libraries can be fetched at https://windows.php.net/downloads/php-sdk/deps/
                                   Uncompress them into the deps folder.
  WITH_SSL=<dll or static>       - Enable OpenSSL support, DLL or static
  WITH_NGHTTP2=<dll or static>   - Enable HTTP/2 support, DLL or static
  WITH_MBEDTLS=<dll or static>   - Enable mbedTLS support, DLL or static
  WITH_CARES=<dll or static>     - Enable c-ares support, DLL or static
  WITH_ZLIB=<dll or static>      - Enable zlib support, DLL or static
  WITH_SSH2=<dll or static>      - Enable libSSH2 support, DLL or static
  WITH_PREFIX=<dir>              - Where to install the build
  ENABLE_SSPI=<yes or no>        - Enable SSPI support, defaults to yes
  ENABLE_IPV6=<yes or no>        - Enable IPv6, defaults to yes
  ENABLE_IDN=<yes or no>         - Enable use of Windows IDN APIs, defaults to yes
                                   Requires Windows Vista or later
  ENABLE_WINSSL=<yes or no>      - Enable native Windows SSL support, defaults to yes
  GEN_PDB=<yes or no>            - Generate Program Database (debug symbols for release build)
  DEBUG=<yes or no>              - Debug builds
  MACHINE=<x86 or x64>           - Target architecture (default is x86)
  CARES_PATH=<path to cares>     - Custom path for c-ares
  MBEDTLS_PATH=<path to mbedTLS> - Custom path for mbedTLS
  NGHTTP2_PATH=<path to HTTP/2>  - Custom path for nghttp2
  SSH2_PATH=<path to libSSH2>    - Custom path for libSSH2
  SSL_PATH=<path to OpenSSL>     - Custom path for OpenSSL
  ZLIB_PATH=<path to zlib>       - Custom path for zlib


Static linking of Microsoft's C RunTime (CRT):
==============================================
If you are using mode=static nmake will create and link to the static build of
libcurl but *not* the static CRT. If you must you can force nmake to link in
the static CRT by passing RTLIBCFG=static. Typically you shouldn't use that
option, and nmake will default to the DLL CRT. RTLIBCFG is rarely used and
therefore rarely tested. When passing RTLIBCFG for a configuration that was
already built but not with that option, or if the option was specified
differently, you must destroy the build directory containing the configuration
so that nmake can build it from scratch.

Building your own application with a static libcurl
===================================================
When building an application that uses the static libcurl library on Windows,
you must define CURL_STATICLIB. Otherwise the linker will look for dynamic
import symbols.

Legacy Windows and SSL
======================
When you build curl using the build files in this directory the default SSL
backend will be WinSSL (Windows SSPI, more specifically Schannel), the native
SSL library that comes with the Windows OS. WinSSL in Windows <= XP is not able
to connect to servers that no longer support the legacy handshakes and
algorithms used by those versions. If you will be using curl in one of those
earlier versions of Windows you should choose another SSL backend like OpenSSL.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接