Thursday, April 19, 2007

One stupid mistake. I don't know why the Linkers don't give clever messages unlike the compilers. A linking error eats away one whole afternoon of valuable time. And morning I discover it is a silly mistake.

For sun compilers, If I try to define extern and initialize (like a bum) in two or more files, the compiler doesnt give any warnings, but it does strange things and the linker cribs about multiple definitions

extern unsigned char gStackCount = 0

This is a stupid Copy and Paste mistake. Never try to intialize an extern global variable (atleast in sun compilers). I tried to initialize it as an extern in two files, just for safety purposes and I was trying the whole day yesterday fighting a linker error regarding multiple defintions . Be careful for such stupid mistakes !!!!! Time is precious when your ass is on fire....

I want to write a piece on how Copy and Paste (although a very precious keystroke) can really screw up the code with untraceable and weird mistakes. I will do that someday. For now cheers on a friday morning!

2 comments:

richie said...

But this isn't an error that can be caught by the compiler, right?

I think a compiler deals with just one translation unit ( the cpp/c file, and the included header files ).

The linker is supposed to link all the .o together. The linker is supposed to resolve undefined symbols, and flag duplicate symbols.
Alva?

sudeep said...

No it won't be caught by the compiler. But the linker can give some good error messages :-) like extern can't be initialized everywhere.....