1

I'm trying to make something like this:

class A
{
    static pthread_mutex_t m;
public:
    template<typename T>
        static void TestFunc(T t) {
            pthread_mutex_lock(&m);
    }
    static void Test()
    {
            TestFunc(13);
    }
};

But receive linker error:

/tmp/cc1HN0fI.o: In function void A::TestFunc<int>(int)': TReaderThread.cpp:(.text._ZN1A8TestFuncIiEEvT_[_ZN1A8TestFuncIiEEvT_]+0xc): undefined reference toA::m' collect2: error: ld returned 1 exit status make: *** [all] Error 1

It seems that this error happens only when TestFunc is template function. It is small peace of code, I need that TestFunc was template and my mutex was static variable. Is it possible to solve this issue in template approch? P.S. Realy I need to do - implement tracer as singlton(with syncronization for output descriptor).

1
  • You defined m, correct? Commented Nov 23, 2015 at 21:11

2 Answers 2

1

Your static pthread_mutex_t m; is declared but is not defined. Add the following line in your *.cpp implementation file:

pthread_mutex_t A::m;
Sign up to request clarification or add additional context in comments.

Comments

0

Template has got nothing to do with the linker error you are getting. What you did inside the class is a declaration. You need to define the static variable outside the class so that memory is allocated and initialized.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.