linker error C++

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
fabriceo
Respected Member
Posts: 280
Joined: Mon Jan 08, 2018 4:14 pm

linker error C++

Post by fabriceo »

Hello
I get a warning by the linker when using static variables in a c++ class:
dsp_app.cpp: Warning: Undefined reference to '_ZN7Counter5countE'

Code: Select all

class Counter { 
public:
    static unsigned count;
    Counter() { count++; }
};

void test() {
    Counter c;
    debug_printf("count %d\n", c.count );
}
FYI, I have added -std=c+11 in the XCC_FLAGS. using XTC15.3.1
this is not a template class requiring some specialisation
any suggestion ??
Thank you !
fabriceo
User avatar
xhuw
Verified
Active Member
Posts: 62
Joined: Wed May 22, 2024 2:36 pm

Post by xhuw »

I cannot reproduce this.

however according to the C++ spec https://en.cppreference.com/w/cpp/language/static.html static members should be defined out of line OR with the inline keyword:

Code: Select all

class Foo {
public:
  static unsigned count;
};
unsigned Foo::count;

// OR
class Foo {
public:
  static inline unsigned count;
};

XMOS Software Engineer

Image