Friday, January 04, 2008

More Linux Frustrations

As stated in my previous post, I've managed to load Ubuntu Linux (my version of Ubuntu is a GUI over a Gnome Linux kernel), dual-boot my computer between Linux and WinXP, and start some very basic programming.

Er, I may have left out that last part about the programming.

I used Synaptic Package Manager to load the Gnu C Compiler (gcc) into Ubuntu. I even managed to get the basic "Hello, world!" program made and compiled. And that's when the frustrations started.

I went a step beyond the "Hello, world!" by writing a really small program that would calculate and display the value of sin(1). That's it. Nothing fancy. Just wanted to see how the output would look. How many digits it would display.

Here's the program:

#include
#include

main()

{

double x;

x = sin(1);

printf("%f", x);

}


As I said, absolutely nothing fancy. I could have probably made it simpler, but this was simple enough. Then, I compiled it with the following command:

gcc complex.c -o complex

and got the following message:

/tmp/ccNjXwiK.o: In function `main':
complex.c:(.text+0x17): undefined reference to `sin'
collect2: ld returned 1 exit status

Those of you who are used to programming in Linux have seen the error. I did not include a link to the math libraries, meaning I didn't add the "-lm" switch with the command. Hence, my command should have been:

gcc complex.c -o complex -lm

That little switch tells the compiler to link to the math library.

Here's my obviously really stupid question: WHY do I have to link to the math library when I have #include math.h in the header of the program? When I compiled this exact same program in my Windows box, I received absolutely no errors with the following command:

cl complex.c

See how simple that was? Just typed in the command for the compiler, and it compiled. Wow! What a thought? Who would have thunk it?

Instead, I have to manually put in a switch telling the compiler to load the math library. When I have math.h in the header.

Yeah, Linux is so much better than Windows.

1 comment:

Anonymous said...

Actually, this isn't specific to Linux at all.

It's a design philosophy issue from somewhere way back; Gcc existed before Linux, and this was in Gcc already back then.

There are a few such legacy programmer-interface issues in Windows too, different ones of course.