Compilation error: 'CAN_message_t' does not name a type

Bodger62

Member
Hello,

I am getting the error mentioned in the title when I compile and link my code, which is shown below.

================================== sketch.ino ==================================

void setup() {
// put your setup code here, to run once:
Serial.begin(115200); //delay(400);
delay(400);
Initialise_CAN_Ports();
}

void loop() {
// put your main code here, to run repeatedly:
}

void setup() {
// put your setup code here, to run once:
Serial.begin(115200); //delay(400);
delay(400);
Initialise_CAN_Ports();
}

void loop() {
// put your main code here, to run repeatedly:
}

================================== can.ino ==================================

#include <FlexCAN_T4.h>

FlexCAN_T4<CAN1, RX_SIZE_16, TX_SIZE_16> Can1; /* orig RX_SIZE_256 TX_SIZE_64 */
FlexCAN_T4<CAN2, RX_SIZE_16, TX_SIZE_16> Can2; /* orig RX_SIZE_256 TX_SIZE_64 */

#define CAN_BUFFER_STORE_LEN (1024U * 8U)

struct Rx_CAN_TIME
{
int hr;
int mins;
int secs;
uint32_t ms;
uint32_t microseconds;
}
CAN_Rx_time[CAN_BUFFER_STORE_LEN];

CAN_message_t CAN_store[CAN_BUFFER_STORE_LEN];

uint32_t CAN_store_wr_idx;
uint32_t CAN_store_rd_idx;

/* ------------------------------------------------------------------ */
/* */
/* Interrupt routine to handle the received CAN messages */
/* */
/* ------------------------------------------------------------------ */
void canSniff(uint8_t bus, const CAN_message_t &msg)
{

CAN_Rx_time[CAN_store_wr_idx].microseconds = micros();

memcpy(&CAN_store[CAN_store_wr_idx], (const void *)&msg, sizeof(CAN_message_t));

CAN_store_wr_idx = (CAN_store_wr_idx + 1U) % CAN_BUFFER_STORE_LEN;

}

/* ------------------------------------------------------------------ */
/* */
/* CAN 1 interrupt Callback function */
/* */
/* ------------------------------------------------------------------ */
void can1_Sniff(const CAN_message_t &msg)
{
canSniff(1, msg);
}

/* ------------------------------------------------------------------ */
/* */
/* CAN 2 interrupt Callback function */
/* */
/* ------------------------------------------------------------------ */
void can2_Sniff(const CAN_message_t &msg)
{
canSniff(2, msg);
}

/* ------------------------------------------------------------------ */
/* */
/* Initialise the CAN hardware for ports 1 and 2 */
/* */
/* ------------------------------------------------------------------ */
void Initialise_CAN_Ports(void)
{
Can1.begin();
Can1.setBaudRate(1000000);
Can1.setMaxMB(16);
Can1.enableFIFO();
Can1.enableFIFOInterrupt();
Can1.onReceive(can1_Sniff);
Can1.mailboxStatus();
Can1.enableMBInterrupts();

Can2.begin();
Can2.setBaudRate(1000000);
Can2.setMaxMB(16);
Can2.enableFIFO();
Can2.enableFIFOInterrupt();
Can2.onReceive(can2_Sniff);
Can2.mailboxStatus();
Can2.enableMBInterrupts();
}

If I put all the code into one file it compiles and links, but I have no idea why. I would be grateful for some advice on how to remove this error, while still keeping two files.

regards,

Ray
 

Attachments

  • can.ino
    2.7 KB · Views: 16
  • sketch_nov4a.ino
    214 bytes · Views: 18
Personally I'd do it by having sketch.ino as the main file and then having can.h and can.cpp for the CAN code.
But keeping the changes to a minimum have you tried adding #include <FlexCAN_T4.h> to sketch.ino?

The header file method would be:
can.h:
C++:
#pragma once
#include <FlexCAN_T4.h>
void Initialise_CAN_Ports(void);
And then rename can.ino as can.cpp and change the #include to be #include "can.h" rather than FlexCAN
#include can.h in sketch.ino.

Any other functions in can.cpp that you want to be accessible from sketch.ino also need to be declared in the header.
If you want a variable to be visible in sketch.ino then define it as extern in the header e.g.
In can.h
extern int mySharedInt;

In can.cpp
int mySharedInt;

Any code that includes can.h can then access mySharedInt.

The #pragma once at the start of the .h serves the same use as the #ifndef / #define / #endif pattern you will see in older .h files. It tells the compiler to only include the file once for each .cpp file. Not an issue for something as simple as this but a good habit to always include even when not strictly needed.
 
Last edited:
Thanks for your reply, but after implementing your suggestions it still doesn't link. I get the following

In file included from /home/raygray/.arduino15/packages/teensy/hardware/avr/1.59.0/libraries/FlexCAN_T4/FlexCAN_T4.h:34,
from /home/raygray/Arduino/sketch_nov4a/can.h:3,
from /home/raygray/Arduino/sketch_nov4a/can.c:2:
/home/raygray/.arduino15/packages/teensy/hardware/avr/1.59.0/libraries/FlexCAN_T4/circular_buffer.h:38:10: fatal error: algorithm: No such file or directory
38 | #include <algorithm>
| ^~~~~~~~~~~
compilation terminated.

exit status 1

Compilation error: exit status 1

I think it's something to do with the path. The strange thing is that if the code is in one file it compiles and links okay.
 

Attachments

  • can.h
    72 bytes · Views: 15
  • sketch_nov4a.ino
    232 bytes · Views: 13
  • can.cpp
    2.7 KB · Views: 20
What compiler are you using? Which version of Teensy library? Anything odd with the setup?
I put your files into Arduino IDE 2.3.2 with Teensy 1.59.0 and default settings and it built without any issues.

edit - #include <algorithm> is trying to pull in a standard cpp library, if the compiler can't find that then this implies some sort of compiler installation/configuration issue. Maybe try re-installing the teensy package?
 
Last edited:
I made a new sketch and everything started working. All a bit strange but got there in the end.

Thanks for your help.
 
Back
Top