40

I know this is programming questions but I'm just frustrated trying to figure out what I'm doing wrong..

I'm using visual studio 2010 and followed all the steps here: http://curl.haxx.se/libcurl/c/visual_studio.pdf

When I try to compile my solution I keep getting this error:

1>------ Build started: Project: LibCurl, Configuration: Debug Win32 ------
1>LibCurl.obj : error LNK2019: unresolved external symbol __imp__curl_easy_cleanup referenced in function _main
1>LibCurl.obj : error LNK2019: unresolved external symbol __imp__curl_easy_perform referenced in function _main
1>LibCurl.obj : error LNK2019: unresolved external symbol __imp__curl_easy_setopt referenced in function _main
1>LibCurl.obj : error LNK2019: unresolved external symbol __imp__curl_easy_init referenced in function _main
1>C:\Users\Kyle\Documents\Visual Studio 2010\libcurl\VisualStudio\LibCurl\Debug\LibCurl.exe : fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Source:

// LibCurl.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#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;
}
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Kyle
  • 3,004
  • 15
  • 52
  • 79
  • make sure you did steps 4.3.2 and 4.3.3 from the linked pdf. You're getting a linker error, which means it's not linking with the file `libcurl.lib`. – Jesse Beder Nov 14 '10 at 08:43
  • Thanks Jesse. I will redo the steps slower and see why it's not being linked. Thanks for the help! – Kyle Nov 14 '10 at 09:02

9 Answers9

110

I've been using static version of libcurl, and to link my program against it properly, I had to add definition:

CURL_STATICLIB

to build configuration of my project.

stoiczek
  • 1,333
  • 1
  • 8
  • 8
  • 15
    This is probably one of the common problems: if you are using libcurl as a STATIC library you need to define CURL_STATICLIB not only in the curl project, but also in the project where you want to use it. – PeterK Oct 29 '12 at 08:58
  • Another interesting point I've just met: You can define it in main project and forget to mention in another vcproj you are using. – Dmitry Kochkin Feb 11 '13 at 16:36
  • 3
    thank you so much. go to vc++ project settings | c++ | preprocessor and add the CURL_STATICLIB to preprocessor definitions and it works – Silver Moon Feb 22 '15 at 10:54
  • 1
    You just saved me a ton of work and a bunch of hair I would have oterhwise pulled out – Rick Jun 04 '15 at 06:13
  • Is it accrual on MSVC only? – triclosan Jul 17 '15 at 13:29
  • you saved my hair too – Hans Jan 08 '16 at 19:32
  • this worked as: #define CURL_STATICLIB, but only if I use W32 library... beats me why it does not work for W64; but, mission accomplished, now I move forward, thanks. – bcop Feb 11 '22 at 09:43
  • Ah! I noticed I was using W32 VS settings - so makes sense... – bcop Feb 11 '22 at 09:49
26

Besides defining CURL_STATICLIB, for me it was also necessary to link the following dependencies (including libcurl.lib or libcurld.lib):

  • Ws2_32.lib
  • Wldap32.lib
cdonts
  • 9,304
  • 4
  • 46
  • 72
  • Defining `CURL_STATICLIB` saved my bacon! Thanks! – T4cC0re May 30 '17 at 18:06
  • 1
    I had several more missing dependencies this answer helped me https://stackoverflow.com/questions/50477484/curl-static-link-unresolved-external-symbols – Stan Oct 16 '20 at 10:23
6

I ran into a similar issue - found that I was referencing the 64-bit location of libcurl.lib. Changed the link directory to the 32-bit location and the project compiled perfectly.

wayne
  • 95
  • 1
  • 4
5

This worked for me on VS2017 - x86 Release/Debug - MFC Static Library

Open project properties and review the following

  • C/C++ - Preprocessor - Preprocessor Definitions - Add CURL_STATICLIB

  • Linker - Input - Additional Dependencies - Add (CTRL+C)

ws2_32.lib

Normaliz.lib

Crypt32.lib

Wldap32.lib

libcurl_a.lib (libcurl_a_debug.lib for debug configuration)

  • C/C++ - General - Additional Include Directories - Add include folder to header files
BigChief
  • 1,413
  • 4
  • 24
  • 37
4

I had the same problem. I wrote how I finally was able to make CurlLib works, here: http://quantcorner.wordpress.com/2012/04/08/using-libcurl-with-visual-c-2010/ if you wish to have a look. Good luck!

tagoma
  • 3,896
  • 4
  • 38
  • 57
4

Looks like the libraries are not being successfully linked. Ensure the library directory is set to include the full path to the libcurl dll. Also make sure this library is actually added to your project.

Willi Ballenthin
  • 6,444
  • 6
  • 38
  • 52
2

After many ideas and configurations, I solved the problem adding this:

#pragma comment(lib, "lib/libcurl_a.lib")

where libcurl_a.lib is the name of the curl lib file and lib is the folder which contains it.

Zsolti
  • 1,571
  • 1
  • 11
  • 22
  • Thanks. That worked for me! Added it to one of the header-files. CURL_STATICLIB was not needed in this case, by the way I'm using my self-compiled version of curl 7.70.0 with: curl-7.70.0\winbuild> nmake /f Makefile.vc mode=dll. In this case use: #pragma comment(lib, "lib/libcurl.lib") – Eddz May 20 '20 at 10:51
1

I had the same error, the problem I had was that I built cURL according to this SO answer, which doesn't work if you wish /MT as a runtime library option.

In order to built cURL with respect to /MT and /MTD you have to also execute Set RTLIBCFG=static before actually building it with the nmake command in the very same console. Full process of building cURL this way can be found here.

Edit: In case the URL stops working, I will also put the instructions here:

  • Download and extract the CUrl source code to a temp directory. http://curl.haxx.se/download.html, in this tutorial we will be using curl-7.37.0
  • Open the “Visual Studio Command Prompt (2010)”
  • Browse to the Winbuilds folder. \curl-7.37.0\winbuild.
  • Type Set RTLIBCFG=static into the command prompt and hit enter. This will set up the compiler to build for /MT and /MTd.
  • Type nmake /f MakeFile.vc mode=static DEBUG=yes to build the debug version or
  • Type nmake /f MakeFile.vc mode=static DEBUG=no to build the release versions.
Rumák
  • 85
  • 7
  • Setting RTLIBCFG=static is not a good idea. ` Typically you shouldn't use that option, and nmake will default to the DLL CRT. RTLIBCFG is rarely used and therefore rarely tested.` – MartinZ Apr 29 '22 at 14:37
0

I had the same error. The problem was that I build the libcurl_a.lib in 32-bit, and my project was in 64-bit. You'll have to use a 64-bit hosted developer command prompt instead of the 32-bit prompt. So instead of opening it via "VS->Tools->Developer Command Prompt", you will have to use the shortcut "x64 Native Tools Command Prompt for VS 2022" from the Windows start menu. See https://learn.microsoft.com/en-us/cpp/build/how-to-enable-a-64-bit-visual-cpp-toolset-on-the-command-line?view=msvc-170