Skip to content

Instantly share code, notes, and snippets.

@cristianadam
Created July 12, 2022 16:46
Show Gist options
  • Save cristianadam/ad06ce4f3e839cb772fedf06bf33762f to your computer and use it in GitHub Desktop.
Save cristianadam/ad06ce4f3e839cb772fedf06bf33762f to your computer and use it in GitHub Desktop.
Hello Google World from C++ using libcurl on Windows.
cmake_minimum_required(VERSION 3.16)
set(HUNTER_PACKAGES CURL)
set(HUNTER_CURL_CMAKE_ARGS
CMAKE_USE_SCHANNEL=ON
CMAKE_USE_OPENSSL=OFF
)
include(FetchContent)
FetchContent_Declare(SetupHunter GIT_REPOSITORY https://github.com/cpp-pm/gate)
FetchContent_MakeAvailable(SetupHunter)
project(curl-test)
find_package(CURL CONFIG REQUIRED)
file(WRITE ${CMAKE_BINARY_DIR}/main.cpp [=[
#include <iostream>
#include <string>
#include <curl/curl.h>
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
int main(void)
{
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
std::cout << readBuffer << std::endl;
}
return 0;
}
]=])
add_executable(test-curl ${CMAKE_BINARY_DIR}/main.cpp)
target_link_libraries(test-curl PRIVATE CURL::libcurl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment