开发者

Will the linker compare the access level when finding a member function at linking time?

Accidently, I encounter a linking error. The program is somewhat like this:

//a.h
class A
{
int a;
#ifdef AAA
public:
#endif
int getA();
}

//a.cpp
include "a.h"
int A::getA()
{
return a;
}

//test.cpp
#include "a.h"
int main()
{
A a;
a.getA();
return 0;
}

These three files are in two project, a.h and a.cpp in a project A in which the AAA macro is undefined, test.cpp in a project Test in which AAA macro is defined. And project Test denpends on project A. Then I encounter a link error. I did this test on visual studio 2008.

So my question is this:"Will the link compare the access level when finding a member function symbol at linking time?" In my previous opinion, th开发者_运维知识库e access level only take effect in compilation. But in this case, it seems that the access level may also make effect in linking time.


The linker is innocent. C++ compilers mangel names and incorporates things as access modifiers, overloads (i.e. return and argument types), template(? not quite sure) etc... in the final to produce something unambigious that also forms whatever the linker considers a valid identifier (at least [a-zA-Z_][a-zA-Z0-9_]*, as C requires no mangling). The linker only sees that mangled name, and it can't report anything except "you call this function but it's not defined anywhere". public A::getA() a different name as private A::getA().

The solution? Don't use the preprocessor for such batshit things. Or convince the VS developers to intercept such error messages and translate them into something more meanignful (but since they didn't in the past and sane code rarely encounters this problem, that's unlikely).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜