Sunday, December 11, 2005

Debugging a correct programme

I spent a few hours trying to figure out why my first seemingly correct C++ programme doesn't compile!!

I just found that the error was because you have to pass -lstdc++ to gcc!! But Why? I told the compiler to include a standard library. Can't it be intelligent enough? Its not even intelligence..its something that can be captured in a rule. gcc being a complete compile suit; that also controls linking and loading..Why does it not add something as trivial as stdc++ library.

Deciphering why -lmath was needed in C(inspite of #include ) might have strong ties to this -lstdc++ thing?

6 comments:

Abhishek Goyal said...

I should say, I used to crib about this whole mess of sending extra perimeters for linking a lot of times. But there is no full proof way of avoiding it. The math.h that you include in the program can be provided with -lmath or -lMyOwnLib, and if the automatically assigns librabry you might not be able to figure out whats going on in here. in case of java since you specify the whole package name, to some extent library name become no so ambiguous for compiler, even then problem can exists, if the included library has two instances with same name and your classpath is not what you have assumed :)

Mahesh Reddy said...

I completely agree on the -lMyOwnLib thing...its just that it would be nice to have a default way of doing things for those vast majority of cases and hooks to expose the advanced features (overriding defaults).

In short, Apple style of designing things ;-)

Vinay Khaitan said...

hi mahesh,

is it really your first c++ program ? difficult for me to believe. Anyway, Its not a surprise that -lstdc++ is needed, because otherwise, you may link stdc++ without any needs.
See, libc is by default linked -lc.
And many libraries dont use c++ libraries.
for example, Qt doesn't require -lstdc++ because they dont use it.
So you need to give -lstdc++ :)
I feel that except STL, you should not use -lstdc++ . Use direct c functions. Or libraries provided by your toolkits.

Anonymous said...

ed gande , by default gcc includes libc

- papa

Hari said...

Not only this..there many such non-sense things with C/C++. If a matrix can be intialized at the declaration time why not the same kind of assignment can not be done inbetween..something like
int a[3][3]={{1,2,3},{2, 3, 4}}--this is allowed.
Inbetween the code
a={{1,2,3},{2, 3, 4}}-- this is not allowed.

Anonymous said...

Good dispatch and this fill someone in on helped me alot in my college assignement. Thanks you on your information.