Skip to content

Instantly share code, notes, and snippets.

@Dr-Chaos
Forked from henriquegemignani/CMakeLists.txt
Last active June 7, 2018 19:45
Show Gist options
  • Select an option

  • Save Dr-Chaos/b0afd70a67ba57d230cde0547867953c to your computer and use it in GitHub Desktop.

Select an option

Save Dr-Chaos/b0afd70a67ba57d230cde0547867953c to your computer and use it in GitHub Desktop.
vcpkg curl example
mkdir build && cd build
cmake .. -G "NMake Makefiles" -DCMAKE_TOOLCHAIN_FILE=C:\Windows\Documents\vcpkg\scripts\buildsystems\vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows && cmake --build .
cmake_minimum_required(VERSION 3.0)
set(CMAKE_BUILD_TYPE Release)
project(Application)
find_package(CURL REQUIRED)
include_directories(Application ${CURL_INCLUDE_DIRS})
add_executable(Application main.cpp)
target_link_libraries(Application ${CURL_LIBRARIES})
// CURL
#include <curl/curl.h>
// STL
#include <sstream>
#include <cerrno>
#include <cstdio>
#include <cstring>
size_t write_callback(char* contents, size_t size, size_t nmemb, void* output_buffer_void) {
auto real_size = size * nmemb;
auto output_buffer = static_cast<std::string*>(output_buffer_void);
output_buffer->append((char*)contents, real_size);
return real_size;
}
int main(int argc, char** argv) {
// Init
curl_global_init(CURL_GLOBAL_DEFAULT);
CURL* curl = curl_easy_init();
// Download from an HTTPS url
const char* url = "https://example.com/";
curl_easy_setopt(curl, CURLOPT_URL, url);
// Do a GET
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
// Save output to a std::string
std::string output_buffer;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &output_buffer);
// Save error to a buffer
char error_buffer[CURL_ERROR_SIZE];
error_buffer[0] = '\0';
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);
// Execute the request
long http_code = 0;
CURLcode res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
if (res == CURLE_OK) {
printf("Request was succesful with status %d, output:\n%s",
http_code, output_buffer.c_str()
);
} else {
printf("Request failed: %s", error_buffer);
}
// Cleanup
curl_easy_cleanup(curl);
curl_global_cleanup();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment