Amber Jain's weblog (OLD)

Visit my new weblog at http://amber-jain.blogspot.in/

Archive for May 2010

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

OR

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 irc.freenode.net) 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:

Source: http://curl.haxx.se/libcurl/c/simple.html


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


int main(void)
{
// Source: http://curl.haxx.se/libcurl/c/simple.html
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}

NOTE: Download and extract this to C:\ so that your directory structure should look like:
C:\libcurl-7.19.3-win32-ssl-msvc\lib\Release
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 (irc.freenode.net).
2. RTFM. http://gcc.gnu.org/onlinedocs/
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?

🙂

Advertisements

Written by Amber Jain

May 18, 2010 at 7:10 PM

Amber Jain’s list of *free* books available online for free

with one comment

Quoting Stewart Brand:

Information wants to be free

I guess after reading the preceding quote, the intent of this blog post is clear.

I wrote a related blog-post sometime back: Links to *free* legal ‘readable stuff’ online.. This post is an extension of the idea behind that post.
I’ll post links to books (in no particular order) that are available online for free here. I’ll update this list whenever possible. If there are similar links/resources that your know of, post them in comments:

0. The C Book, second edition (by Mike Banahan, Declan Brady and Mark Doran)
This was originally published by Addison Wesley in 1991. This book is no longer in print though it can be read online for free.
The C book

1. Handbook of Applied Cryptography – fifth printing (by Alfred J. Menezes, Paul C. van Oorschot and Scott A. Vanstone)
Handbook of Applied Cryptography

2. Bruce Eckel’s Free Electronic Books/seminar etc:
http://www.mindviewinc.com/Books/downloads.html
http://mindview.net/CDs/ThinkingInC/beta3



See also: Links to *free* legal ‘readable stuff’ online.

Written by Amber Jain

May 14, 2010 at 6:40 PM