Amber Jain's weblog (OLD)

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

Archive for the ‘Programming’ 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

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

g++ warnings on OpenBSD: “strcpy() is almost always misused, please use strlcpy()” and “strcat() is almost always misused, please use strlcat()”

with 4 comments

strcpy() and strcat() related warnings on OpenBSD with gcc/g++

I have a box with OpenBSD 4.5 installed. When compiling c++ programs using g++, I get following warnings:

/usr/lib/libstdc++.so.47.0: warning: strcpy() is almost always misused, please use strlcpy()
/usr/lib/libstdc++.so.47.0: warning: strcat() is almost always misused, please use strlcat()
Well, “Unix Programming Tools” from Stanford’s website says:
“Getting used to compiles that produce “a few warnings” is a very bad habit”.

I asked the reason for these warnings on ##c++-basic on irc.freenode.net. The guys there suggested me some options (e.g. -Wno-deprecated, -Wno-deprecated-declarations)

Then somehow this thought strike my mind. What if this has something to do with ‘propolice’? OR What if this is OpenBSD related issue (?) OpenBSD strives hard to be secure. Although, not all OpenBSD packages go through rigorous security audit, but then gcc is something that is likely to be ‘modified’ to fit OpenBSD’s goals. So, I asked this on #openbsd (at freenode). Here’s the IRC log:

AmberJ> Why does gcc/g++ outputs 2 warnings: “strcpy() is almost always misused, please use strlcpy() [and the same with strcat]”….Is it because openbsd has propolice enabled?
….because I don’t seem to encounter those warnings on other OSes with propolice disabled
AmberJ: it’s a linking warning, you’ll only see it when you link something that uses strcpy() etc
oenone, But why those warnings pop up only on OpenBSD ?
AmberJ: because other OSes don’t have the warnings in their version of libc

I updated this info to ##c++-basic. IRC-log:

AmberJ> That’s it! I hope that is the answer to my original question
jps_77, SukhE metabol Leoneof` thanks all πŸ™‚
AmberJ: that was a retarded reply that you got in #openbsd
try in #gcc

I then tried at #gcc as suggested(at freenode). IRC log:

AmberJ> Why does gcc/g++ outputs 2 warnings: “strcpy() is almost always misused, please use strlcpy() [and the same with strcat]”….Is it because openbsd has propolice enabled?
I’m using gcc 3.3.5 (propolice)
Ofcourse, I’m using OpenBSD 4.5
AmberJ: that sounds a lot like OpenBSD broke it. (they invented those l forms)
noshadow, Someone at #openbsd mentioned: “Other OSes don’t hae similar warnings because other OSes don’t have the warnings in their version of libc”
AmberJ: yes, that’s a possible place where they could have put those uncessary warnings.
noshadow, ok ty πŸ™‚
though the warning is really the wrong way round. why strcpy is usefull often, strlcpy is always always misused…
ok

Ofcourse, you can feel the ‘anti-openbsd’ feeling in the air πŸ˜‰

In the end, the folks at ##c++-basic suggested me that I try to install a newer version of gcc i.e. 4.x (mind you, gcc/g++ 3.3.5-propolice is the version that is shipped with OpenBSD 4.5) and if this problem persists (Is this really a problem? Read this. I suppose the warning is apt.), they suggested that I try to compile gcc from source.

More soon.

πŸ™‚

Written by Amber Jain

January 9, 2010 at 1:24 PM