如何在C ++应用程序中使用curl Nuget包 [英] How to use curl Nuget package in C++ application

查看:431
本文介绍了如何在C ++应用程序中使用curl Nuget包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Visual Studio 2017开发的C ++应用程序中使用curl.我喜欢使用Nuget的想法,因为它是实现库的一种非常干净的方法.我尝试遵循Microsoft论坛中的以下示例,该示例导致我使用"rmt_curl"包(在下面链接).但是,这使VS无法找到"curl.h".

I am trying to use curl in a C++ application I am developing using Visual Studio 2017. I like the idea of using Nuget because it is a very clean way of implementing a library. I tried to follow the example below from the Microsoft forum, which led me to use the "rmt_curl" package (linked below). This, however, left VS unable to find "curl.h".

https://social.msdn.microsoft.com/Forums/sqlserver/zh-CN/a5cf5caa-450e-425b-af5b -84f8e1c198f9/c-visual-studio-2015-如何在我的项目中包含Nuget包?forum = vcgeneral

rmt_curl nuget软件包: https://www.nuget.org/packages/rmt_curl/

rmt_curl nuget package: https://www.nuget.org/packages/rmt_curl/

因此,我切换为使用"curl"包,该包修复了头文件,但为每个curl函数导致了未解析的外部符号".

So I switched to use the "curl" package, which fixed the header file, but resulted in "unresolved external symbol" for each of the curl functions.

curl nuget软件包: https://www.nuget.org/packages/curl/

curl nuget package: https://www.nuget.org/packages/curl/

受其他评论和答案的启发,我将"libcurl.lib"添加到项目属性->链接器->输入->其他依赖项"列表中.这将导致无法打开文件'libcurl.lib'".

Inspired by other comments and answers I added "libcurl.lib" to the project "Properties->Linker->Input->Additional Dependencies" list. This results in "cannot open file ‘libcurl.lib’".

尝试了"packages \ curl.7.30.0.2"文件夹中大约一半的"libcurl.lib文件"后,我终于找到了一个已编译的文件.我在项目属性->链接器->常规->附加库目录"中添加了"$(SolutionDir)\ p​​ackages \ curl.7.30.0.2 \ build \ native \ lib \ v110 \ Win32 \ $(Configuration)\ dynamic"字段.

After trying about half of the "libcurl.lib files in the "packages\curl.7.30.0.2" folder, I finally found one that compiled. I added "$(SolutionDir)\packages\curl.7.30.0.2\build\native\lib\v110\Win32\$(Configuration)\dynamic" to the project "Properties->Linker->General->Additional Library Directories" field.

现在的问题是,运行它时,找不到LIBCURL.dll".这使我意识到,我早先喜欢动态版本的.lib文件.这不是我想要的.我想静态链接库,而不要麻烦DLL.

Now the problem is that when running it, "LIBCURL.dll is not found". This made realize that earlier I liked against the dynamic version of the .lib file. This is not what I want. I want to statically link the library and not bother with a DLL.

这是在实际应用中尝试运行之前的示例代码:

Here is the sample code I am trying to run before I try this in my real application:

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


int main()
{
    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;
}

我详细研究了每个相关问题,如果我需要的答案在其中,则看不到它:

I looked in detail at each of these related questions and if the answer that I need is in there, I do not see it:

使用curl NuGet无法解析的外部符号

使用Visual Studio 2013的cURL

CURL Nuget visual函数_main()中引用的Studio 2013无法解析的外部符号__imp__curl_easy_init

添加curl库C ++

这是我第一次将Nuget与C ++项目一起使用,因此很可能我根本不了解简单的内容.

This is the first time I am using Nuget with a C++ project, so it is very possible that I simply do not understand something simple.

推荐答案

如何在C ++应用程序中使用curl Nuget包

How to use curl Nuget package in C++ application

由于使用的是curl软件包,因此您会注意到Visual Studio只有两个lib版本: v100 和& v110 :

Since you are using the curl package, you will notice that there are only two lib versions for Visual Studio: v100 & v110:

curl程序包仅应在Visual Studio V100和V110上正常工作.我在Visual Studio 2010和2012上使用curl包测试了您的示例代码,效果很好.

The curl package should only work fine on Visual studio V100 and V110. I tested your sample code with the curl package on Visual Studio 2010 and 2012, and it worked fine.

因此,此程序包与VS 2015& VS 2017 (VS 2017使用工具集v141,该软件包仅支持v110和v120).如果要在Visual Studio 2015和2017中使用此程序包,可以将工具集更改为上一个工具包.转到项目属性->常规->平台工具集,并将其更改为Visual Studio 2010(v100). (您将需要安装VS2010或VS 2012 .)对其进行测试并确认其工作正常.

So this package is not compatible with VS 2015 & VS 2017 (VS 2017 uses toolset v141 and the package only supports v110 and v120). If you want to use this package on Visual Studio 2015 and 2017, you can change the toolset to a previous one. Go into Project Properties->General->Platform Toolset, and change it to Visual Studio 2010 (v100). (You will need to install the VS2010 or VS 2012) Test it and confirm it works fine.

如果您不想安装VS2010或VS2012,则可以尝试使用rmt_curl软件包,但是有些地方需要注意,否则您将无法获得它工作.

If you don't want to install VS2010 or VS2012, you can try to use the rmt_curl package instead, but there are some points you need to pay attention to, otherwise you will not get it to work.

对于 VS2015 ,在安装软件包rmt_curl后,您会注意到curl.h存在于外部依赖项中:

For VS2015, after you install the package rmt_curl, you will notice that the curl.h exists in the External Dependencies:

因此,只需使用#include <curl.h>而不是#include <curl/curl.h> 即可包含头文件.使用#include <curl.h>后,该项目在我的Visual Studio 2015中正常构建.

So just use #include <curl.h> instead of #include <curl/curl.h> to include the header file. After using #include <curl.h>, the project built fine in my Visual Studio 2015.

对于Visual Studio 2017,rmt_curl不起作用,但是您可以将工具集更改为Visual Studio 2015(v140)(您需要安装Microsoft Build Tools 2015或VS 2015).它对我有用.

For Visual Studio 2017, rmt_curl does not work, but you can change the toolset to Visual Studio 2015 (v140) (You'll Need install Microsoft Build Tools 2015 or VS 2015). It works for me.

希望这个答案不会打扰您.似乎curl软件包不是完美的,所以我们在使用它时应该多加注意,这样它才能变得更加完美.

Hope that this mess of answer doesn't bother you. It seems that the curl package is not perfect, so we should pay more attention when using it so it can become more perfect.

希望以上信息可以为您提供帮助,如果您对此解决方案有任何疑问,请告诉我.

Hope the above info can help you, and if you have any doubts about this solution, please let me know.

这篇关于如何在C ++应用程序中使用curl Nuget包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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