1

I am currently porting some code from Visual Studio to Mingw GCC. It seems there is a problem when I attempt to use a static variable . This code works fine in Visual studio so I am not sure what the problem might be here. I have pasted an outline of what the code looks like. If this is insufficient I could put in more.

Header file: .h
namespace LG_Wrapper
{
    template <LG_Thread Thread>
    class EffectApplication : public ktApplication
    {
    public:
    static EffectApplication<Thread>& GetInstance();
    protected:
        .....
        .....
        static boost::recursive_mutex mResource;
      }
}

DECLARE_LEGACY_TYPES(EffectApplication);


Source File: .cpp
namespace LG_Wrapper
{

    template<>
    boost::recursive_mutex  EffectApplication<LG_Thread_NAME>::mResource;  //LG_Thread_NAME type discussed below

    template <LG_Thread Thread>
    EffectApplication<Thread>& EffectApplication<Thread>::GetInstance()
    {
        boost::recursive_mutex::scoped_lock mutex( mResource ); //Error with mResource

        static EffectApplication<Thread> instance;
        return instance;
    }
    ....
}

Now I am not sure about the LG_Thread_NAME type. After going through the code I noticed that I define the following in the project settings

LG_THREAD_NAME=GAME

This definition comes into play with this macro somewhere else in the code

#define DECLARE_LEGACY_TYPES(...)

I am having issue accessing mResource The code builds fine if I comment out the statement

boost::recursive_mutex::scoped_lock mutex( mResource );

Any suggestions on why I might be getting the following error with the above code ?

(.text$_ZN10KT_Wrapper17EffectApplicationILNS_9LT_ThreadE0EE11GetInstanceEv[_ZN10LT_Wrapper17EffectApplicationILNS_9LT_ThreadE0EE11GetInstanceEv]+0x11): undefined reference to `LT_Wrapper::EffectApplication<(LT_Wrapper::LT_Thread)0>::mResource'

Update: I am certain that there is something wrong with the variable mResource because in the static method. If I do this it builds fine

  boost::recursive_mutex l;
boost::recursive_mutex::scoped_lock mutex( l );

Duplicate Question Issue: I am not sure how this question is a duplicate of the links provided. In all the links provided the ops have not initialized their static variables. Here I have initialized my static variable outside the class.

James Franco
  • 4,516
  • 10
  • 38
  • 80
  • Did you include the `boost` in the include path? – Thomas Matthews Apr 15 '15 at 22:42
  • Yes I did. I am using a lot of boost libraries in the project. And the boost libraries are included. I have also used `-lboost_thread-mgw48-mt-d-1_57` flag when building – James Franco Apr 15 '15 at 22:44
  • Did you give a definition of `mResource` in a translation unit? Not a declaration (like you have there), but a definition. [Like this](http://stackoverflow.com/questions/16284629/undefined-reference-to-static-variable-c) and [this](http://programmers.stackexchange.com/questions/145299/why-the-static-data-members-have-to-be-defined-outside-the-class-separately-in-c). – Cornstalks Apr 15 '15 at 22:54
  • @Cornstalks I am not sure what you mean. I have given an initializing value for the static variable which is in the source file as such `boost::recursive_mutex EffectApplication::mResource;` – James Franco Apr 15 '15 at 23:00
  • I am not sure how this is a duplicate. I have initialized the static object outside the class. In the links provided the OPS have not done that – James Franco Apr 15 '15 at 23:03
  • Are you compiling and linking to that source file? – Cornstalks Apr 15 '15 at 23:04
  • Yes. Also could you tell me how this question is a duplicate ? I might be missing something then because from what I have read from those posted links I am already doing that. – James Franco Apr 15 '15 at 23:06
  • @JamesFranco: It would be a whole lot easier to tell you what you're doing wrong if you provided a [MVCE](http://stackoverflow.com/help/mcve). My guess is that it has to do with how your definition is a `template <>` that only covers `LG_Thread_NAME` (rather than a truly generic `template `). If you can provide enough details to prove this isn't a duplicate and properly demonstrate what the issue is, then I'll vote to reopen. But as it is, there's not enough details here for me to be convinced otherwise. That MVCE thing I mentioned is critical. – Cornstalks Apr 15 '15 at 23:10
  • Thanks for clearing that up. Ill try to build a working example and update this post. thanks – James Franco Apr 15 '15 at 23:11
  • So I updated the question with more finding. Apparently I did not mention a macro being involved. I just added that - Hopefully that would reopen the question – James Franco Apr 16 '15 at 17:42

1 Answers1

0

The initialization

template<>
boost::recursive_mutex  EffectApplication<LG_Thread_NAME>::mResource;

still is not correct, since you initialize mResource not for all template parameter, but only for specialization with LG_Thread_NAME. So I guess when you try to access it with some another template parameter you've got an error.

Correct way is like this

template<LG_Thread Thread>
boost::recursive_mutex  EffectApplication<Thread>::mResource;

Also I'm not sure with my answer because the error says that the problem is with LT_Wrapper class and template parameter (LT_Wrapper::LT_Thread)0 looks strange to me.

Nikolay K
  • 3,770
  • 3
  • 25
  • 37