Skip to content

GU Setting up your environment

Marcus Ottosson edited this page Jan 25, 2015 · 2 revisions

You are here.

If not, go here to get set up.

Download, compile and install

Now let's go ahead and download and unpack the libcurl source-code.

I'm storing it under

/home/marcus/Downloads/curl-7.40.0

Now we can configure, compile and install like this.

$ cd ~/Downloads/curl-7.40.0   # Enter the directory in which the source code is located
$ ./configure                  # Run the autotools* configuration
$ make                         # Compile
$ sudo make install            # Install into the global space, with sudo**
  • ** sudo means "Run as Administrator". You may be prompted for your password.

Test

To make sure we're at our best, let's try and compile one of the supplied example files from the http://curl.haxx.se website.

simple.c

#include <stdio.h>
#include <curl/curl.h>
 
int main(void)
{
  CURL *curl;
  CURLcode res;
 
  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
    /* example.com is redirected, so we tell libcurl to follow redirection */ 
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
 
    /* Perform the request, res will get the return code */ 
    res = curl_easy_perform(curl);
    /* Check for errors */ 
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));
 
    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  return 0;
}

From your terminal, type the following.

$ cd  # Return to your home directory
$ nano simple.c
# 1. Now paste the above source code using Ctrl + Shift + V
# 2. Ctrl + X to quit
# 3. Y to save
$ gcc simple.c -o simple -l curl

-l curl

This tells gcc to link with the library curl which we compiled just a few moments ago. When given a string such as this, gcc goes ahead and looks in a few places it expects to find libraries such as curl, like in the exact place we just installed it into.

We can inspect these paths, by running the following command.

$ gcc -xc -E -v -

At the very end, you should see something like this.

#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/4.6/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/4.6/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include

And if we take a look at, for example, /usr/local/include we can indeed see that therein lies curl.

$ cd /usr/local/include
$ ls
curl

By linking with this library, <curl/curl.h> curl is made available and can be imported along with the standard library stdio.h. Amazing!

  • More information about GCC arguments here

Run it

You may not have noticed, but the file compiled just now and produced an executable for us. If you glance at the source code above, you might notice the address example.com. This is the website we download via this simple program, that is why the output looks similar to a .html file!

$ ./simple
<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
...

Conclusion

Wonderful, all is well in the world and we can continue on to developing our Terminal Download Application!

[Writing the software ->](Writing the software)