0

I am trying to create an extern object array, but I think I am having a linking problem. I have a class defined on class.hpp file, on the Declarations.cpp file I include the class header and I proceed to create an array of the class, then on my second header file I have the same array declared as extern, this header is to be included where I need to use it, then main_header.hpp file is used to initialize the extern array , so they can be used wherever main_header.hpp is included.

But I am getting:

error: undefined Reference to "myArray" on main_header.hpp

The code below is the minimal code to reproduce the issue, in here I had omitted header guards and code that is not important.

this is my setup:

class.hpp

class myClass
{
  //Class Declaration
};

/* end of file */

Declarations.cpp

#include "class.hpp"

myClass myArray[4];

/* end of file */

main_header.hpp

#include "class.hpp"

extern myClass *myArray;

/* end of file */

main_header.cpp

#include "main_header.hpp"

void setup()
{
  for(uint8_t i = 0; i < 4; i++)
  {
    myArray[i] = myClass();
    myArray[i].begin();
  }
}

/* end of file */

main.cpp

#include "main_header.hpp"

void function()
{
  setup();
  myArray[0].test();
}

/* end of file */

How can I properly declare an extern object array??

Thanks in advance!

Cheche Romo

edit: I am compiling with g++ on PSoC Creator.

edit2:

if in my Declarations.cpp I add

int var = 0;

then on main_header.hpp I add

extern int var;

an then on main cpp

int a = var;

it shows me no error for var, only for myArray.

3
  • 1
    How did you compile the program? Commented May 14, 2018 at 5:08
  • I am compiling with g++ in PSoC Creator. Commented May 14, 2018 at 5:14
  • I solved it, there was an invisible unicode character on declarations file, I don't know why did the compiler didn't warned me. Sorry for this trash question. Commented May 14, 2018 at 6:25

1 Answer 1

1

Change main_header.hpp to be:

extern myClass myArray[];
Sign up to request clarification or add additional context in comments.

3 Comments

I have already tested that, and it gives me the same error :(
You did link, declarations.obj into your final project output, right?
Yes it should be linked im compiling with g++ on PSoC creator, I mean im not sure how linking is done, but it is not the first time I use extern variables on this environment, but it is the first time I declare an extern object array.

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.