1

i have this simple C++ code.

#include <iostream>

#include <windows.h>
#include <Lmcons.h>

#define CURL_STATICLIB

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

#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "curl\\libeay32.lib")
#pragma comment(lib, "curl\\openldap.lib")
#pragma comment(lib, "curl\\ssleay32.lib")

#if defined _DEBUG
    #pragma comment(lib, "curl\\lib\\Debug\\curllib_static.lib")
#else
    #pragma comment(lib, "curl\\lib\\Release\\curllib_static.lib")
#endif

using namespace std;

int main()
{
    TCHAR name [ UNLEN + 1 ];
    DWORD size = UNLEN + 1;

    if (GetUserName( (TCHAR*)name, &size ))
        wcout << "Hello, " << name << "!\n";
    else
        wcout << "Hello, unnamed person!\n";

    CURL *curl;
    CURLcode res;

    curl_version();


    /* In windows, this will init the winsock stuff */ 
    curl_global_init(CURL_GLOBAL_ALL);

    /* get a curl handle */ 
    curl = curl_easy_init();
    if(curl) {
    /* First set the URL that is about to receive our POST. This URL can
        just as well be a https:// URL if that is what should receive the
        data. */ 
    curl_easy_setopt(curl, CURLOPT_URL, "http://requestb.in/19l9fjg1");
    /* Now specify the POST data */ 
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");

    /* Perform the request, res will get the return code */ 
    res = curl_easy_perform(curl);
    /* Check for errors */ 
    if(res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));

    /* always cleanup */ 
    curl_easy_cleanup(curl);
    }
    curl_global_cleanup();

    system("PAUSE");

    return 0;
}

My directory tree.

X
|   app.cpp
|   
+---curl
|   |   CHANGES
|   |   curl.exe
|   |   curllib.dll
|   |   libcurl_imp.lib
|   |   libeay32.dll
|   |   libeay32.lib
|   |   openldap.dll
|   |   openldap.lib
|   |   README
|   |   ssleay32.dll
|   |   ssleay32.lib
|   |   
|   +---docs
|   |   |   BINDINGS
|   |   |   [...]
|   |   |   VERSIONS
|   |   |   
|   |   +---examples
|   |   |       10-at-a-time.c
|   |   |       [...]
|   |   |       threaded-ssl.c
|   |   |       
|   |   \---libcurl
|   |           ABI
|   |           [...]
|   |           Makefile.in
|   |           
|   +---include
|   |   |   Makefile.am
|   |   |   [...]
|   |   |   README
|   |   |   
|   |   \---curl
|   |           curl.h
|   |           [...]
|   |           types.h
|   |           
|   +---lib
|   |   +---Debug
|   |   |       curllib.dll
|   |   |       curllib.lib
|   |   |       curllib_static.lib
|   |   |       
|   |   \---Release
|   |           curllib.dll
|   |           curllib.lib
|   |           curllib_static.lib
|   |           
|   \---src
|       +---DLL-Debug
|       |       curl.exe
|       |       curllib.dll
|       |       openldap.dll
|       |       
|       +---DLL-Release
|       |       curl.exe
|       |       curllib.dll
|       |       openldap.dll
|       |       
|       +---LIB-Debug
|       |       curl.exe
|       |       
|       \---LIB-Release
|               curl.exe
|               
+---Debug
|       app.obj
|       [...]
|       vc110.pdb
|       
\---Release
        app.obj
        [...]
        vc110.pdb

And thats my settings.

Linker -> General -> Additional library directories
    X\curl\lib\Release;X\curl;%(AdditionalLibraryDirectories)
Linker -> Input -> Additional dependences
    $(ProjectDir)curl\lib\Debug\curllib_static.lib;%(AdditionalDependencies)
VC++-Directories -> Include directories
    $(ProjectDir)curl\include\curl;$(IncludePath)
VC++-Directories -> Library directories
    $(ProjectDir)curl\lib\Release;$(LibraryPath)

**$(ProjectDir) is X**

Errors.

Error   2   error LNK2005: _calloc already defined in LIBCMT.lib(calloc.obj).   X\MSVCRT.lib(MSVCR110.dll)  X
Error   3   error LNK2005: _realloc already defined in LIBCMT.lib(realloc.obj). X\MSVCRT.lib(MSVCR110.dll)  X
Error   4   error LNK2005: _free already defined in LIBCMT.lib(free.obj).   X\MSVCRT.lib(MSVCR110.dll)  X
Error   5   error LNK2005: _malloc already defined in LIBCMT.lib(malloc.obj).   X\MSVCRT.lib(MSVCR110.dll)  X
Error   6   error LNK2005: _strrchr already defined in LIBCMT.lib(strrchr.obj). X\MSVCRT.lib(MSVCR110.dll)  X
Error   7   error LNK2005: _strchr already defined in LIBCMT.lib(strchr.obj).   X\MSVCRT.lib(MSVCR110.dll)  X
Error   8   error LNK2005: _fwrite already defined in LIBCMT.lib(fwrite.obj).   X\MSVCRT.lib(MSVCR110.dll)  X
Error   9   error LNK2005: ___iob_func already defined in LIBCMT.lib(_file.obj).    X\MSVCRT.lib(MSVCR110.dll)  X
Error   10  error LNK2005: _strtoul already defined in LIBCMT.lib(strtol.obj).  X\MSVCRT.lib(MSVCR110.dll)  X
Error   11  error LNK2005: _memmove already defined in LIBCMT.lib(memmove.obj). X\MSVCRT.lib(MSVCR110.dll)  X
Error   12  error LNK2005: _tolower already defined in LIBCMT.lib(tolower.obj). X\MSVCRT.lib(MSVCR110.dll)  X
Error   13  error LNK2005: _atoi already defined in LIBCMT.lib(atox.obj).   X\MSVCRT.lib(MSVCR110.dll)  X
Error   14  error LNK2005: _isxdigit already defined in LIBCMT.lib(_ctype.obj). X\MSVCRT.lib(MSVCR110.dll)  X
Error   15  error LNK2005: _isdigit already defined in LIBCMT.lib(_ctype.obj).  X\MSVCRT.lib(MSVCR110.dll)  X
Error   16  error LNK2005: _isspace already defined in LIBCMT.lib(_ctype.obj).  X\MSVCRT.lib(MSVCR110.dll)  X
Error   17  error LNK2005: __strtoi64 already defined in LIBCMT.lib(strtoq.obj).    X\MSVCRT.lib(MSVCR110.dll)  X
Error   18  error LNK2005: _memchr already defined in LIBCMT.lib(memchr.obj).   X\MSVCRT.lib(MSVCR110.dll)  X
Error   19  error LNK2005: _fclose already defined in LIBCMT.lib(fclose.obj).   X\MSVCRT.lib(MSVCR110.dll)  X
Error   20  error LNK2005: _strtol already defined in LIBCMT.lib(strtol.obj).   X\MSVCRT.lib(MSVCR110.dll)  X
Error   21  error LNK2005: _sprintf already defined in LIBCMT.lib(sprintf.obj). X\MSVCRT.lib(MSVCR110.dll)  X
Error   22  error LNK2005: _fflush already defined in LIBCMT.lib(fflush.obj).   X\MSVCRT.lib(MSVCR110.dll)  X
Error   23  error LNK2005: __errno already defined in LIBCMT.lib(dosmap.obj).   X\MSVCRT.lib(MSVCR110.dll)  X
Error   24  error LNK2005: __lseeki64 already defined in LIBCMT.lib(lseeki64.obj).  X\MSVCRT.lib(MSVCR110.dll)  X
Error   25  error LNK2005: _getenv already defined in LIBCMT.lib(getenv.obj).   X\MSVCRT.lib(MSVCR110.dll)  X
Error   26  error LNK2005: _isalnum already defined in LIBCMT.lib(_ctype.obj).  X\MSVCRT.lib(MSVCR110.dll)  X
Error   27  error LNK2005: _isalpha already defined in LIBCMT.lib(_ctype.obj).  X\MSVCRT.lib(MSVCR110.dll)  X
Error   28  error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info@@AAE@ABV0@@Z) already defined in LIBCMT.lib(typinfo.obj).  X\MSVCRT.lib(ti_inst.obj)   X
Error   29  error LNK2005: "private: class type_info & __thiscall type_info::operator=(class type_info const &)" (??4type_info@@AAEAAV0@ABV0@@Z) already defined in LIBCMT.lib(typinfo.obj).    X\MSVCRT.lib(ti_inst.obj)   X
Error   30  error LNK2005: __strdup already defined in LIBCMT.lib(strdup.obj).  X\MSVCRT.lib(MSVCR110.dll)  X
Error   31  error LNK2005: __close already defined in LIBCMT.lib(close.obj).    X\MSVCRT.lib(MSVCR110.dll)  X
Error   32  error LNK2005: __fileno already defined in LIBCMT.lib(fileno.obj).  X\MSVCRT.lib(MSVCR110.dll)  X
Error   33  error LNK2005: __read already defined in LIBCMT.lib(read.obj).  X\MSVCRT.lib(MSVCR110.dll)  X
Error   34  error LNK2005: __strnicmp already defined in LIBCMT.lib(strnicmp.obj).  X\MSVCRT.lib(MSVCR110.dll)  X
Error   35  error LNK1169: One or more multiply defined symbols found in.   X\..\Release\X.exe  X

If i replace curllib_static.lib with curllib.lib above i would get a error that the libcurl.dll is missing.

Because that AND because i want no extra libcurl.dll in my directory i'm using static libcurl.

How to fix that?

I'm using this libcurl with Microsoft Visual Studio 2012. http://curl.haxx.se/download/libcurl-7.19.3-win32-ssl-msvc.zip

PatrickB
  • 3,225
  • 5
  • 31
  • 55
  • Related: http://stackoverflow.com/questions/3411259/using-libcurl-without-dll – Adriano Repetti Sep 03 '14 at 12:36
  • @SHR: I added the errors a few seconds ago. And i tried to use the namespace at the end, but same errors. – PatrickB Sep 03 '14 at 12:42
  • @AdrianoRepetti: Doesnt work. I updated code for app.cpp above. – PatrickB Sep 03 '14 at 12:42
  • 1
    try to define `CURL_STATICLIB` before the `#include ` if you didn't did it in the pre-processor. – SHR Sep 03 '14 at 12:49
  • Thanks @SHR. But now i have other errors. I tried to comment some #pragma comment out but nothing changes. (except more/different errors) – PatrickB Sep 03 '14 at 12:54
  • @PatrickB now you've found the curl lib, but not ws2_32 and ssl. better add the libs to project properties. but meanwhile add full paths to `#pragma comment`s – SHR Sep 03 '14 at 13:04
  • Uhm... Wrong errors. I tried something and got "is already defined" ... One second, i fix this in first post. @SHR: Updated errors! – PatrickB Sep 03 '14 at 13:05
  • Look like the curl (or some other lib) was compile with other version of compiler. when using static lib it must be compiled with the same version of compiler. (DLLs are less sensitive, especially if use extern "c" interface ) a possible solution is to compile the lib by yourself. – SHR Sep 03 '14 at 13:40
  • Have you succeeded building curl statically? – Lukas Salich Sep 14 '19 at 23:49

2 Answers2

0

Problems like this are quite common with Visual C++, and it's not easy to describe exactly what to do to fix them. You need to take great care and really understand which libraries you are including, and there isn't enough info here to work it out.

One common cause is using the wrong runtime library. In VS that would be the difference between the /MT and /MD switches, but you seem to be using makefiles so that may not apply. LIBCMT is the multi-threaded library, but it looks as if you may be including another one as well.

I don't recommend using #pragmas to include link libraries. Put those in the makefile so you can see it all in one place.

Often it helps to run the linker with logging enabled to see exactly what libraries are being called in.

Your directory tree and settings are little help, but a minimal complete project including a makefile might allow someone here to diagnose the problem.

david.pfx
  • 10,520
  • 3
  • 30
  • 63
  • Is there something like jsfiddle for projects like this? or should i upload the project as zip for you? – PatrickB Sep 03 '14 at 13:50
  • No. What we're talking about is an http://stackoverflow.com/help/mcve. New project, minimal source code to reproduce, post it all here so everyone can see and learn, and hopefully answer. More work for you, more value for readers. – david.pfx Sep 03 '14 at 22:56
0

There is an old question in stackoverflow which issues a very similar problem, take a look here:

error LNK2005: xxx already defined in MSVCRT.lib(MSVCR100.dll) C:\something\LIBCMT.lib(setlocal.obj)

Here another link:

LNK2005 errors with LIBCMTD.lib and MSVCRTD.lib

I hope it helps you!

Community
  • 1
  • 1
fenix688
  • 2,435
  • 2
  • 17
  • 23