Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use curl Nuget package in C++ application

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/en-US/a5cf5caa-450e-425b-af5b-84f8e1c198f9/c-visual-studio-2015-how-to-include-nuget-package-in-my-project?forum=vcgeneral

rmt_curl nuget package: https://www.nuget.org/packages/rmt_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 package: https://www.nuget.org/packages/curl/

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’”.

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.

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:

unresolved external symbol using curl NuGet

cURL with Visual Studio 2013

CURL Nuget visual studio 2013 unresolved external symbol __imp__curl_easy_init referenced in function _main()

Add curl library 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.

like image 923
Benjamin Cuningham Avatar asked Mar 08 '23 07:03

Benjamin Cuningham


1 Answers

How to use curl Nuget package in C++ application

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

enter image description here

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.

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.

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.

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

enter image description here

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.

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.

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.

like image 122
Leo Liu-MSFT Avatar answered Mar 28 '23 06:03

Leo Liu-MSFT