Undefined reference to ... when passing a byte from a struct to pinMode

Hi everyone.
I'm writing a code where I have a struct containing the pinmap of my device on a dedicated file. I then use this struct to initialize the pins and do other stuff. I import the object on another file with extern and try to set pinMode with that pin byte I saved inside this 'p_pinmap' object. This procedure for reasons I cannot understand make the compiler trow "undefined reference to 'p_pinmap' ". If I use digitalWrite or assign the member inside a new variable the complier doesn't complain.
Can somebody explain me what am I doing wrong?

//main.ino

#include "init.h"
#include "parameters.h"

extern const pins p_pinmap;

void setup() {
  //initiate the channels
  initialize();
}

void loop() {
  delay(1000);
}
//init.h

#ifndef _INIT_
#define _INIT_

void initialize();

#endif
//init.cpp

#include "init.h"
#include "parameters.h"

extern const pins p_pinmap;

void initialize(){
  pinMode(p_pinmap.LED_Clock,OUTPUT); //undefined reference
  pinMode(p_pinmap.Heater[1],OUTPUT); //undefined reference
  const byte a=5;                     //OK
  const byte b=p_pinmap.LED_Clock;    //OK
  pinMode(b,OUTPUT);                  //undefined reference
  pinMode(7,OUTPUT);                  //OK
  pinMode(a,OUTPUT);                  //OK
  
}
//parameters.h

#ifndef _PARAMETERS_
#define _PARAMETERS_

#include <Arduino.h>

/*-----PINOUT-----*/
struct pins {
  const byte Heater[4];
  const byte Temp[4];
  const byte AnPot[4];
  const byte LED_Clock;
  const byte LED_Data;
  const byte LED_Latch;
  const byte DHTPIN;
  pins():   //fill here the pin numbers from 1st channel to the last
    Heater({10,11,12,13}),
    Temp({2,3,4,5}),
    AnPot({A1,A2,A3,A4}),
    LED_Clock(6), LED_Data(7), LED_Latch(8), DHTPIN(9) {};
};

#endif
//parameters.cpp

#include "parameters.h"

const pins p_pinmap;

Put this in parameters.h after the struct declaration:

extern const pins p_pinmap;

Remove that line from you .ino file.

Const implies internal linkage, so you explicitly have to mark p_pinmap as extern in your .cpp file (e.g. by including the extern declaration in the header as gfvalvo suggested).

ok, this actually works but I don't really understand why it needs to be in the .h file if I don't use it there but I define it in the .cpp. .cpp is the compilation unit, isn't it?
This was a snippet that I shortened but the actual code that I wrote in the parameters.h contains another bigger struct that contains multiple variables used everywhere and as this p_pinmap I define it only in the .cpp and not in the .h. For that struct / object that I extern the compiler doesn't complain anywhere.

Well, actually I had the same problem with that other struct. Undefined reference. I had created only the .h file and implemented therr the definiton and declaration. Than I resolved the problem by creating the .cpp file of parameters and moving there the definition of the object. So I don't know why for this struct p_pinmap now it asks again tu put the extern definition back in the header file.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.