sszd
January 28th, 2008, 10:51 AM
Given the following code.
class Base
{
public:
int fn (const int);
};
int Base::fn (int i)
{
i = i + 2;
int j = i * 2;
return j;
}
int main ()
{
Base b;
int i = 7;
int j = b.fn (i);
}
Notice the inconsistency in the member function declaration vs its definition.
I can compile the above code using either the Sun 5.8 compiler or the gnu compiler. Neither one gives an error, or even so much as a warning. Why?
Interestingly enough, if you compile and link in separate steps, or create a dynamic library out of the class portion of the code, and then attempt to link with the library, you do get undefined symbol results, which is what I would have expected when compiling and linking in one step.
A little background: The only reason this is being brought up is some developers around here discovered a problem very similar to this with our system when trying to build some new code. The thing that was interesting about their problem was that the code would compile and link (in separate steps) just fine in debug mode. But in no-debug mode the issue surfaced, which was a good thing because the obvious fix was to fix the signatures. My curiosity got the best of me so I attempted to duplicate their problem with some very simple code (as shown above). I've described what happens above, but what I've been unable to duplicate is the debug vs no-debug issue. If anyone has any thoughts on this latter issue, I'd be interested in hearing that too. Thanks.
class Base
{
public:
int fn (const int);
};
int Base::fn (int i)
{
i = i + 2;
int j = i * 2;
return j;
}
int main ()
{
Base b;
int i = 7;
int j = b.fn (i);
}
Notice the inconsistency in the member function declaration vs its definition.
I can compile the above code using either the Sun 5.8 compiler or the gnu compiler. Neither one gives an error, or even so much as a warning. Why?
Interestingly enough, if you compile and link in separate steps, or create a dynamic library out of the class portion of the code, and then attempt to link with the library, you do get undefined symbol results, which is what I would have expected when compiling and linking in one step.
A little background: The only reason this is being brought up is some developers around here discovered a problem very similar to this with our system when trying to build some new code. The thing that was interesting about their problem was that the code would compile and link (in separate steps) just fine in debug mode. But in no-debug mode the issue surfaced, which was a good thing because the obvious fix was to fix the signatures. My curiosity got the best of me so I attempted to duplicate their problem with some very simple code (as shown above). I've described what happens above, but what I've been unable to duplicate is the debug vs no-debug issue. If anyone has any thoughts on this latter issue, I'd be interested in hearing that too. Thanks.