0

I have this cpp file which contains 2 arrays:

const float arr[123]={/*..*/};
const float ave[213]={/*..*/};

And in my main.cpp I have:

int main()
{
   extern float arr[123];
   float temp[123];
   for(unsigned i=0; i<123;i++)
      temp[i]=arr[i];
   return 0;
}

When I build my project I got an error saying that there’s no definition for arr. It’s a linker error.

What could be the problem?

  • 1
    Some reading on what you ran up against: [Why does const imply internal linkage in C++, when it doesn't in C?](https://stackoverflow.com/questions/998425/why-does-const-imply-internal-linkage-in-c-when-it-doesnt-in-c) – user4581301 May 04 '21 at 15:26

1 Answers1

2

Your const variable has internal linkage by default.
You need to declare it as extern as well to counteract that.

JDługosz
  • 5,592
  • 3
  • 24
  • 45