简体   繁体   中英

Using libcurl on 32bit Windows

I'm trying to compile with libcurl on a 32bit windows install. I'm using mingw as a compiler. I downloaded libcurl from https://bintray.com/artifact/download/vszakats/generic/curl-7.58.0-win32-mingw.7z and am using this to compile my project:

g++ -o test.exe -g just_get_output.cpp http_requests.cpp -L C:\curl-7.58.0-win32-mingw\lib -lcurl -lpsapi

All the libcurl code is in http_requests.cpp:

#include <stdio.h>
#define CURL_STATICLIB
#include <curl/curl.h>

// https://kukuruku.co/post/a-cheat-sheet-for-http-libraries-in-c/

int http_post(char* url, char* data)
{
    CURL *curl;
    CURLcode res;

    curl_global_init(CURL_GLOBAL_ALL);

    curl = curl_easy_init();
    if(curl) 
    {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
        res = curl_easy_perform(curl);
        if(res != CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        }
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
    return 0;
}

My main body of code, heavily redacted:

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <windows.h>
#include <tchar.h>
#include <psapi.h>
#include "http_requests.cpp"

int main( void ) {
    std::string info = "test";
    std::string url = "192.168.30.1:8080";
    http_post(url, info)
    return 0;
}

The error I'm getting:

Z:\>g++ -o test.exe -g just_get_output.cpp http_requests.cpp -L C:\curl-7.58.0-win32-mingw\lib\libcurl.a -lcurl -lpsapi
In file included from just_get_output.cpp:11:0:
http_requests.cpp:3:23: fatal error: curl/curl.h: No such file or directory
 #include <curl/curl.h>
                       ^
compilation terminated.
http_requests.cpp:3:23: fatal error: curl/curl.h: No such file or directory
 #include <curl/curl.h>
                       ^
compilation terminated.

What is going on? My precompiled libcurl.a is in the right directory, I'm linking it properly using -L and -l, and I'm not sure what I'm missing.

This is a compile error. It's expecting the directory above curl/curl.h to be on the include path.

For anyone else looking, you can use -I (capital I) to link to C:\\curl-7.58.0-win32-mingw\\include. Thank you @RichardCritten!

As always, you should read the documentation :

Compiling the Program

Your compiler needs to know where the libcurl headers are located. Therefore you must set your compiler's include path to point to the directory where you installed them. The 'curl-config'[3] tool can be used to get this information:

$ curl-config --cflags

I haven't used libcurl but, based on this page, I think what you should be executing is this:

g++ -o test.exe -g \
   just_get_output.cpp http_requests.cpp \
   `curl-config --cflags` \
   `curl-config --libs`

Now the required parameters will be emplaced properly for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM