C++ 使 LibCurl 与 Visual Studio 2013 一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20171165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:56:54  来源:igfitidea点击:

Getting LibCurl to work with Visual Studio 2013

c++visual-c++dlllibcurl

提问by Quaxton Hale

I am having trouble getting LibCurl to work with Visual Studio 2013. I downloaded the current version (curl-7.33.0) and tried following the instructions I found on this site: Using LibCurl with Visual 2010

我在让 LibCurl 与 Visual Studio 2013 一起工作时遇到问题。我下载了当前版本 (curl-7.33.0) 并尝试按照我在本网站上找到的说明进行操作:将 LibCurl 与 Visual 2010 一起使用

But I can't find curllib.libin the folder I downloaded. And I am still getting errors: enter image description here

但是我在下载的文件夹中找不到curllib.lib。我仍然收到错误: 在此处输入图片说明

After searching the internet for more help. I now get these error messages. There appears to be a problem with linking to libcurl.lib?

在互联网上搜索更多帮助后。我现在收到这些错误消息。链接到 libcurl.lib 似乎有问题?

enter image description here

在此处输入图片说明

This is what I have configured: enter image description here

这是我配置的: 在此处输入图片说明



enter image description here

在此处输入图片说明

Inside /lib I have libcurl.liband libcurl.dll

在 /lib 我有libcurl.liblibcurl.dll



UPDATE

更新

I downloaded this release for Win32 MSVC: http://curl.haxx.se/download.html#Win32After adding the libcurl libraries and successfully compiling, I am now getting this error message:

我为 Win32 MSVC 下载了此版本:http: //curl.haxx.se/download.html#Win32添加 libcurl 库并成功编译后,我现在收到此错误消息:

 The application was unable to start correctly (0xc000007b). Click OK to close the application.

Here is the sample code I am trying to run:

这是我尝试运行的示例代码:

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


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

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
        res = curl_easy_perform(curl);

        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    return 0;
}


FINAL UPDATE

最后更新

I believe I have gotten LibCurl to work with Visual Studio 2013 now. Persistence ftw! Although, after spending hours trying to solve these error messages, I am a little hesitant at saying everything is working fine now. That is why I am putting a bounty on this question to get clear and conciseinstructions on getting LibCurl to work with Visual Studio 2013.

我相信我现在已经让 LibCurl 与 Visual Studio 2013 一起工作了。坚持ftw!虽然,在花了几个小时试图解决这些错误消息之后,我还是有点犹豫,现在一切正常。这就是为什么我在这个问题上悬赏以获取有关让 LibCurl 与 Visual Studio 2013 一起使用的清晰简明的说明。

This is what I did to get it to work:

这是我为使其工作所做的工作:

  1. First, download the Win32 MSVC package here: http://curl.haxx.se/download.html#Win32For these instructions sake, let's say you downloaded to C:\LibCurl

  2. Start a new project in Visual Studio. Go to Project|Project Properties|VC++ Directories|Include Directories| Add the path to the include directory inside the downloaded package. (C:\LibCurl\include)

  3. Next, go to Project|Project Properties|Linker|General|Additional Library Directories| Add the path to the lib directory. (Where curllib.dll is located)

  4. Then, go to Project|Project Properties|Linker|Input|Additional Dependencies| And add curllib.lib

  5. Now if you compile a test program, you will likely get the message saying libsasl.dll is missing. You will need to download this file and put it in the same directory as your build. I used 7-Zip to extract libsasl.dllfrom OpenLDAP for Windows. OpenLDAP for Windows

  1. 首先,在这里下载 Win32 MSVC 包:http://curl.haxx.se/download.html#Win32 为了这些说明,假设你下载到 C:\LibCurl

  2. 在 Visual Studio 中启动一个新项目。转到项目|项目属性|VC++目录|包含目录| 将路径添加到下载的包内的包含目录中。(C:\LibCurl\include)

  3. 接下来,转到项目|项目属性|链接器|常规|附加库目录| 添加lib目录的路径。(curllib.dll 所在的位置)

  4. 然后,转到项目|项目属性|链接器|输入|附加依赖项| 并添加curllib.lib

  5. 现在,如果您编译测试程序,您可能会收到消息,指出缺少 libsasl.dll。您需要下载此文件并将其放在与构建相同的目录中。我使用 7-Zip从OpenLDAP for Windows 中提取libsasl.dll适用于 Windows 的 OpenLDAP

This is the result of my test code from above: enter image description here

这是我上面的测试代码的结果: 在此处输入图片说明

采纳答案by Micha?

I would say that in a comment, but I am lacking in points. You don't have to copy any .dll into your program run catalog. Go to Project | Properties | Configuration Propertiesand in line Envrionmentwrite: PATH=$(ExecutablePath)$(LocalDebuggerEnvironment).

我会在评论中这么说,但我缺乏要点。您不必将任何 .dll 复制到您的程序运行目录中。转到项目 | 属性 | 配置属性和行管环境写:PATH=$(ExecutablePath)$(LocalDebuggerEnvironment)

From now on, all .dlls from any catalog you mention in Project|Project Properties|VC++ Directories|Binaryshould be usable without copying them.

从现在开始,您在Project|Project Properties|VC++ Directory|Binary 中提到的任何目录中的所有 .dll 都应该可以使用而无需复制它们。

The rest is exactly as you written.

其余的和你写的完全一样。

回答by mtlynch

A lot of these instructions are out of date because they recommend the win32-ssl-devel-msvcpackage for curl, which no longer exists.

很多这些说明已经过时了,因为它们推荐用于 curl的win32-ssl-devel-msvc包,不再存在

The following instructions allow you to build libcurl using only:

以下说明允许您使用构建 libcurl :

  • Visual Studio 2013
  • curl generic source tarball (tested on curl 7.44.0).
  • 视觉工作室 2013
  • curl 通用源 tarball(在 curl 7.44.0 上测试)。

A. Build libcurl static library

A.构建libcurl静态库

  1. Download the latest curl generic sourcefrom: http://curl.haxx.se/latest.cgi?curl=tar.gz
  2. Extract the source to a local directory (we'll be using C:\libcurl)
  3. Open a command prompt
  4. "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\vcvars32.bat"To initialize your VC environment variables (adjust your VS 2013 installation directory as needed)
  5. cd C:\libcurl\winbuild
  6. nmake /f Makefile.vc mode=static VC=12
  7. The build should appear in C:\libcurl\builds\libcurl-vc12-x86-release-static-ipv6-sspi-winssl
  1. 从以下位置下载最新的 curl通用源http: //curl.haxx.se/latest.cgi?curl = tar.gz
  2. 将源提取到本地目录(我们将使用C:\libcurl
  3. 打开命令提示符
  4. "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\vcvars32.bat"初始化你的 VC 环境变量(根据需要调整你的 VS 2013 安装目录)
  5. cd C:\libcurl\winbuild
  6. nmake /f Makefile.vc mode=static VC=12
  7. 构建应该出现在 C:\libcurl\builds\libcurl-vc12-x86-release-static-ipv6-sspi-winssl

B. Link Against libcurl in Visual Studio

B. 在 Visual Studio 中链接 libcurl

  1. In Visual Studio, right click your project in Solution Explorer, then click "Properties"
  2. Configuration Properties > C/C++ > General > Additional Include Directories: add C:\libcurl\builds\libcurl-vc12-x86-release-static-ipv6-sspi-winssl\include
  3. Configuration Properties > C/C++ > Preprocessor > Preprocessor Definitions: add CURL_STATICLIB
  4. Configuration Properties > Linker > General > Additional Library Directories: add C:\libcurl\builds\libcurl-vc12-x86-release-static-ipv6-sspi-winssl\lib
  5. Configuration Properties > Linker > Input > Additional Dependencies: add libcurl_a.lib
  1. 在 Visual Studio 中,在解决方案资源管理器中右键单击您的项目,然后单击“属性”
  2. 配置属性 > C/C++ > 常规 > 附加包含目录:添加 C:\libcurl\builds\libcurl-vc12-x86-release-static-ipv6-sspi-winssl\include
  3. 配置属性 > C/C++ > 预处理器 > 预处理器定义:添加 CURL_STATICLIB
  4. 配置属性 > 链接器 > 常规 > 附加库目录:添加 C:\libcurl\builds\libcurl-vc12-x86-release-static-ipv6-sspi-winssl\lib
  5. 配置属性 > 链接器 > 输入 > 附加依赖项:添加 libcurl_a.lib

C. Call libcurl from Your Project

C. 从你的项目中调用 libcurl

The following sample shows a call to libcurl:

以下示例显示了对 libcurl 的调用:

#include "stdafx.h"

#include <curl/curl.h>

void main(int argc, char* argv[])
{
    CURL *curl = curl_easy_init();
    if (curl) printf("curl_easy_init() succeeded!\n"); 
    else fprintf(stderr, "Error calling curl_easy_init().\n");
}

回答by John Bandela

The easiest way to do this that I found is first make sure that nuget is installed.

我发现最简单的方法是首先确保安装了 nuget。

http://www.nuget.org/

http://www.nuget.org/

Then create your project.

然后创建您的项目。

Then go to http://www.nuget.org/packages/curl/and follow the instructions which is to go the package manager console and type PM> Install-Package curl

然后转到http://www.nuget.org/packages/curl/并按照说明转到包管理器控制台并键入PM> Install-Package curl

If you then look for the packages directory in your project directory, you will find the include files and the library files. Note that there is a version for Visual Studio 110, not 120, but because libcurl is a C library you can use it with Visual Studio 2013. Make sure the include directory and lib directory are specified under the Visual C++ directories in project properties.

如果您随后在项目目录中查找包目录,您将找到包含文件和库文件。请注意,有一个适用于 Visual Studio 110 的版本,而不是 120,但由于 libcurl 是一个 C 库,您可以将它与 Visual Studio 2013 一起使用。确保在项目属性的 Visual C++ 目录下指定了包含目录和 lib 目录。

Make sure you have the following files as addition input to the linker libcurl.lib;libeay32.lib;ssleay32.lib;Ws2_32.lib;libssh2.lib;zlib.lib;wldap32.lib;

确保您有以下文件作为链接器的附加输入 libcurl.lib;libeay32.lib;ssleay32.lib;Ws2_32.lib;libssh2.lib;zlib.lib;wldap32.lib;

回答by Sergei Nikulov

Another way to use curl/libcurl is build with CMakev2.8.12+ (assuming that git is already installed on your computer)

使用 curl/libcurl 的另一种方法是使用CMakev2.8.12+构建(假设您的计算机上已经安装了 git)

Open cmd window and change dir to appropriate folder

打开cmd窗口并将dir更改为适当的文件夹

git clone https://github.com/bagder/curl.git
mkdir msbuild
cd msbuild
cmake ..\curl -G"Visual Studio 12 Win64" -DCMAKE_INSTALL_PREFIX=C:\curl.vc12 -DCURL_STATICLIB=ON
< ... lots of output here ... >

Open generated CURL.sln in Visual studio and build it.

在 Visual Studio 中打开生成的 CURL.sln 并构建它。

CMake options I use in example

我在示例中使用的 CMake 选项

-G selects build generator. In our case Visual Studio 2013 64 bit target

-DCMAKE_INSTALL_PREFIX - provides root folder where targets should be installed

-DCURL_STATICLIB=ON - generates build for static library

-G 选择构建生成器。在我们的例子中,Visual Studio 2013 64 位目标

-DCMAKE_INSTALL_PREFIX - 提供应安装目标的根文件夹

-DCURL_STATICLIB=ON - 为静态库生成构建

After building install target, your will find bin/include/lib folders in C:\curl.vc12

构建安装目标后,您将在 C:\curl.vc12 中找到 bin/include/lib 文件夹

Provide those path to your solution and build your code with curl lib.

为您的解决方案提供这些路径并使用 curl lib 构建您的代码。

回答by Nil

I tried to do it from scratch with VS2012 (I don't have 2013) and it works perfectly.

我试图用 VS2012(我没有 2013)从头开始做,它工作得很好。

So, I'm not sure what your problem is, but:

所以,我不确定你的问题是什么,但是:

  • Make sure you download the right archive.
  • Try to put the cURL folder on a path without space.
  • If you know someone who use VS2012 or older, try your code with the same include and lib and see if it works.
  • Paste a minimal working example of your code so I can test it.
  • 确保下载正确的存档。
  • 尝试将 cURL 文件夹放在没有空格的路径上。
  • 如果您认识使用 VS2012 或更早版本的人,请使用相同的包含和库尝试您的代码,看看它是否有效。
  • 粘贴您的代码的最小工作示例,以便我可以对其进行测试。

回答by David

This is a bit late, but for those who still have problems, this method worked best for me:

这有点晚了,但对于那些仍然有问题的人来说,这种方法最适合我:

  1. Add VS to the system PATH:
    For example: C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin.
  2. Download current curl-X.zip from http://curl.haxx.se/download.htmland unzip.
  3. Open command line at curl-X/winbuild.
  4. Call vcvars32.bat.
  5. Call nmake /f Makefile.vc mode=static VC=12.
  6. Goto curl-X/builds/libcurl-XXX.
  1. 将VS添加到系统PATH:
    例如:C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin.
  2. 从下载当前 curl-X.ziphttp://curl.haxx.se/download.html并解压缩。
  3. 在 处打开命令行curl-X/winbuild
  4. 打电话vcvars32.bat
  5. 打电话nmake /f Makefile.vc mode=static VC=12
  6. 转到curl-X/builds/libcurl-XXX

There you find the includes and a libcurl_a.lib. This lib works fine for me.
Remember to define -DCURL_STATICLIBwhen you compile your code with this lib.

在那里你可以找到包含和一个libcurl_a.lib. 这个库对我来说很好用。
请记住在-DCURL_STATICLIB使用此库编译代码时进行定义。

回答by kovac

For Visual Studio 2017, the steps in linkworked for me. In case the link expires or specifically for those who download the libcurl zip file instead of cloning from GitHub, I will note down the steps here.

对于 Visual Studio 2017,链接中的步骤对我有用。如果链接过期或专门针对那些下载 libcurl zip 文件而不是从 GitHub 克隆的人,我会在此处记下这些步骤。

  1. Set environment variables with “C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat” x64. If the command is successful, you will see a message that says Environment initialized for 'x64'

  2. Download and extract the compressed libcurl file from download libcurl. I used the .zipfile.

  3. cdinto winbuilddirectory inside the extracted libcurl folder.
  4. Run nmake /f Makefile.vc mode=dll MACHINE=x64to build. For more information on build options, please refer to BUILD.WINDOWStext file in winbuildfolder.
  5. Go up one directory level and cdinto buildsfolder to find the compiled files.
  1. 使用“C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat” x64. 如果命令成功,您将看到一条消息,指出Environment initialized for 'x64'

  2. 下载 libcurl下载并解压压缩的 libcurl 文件。我使用了该.zip文件。

  3. cd进入winbuild解压缩的 libcurl 文件夹内的目录。
  4. 运行nmake /f Makefile.vc mode=dll MACHINE=x64构建。有关构建选项的更多信息,请参阅BUILD.WINDOWS文件winbuild夹中的文本文件。
  5. 上一级目录并cd进入builds文件夹以查找已编译的文件。

All the best!

祝一切顺利!

回答by Daisy

I found an easy way to get it work in VC++ using the latest package. I basically followed the steps in Using libcurl in Visual Studio. The libcurl and VC++ are very old in the instruction.

我找到了一种使用最新包在 VC++ 中使其工作的简单方法。我基本上遵循在 Visual Studio 中使用 libcurl 中的步骤。指令中的 libcurl 和 VC++ 已经很老了。

First download the ZIP file on download page https://curl.haxx.se/download.htmlThe ZIP package is https://curl.haxx.se/download/curl-7.50.1.zip

首先在下载页面https://curl.haxx.se/download.html下载 ZIP 文件ZIP 包是https://curl.haxx.se/download/curl-7.50.1.zip

Go to projects-> Windows\VC10 (or your version of VC)\lib\libcurl.sln, open the project in VC++.

进入projects->Windows\VC10(或者你的VC版本)\lib\libcurl.sln,用VC++打开项目。

Build the project in DLL Release. DLL debug doesn't work on my VC++.

在 DLL Release 中构建项目。DLL 调试不适用于我的 VC++。

Go to build\Win32\VC10\DLL Release, you can find the lib and dll files generated from previous step.

进入build\Win32\VC10\DLL Release,可以找到上一步生成的lib和dll文件。

Create a folder new, with include and lib folders. Copy the libcurb.dll and libcurb.lib whatever is in the DLL Release folder to the new\lib. Copy everything in curl-7.50.1\include to new\include folder.

新建一个包含 include 和 lib 文件夹的文件夹。将 DLL Release 文件夹中的 libcurb.dll 和 libcurb.lib 复制到 new\lib。将 curl-7.50.1\include 中的所有内容复制到 new\include 文件夹。

C++ Properties -> Configuration Properties -> VC++ Directories, add new\include to Include Directories, new\lib to Library Directories; add new\lib to Linker -> General -> Additional Library Directories, add libcurl.lib to Linker -> Input -> Additional Dependencies

C++ Properties -> Configuration Properties -> VC++ Directories,在Include Directories中添加new\include,在Library Directories中添加new\lib;将 new\lib 添加到 Linker -> General -> Additional Library Directories,将 libcurl.lib 添加到 Linker -> Input -> Additional Dependencies

It seems that I have to put the dll file under the same folder with executable file.

看来我必须将dll文件与可执行文件放在同一文件夹下。

It should work.

它应该工作。

回答by evoskuil

The problem is that the targets for the default VS2013 platform tools are not set in the NuGet packages. This is why it works in VS2012 but not VS2013. I manually created replacement targets files. Instructions and download:

问题是默认 VS2013 平台工具目标未在 NuGet 包中设置。这就是为什么它适用于 VS2012 而不适用于 VS2013。我手动创建了替换目标文件。使用说明和下载:

https://github.com/evoskuil/curl-nuget-targets

https://github.com/evoskuil/curl-nuget-targets

回答by Steve Holme

Download the curl v7.37.0 source code and use the Visual Studio project files provided.

下载 curl v7.37.0 源代码并使用提供的 Visual Studio 项目文件。

I've spent the last few weeks polishing my own personal project files, that were based off the original VC6 files, and adding them to the repository.

过去几周我一直在完善我自己的个人项目文件,这些文件基于原始 VC6 文件,并将它们添加到存储库中。

.dsw / .dsp (VC6), .sln / .vcproj (VC7, VC7.1, VC8 and VC9 as well as .sln / .vcxproj (VC10, VC11 and VC12) files are provided for both DLL and Static Library builds with support for OpenSSL and Windows SSPI / SChannel in both Win32 and x64 configurations.

.dsw / .dsp (VC6)、.sln / .vcproj(VC7、VC7.1、VC8 和 VC9 以及 .sln / .vcxproj(VC10、VC11 和 VC12)文件提供给 DLL 和静态库构建在 Win32 和 x64 配置中支持 OpenSSL 和 Windows SSPI/SChannel。