Amber Jain's weblog (OLD)

Visit my new weblog at

Archive for the ‘Microsoft Windows’ Category

Compile C code (that uses libraries) using gcc on Windows

with one comment

Ok. I know that you probably know how to compile code using gcc. You use something like following command:

gcc -Wall -g input_filename.c -o output_filename


gcc -Wall -c -g input_filename.c
gcc -Wall -o output_filename input_filename.c

Right? Ok. RTFM for more details.

The above compile command is really easy and works on Linux, BSD, Unix, Windows etc. On Linux/BSD/Unix, compiling code that uses some library is not so difficult and (almost?) same across different distributions (Makefiles exist to ease the compile/build process). But on Windows, it’s a bit different. Personally, it took me more than half an hour and help of fine folks at #mingw (at to understand how to compile a C program that uses some library.

In this post, I’ll take libcurl as an example to illustrate compiling a C program that uses some library. libcurl is the multiprotocol file transfer library.
For this post, we will try compiling and building (an executable) using following example code:


#include <stdio.h>
#include <curl/curl.h>

int main(void)
// Source:
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "");
res = curl_easy_perform(curl);
/* always cleanup */
return 0;

NOTE: Download and extract this to C:\ so that your directory structure should look like:
More libcurl downloads are here.

Let me start with the command that is the ultimate purpose of this post (i.e. to compile C code on Windows that uses some library). Here:

gcc main.o -o test_binary -LC:\libcurl-7.19.3-win32-ssl-msvc\lib\Release -lcurllib

Here are details of various command line arguments (in order) in above command:

#1. gcc: The first command line argument, ‘gcc’, is the name of compiler.

#2. main.o: The second argument, ‘main.o’, is the name of object file input to compiler that was previously compiled from source code using:
gcc -Wall -c -g main.c

#3. -o: The third argument, ‘-o’, specifies that the argument immediately following this argument is the name of output binary executable, as per the needs of the programmer (or the person who is compiling the code). Also, see #4 that follows.

#4. test_binary: The fourth argument, ‘test_binary’, is the name of output binary executable, as per the needs of the programmer (or the person who is compiling the code). This is bind to ‘-o’ command line argument and should immediately follow it. Also, see #3 that precedes.

#5. -LC:\libcurl-7.19.3-win32-ssl-msvc\lib\Release: Now comes the real part (Pause here and remind yourself the actual purpose of this post). The fifth argument, ‘-L’, specifies that the string appended to -L flag/option is the directory that should be searched for library files specified by ‘-l’ flag (Also, see #6 that follows). Mind you, there is *no* space between the option flag (-L) and the library directory name.

#6. -lcurllib: The sixth argument, ‘-l’, tells linker that the string appended to -l option flag is the library that is to be searched for unresolved symbols when linking. The actual file will be something like either libmylib.a or mylib.lib or something similar (@experts: right?).

I must confess here that I’m still unable to use makefiles *on windows* to automate the process of compiling and building (though I tried only for less than 1 hour). Manual compiling is surely cumbersome, but who cares ๐Ÿ˜€ I very rarely compile my code on Windows and so, I’ll get past this hurdle too sometime in future when I get some free time. And, maybe I’ll do a blogpost on it too.

That’s it. I guess that you get it. If you still have questions/problems, you can try following:
1. Get help from #mingw (
2. RTFM.
3. Stanford’s Unix Programming Tools contains some useful information (It’s really useful, even if it is for Unix. Read it.).

PS: In case you get ‘XYZ.dll not found’ error when running the executable, just find the right DLL(s) [on the internet] that are missing and put them in same directory that contains the binary executable. Easy, isn’t it?


Written by Amber Jain

May 18, 2010 at 7:10 PM