0

I have the following piece of code, that works with gcc 4.3 compiler, but when I compiled with gcc 4.8, got resolved symbol error ( linking time)

//test.cc
ULONG CPULimit = 200; 

// test.h

namespace ABC
{
    class STAT
    {
    public:
        static ULONG getCPULimit();
    }
}

in the same test.h file itself, I have defined the getCPULimit() function inline

inline ULONG
ABC::STAT::getCPULimit()
{
    extern ULONG CPULimit; 
    return CPULimit;
}

The above code worked with 4.3 compiler, with 4.8 got unresolved symbol error.

moving extern ULONG CPULimit outside the function, will work but it exposes the global variable. now I wrapped the function with extern "C" like this

extern "C"
{
    inline ULONG
    ABC::STAT::getCPULimit()
    {
       extern ULONG CPULimit; 
       return CPULimit;
    }
}

and surprisingly it worked, 1) I'm not sure how it worked , could anyone shed some light? Is this the right way of doing it?

2) what does it mean to have two externs (one extern C and one extern)

4

2 回答 2

0

ULONG is not a type, but if you didn't want to use a normal one you could typedef one. EDIT: ULONG should be ulong.

于 2014-03-18T16:09:30.590 回答
0

The most natural way - make your global variable static and make getCPULimit function non-inlined and implemented in .cc file.

I'm almost sure having C-ABI-incompatible function within extern "C" block is either undefined or forbidden. However, your function is static, so it may be compatible with C ABI (although it looks like a bad idea). Perhaps someone could bring a quotation from C++ standard about that.

于 2014-03-18T18:09:00.643 回答