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)

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Wild Widow
  • 2,359
  • 3
  • 22
  • 34
  • 1
    This is not your actual code, `ULONG` is not a type – Jonathan Wakely Mar 18 '14 at 16:06
  • `ULONG` is a `typedef unsigned long` in Windows headers. I suspect, you've updated your `MinGW` from `gcc 4.3` to `gcc 4.8` and got this error? – lapk Mar 18 '14 at 16:28
  • Why are you extern "C"'ing a static member of a class? Isn't that exactly the opposite of what extern "C" is meant to mean (i.e. making sure that a C++ compiler understands it needs to apply a C ABI to a function call to a external C object). – Klaas van Gend Mar 19 '14 at 08:49

2 Answers2

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.

Bfitzy
  • 1
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.

keltar
  • 17,711
  • 2
  • 37
  • 42