For an ongoing project I needed to retrieve webpages from my C-program and first I used sockets directly, but then I thought why not try use libcurl? If I could use libcurl and link it statically then I would get the good features from libcurl, but still could make my application stand alone.
With my current setup running Debian (Lenny), I tried to just apt-get the libraries (libcurl4-openssl-dev) and build using those libraries That went fine until I tried to link them statically with my application. After reading different mail archives, forum and tried on my own I got my application to link (static) with libcurl. Basically what I needed to do was.
- Download libcurl source
- Compile libcurl myself (disable one define, more about that below).
- Point to my own built libcurl.a library when building my own application.
So to save some headache for myself next time I want to do it or for someone else reading this I’ve listed the steps I had to do to get this to work.
Download libcurl source
As of writing this, the latest version of libcurl was 7.21.0 so I downloaded and unpacked the source.
Next it was time to run ./configure and make. Here I basically disabled everything for libcurl except HTTP and FILE.
Edit the file lib/curl_config.h which is generated during the configure step. In this file we uncomment the line saying
If I didn’t do this, then I would have a dependency to librt which I didn’t want to.
Now I was done with libcurl, next I should use this library in my application.
Build the application using libcurl
In simple_libcurl.tar.gz I’ve put together the source and a Makefile which builds using the library just created. The magick is the LDFLAGS line used in the linking step on line 12 and 24 below. I.e. on line 12 I had have to point the the folder where the library is located that I had built myself (-L/path/to/my/libcurl.a) and I still needed to tell the linker that I wanted to use libcurl, hence the -lcurl parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
When this is achieved, then you have a quite clean binary when it comes to dependencies. In the example code in simple_libcurl I now have these dependencies on a machine running Debian (Lenny).
$ ldd main linux-vdso.so.1 => (0x00007f157db66000) libc.so.6 => /lib/libc.so.6 (0x00007f157d5f9000) /lib64/ld-linux-x86-64.so.2 (0x00007f157d94c000)
Have fun with libcurl!