Step by Step How To Use cURL Library Using C From VS Code Windows



When we want to develop a pure C program, not C++, and we need to transfer and download files over the internet via http or any other internet protocol. I am not expert in this but it is probably platform specific, meaning different OS have different API. so without a library, it’s gonna be so much painful to just implement Http GET or POST.

In C there’s been a solid Http file transfer library as you may already know is a library called cURL. So we can use cURL to do file transfer.

Here’s a step by step how to use cURL in C and an example code doing HTTP GET, to download Google webpage and store it in our current project directory.

1. Firstable, if you don’t have a VCPKG package manager, you need to follow instructions to install VCPKG here. If you already have VCPKG, skip to step 3.

2. In that article I also cover how to start a CMake project in VS code.

3. Because we are going to write our code in C, you might rename main.cpp to main.c, and change inside CMakeLists.txt line add_executable(your-project main.cpp) to add_executable(my-project main.c).

4. Now we install cURL library using VCPKG
vcpkg install curl
vcpkg integrate install
5. And add cURL to our CMakeLists.txt
find_package(CURL CONFIG REQUIRED)
target_link_libraries(your-project PRIVATE CURL::libcurl)
6. Now test using this C code, it’s a code to download Google web page:



Programming in C or C++ may not be very convenient as it's kind of on low level from the programming language spectrum. But I love doing programming in C because of this love, it's kind of challenging, but only a personal project. The company I work for usually just uses PHP, Java and Javascript, as the company i work only do web application stuff.

There's plenty of similar libraries in C for doing internet file transfer, not just cURL, for example there's wget, and even you can find many small library projects on github for doing your file transfer. But cURL is much popular and stable, so it is ready for production. I mean literally you probably use cURL every single day when you are working with a computer, but you didn’t notice that, because it’s hiding behind the backend of your app you are using.

Popular posts from this blog

JavaFX Login Example Codes

ERROR 1348 Column Password Is Not Updatable When Updating MySQL Root Password

How To Create Spring Boot Project Using Netbeans

Upload and Download Rest API using Spring Boot Reactive WebFlux

Spring Kafka - how to use ReplyingKafkaTemplate send and reply synchronously