0
votes

I have a problem with compiling DLL in C++. I am using CURL library for HTTP POST.

When I compile my code as Console App, I do not get compile errors. But that same code gets Unresolved external symbol errors.

Lines like:

curl_global_init(CURL_GLOBAL_DEFAULT);

curl = curl_easy_init();

curl_easy_setopt(...);
curl_mime_name(...);

What I am doing wrong with dll? What is missing?

Here is the the code:

// dllmain.cpp : Defines the entry point for the DLL application.
#define CURL_STATICLIB
#include "pch.h"
#include "CURL/curl.h"

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

__declspec(dllexport) int httpPOST();

BOOL APIENTRY DllMain(HMODULE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved
)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

int httpPOST() {
    CURL* curl;
    CURLcode res;

    curl_global_init(CURL_GLOBAL_DEFAULT);

    curl = curl_easy_init();

    if (curl) {
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
        struct curl_slist* headers = NULL;
        headers = curl_slist_append(headers, "Cookie: cmslog=1");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        curl_mime* mime;
        curl_mimepart* part;
        mime = curl_mime_init(curl);
        part = curl_mime_addpart(mime);
        curl_mime_name(part, "username");
        curl_mime_data(part, "user", CURL_ZERO_TERMINATED);
        part = curl_mime_addpart(mime);
        curl_mime_name(part, "requestcode");
        curl_mime_data(part, "123456", CURL_ZERO_TERMINATED);
    }
    return 0;
}

Code inside httpPOST() compiles without problems in console app.

Errors are:

Error LNK2001 unresolved external symbol __imp__curl_global_init

Error LNK2001 unresolved external symbol __imp__curl_easy_init

Error LNK2001 unresolved external symbol __imp__curl_easy_setopt

Error LNK2001 unresolved external symbol __imp__curl_slist_append

Error LNK2001 unresolved external symbol __imp__curl_mime_init

...

Error LNK1120 8 unresolved externals

1
Can you share the error message? - Jean-Marc Volle
My crystal ball tells me you're not linking the CURL library; you're consuming the headers to do your compile, but link-time is missing all the CURL things you referenced because the library is either missing or never added. Are you sure CURL/libcurl_a.lib is correct ? - WhozCraig
I have added errors. CURL/libcurl_a.lib is correct, I have checked it again. @Jean-MarcVolle @WhozCraig - look_up
Most probably you do not build the dll the same way you built the console app. Do you have strictly the same threading/debug options? - Jean-Marc Volle
Wrong library flavor. If linking the static curl library is actually intended then you need to define CURL_STATICLIB in the consuming project. This is not well standardized btw, alternative. - Hans Passant

1 Answers

1
votes

Problem was in #define CURL_STATICLIB order.

Corect order is:

#include "pch.h"
#define CURL_STATICLIB
#include "CURL/curl.h"

Thank you all for help.