C/C++ Question Wrapping a library with 2 pairs of .h and .cpp files

janderkan

Well-Known Member
Licensed User
Longtime User
A common CAN.h file looks like this:

C++:
#ifndef CAN_H
#define CAN_H
#ifdef ARDUINO_ARCH_ESP32
#include "ESP32SJA1000.h"
#else
#include "MCP2515.h"
#endif
#endif[
/CODE]

And in the 2 referenced .h files are a common definition of CAN:

[CODE=cpp]
extern MCP2515Class CAN;
   
extern ESP32SJA1000Class CAN;

In Arduino i just do :

C++:
#include <CAN.h>

after that I can use the CAN object directly.

But I cannot figure out how to do it in a wrapper !

Jan
 

janderkan

Well-Known Member
Licensed User
Longtime User
In my .h file I use this:
C++:
    private:
        MCP2515Class canbus;
and it works.
But this does not work:
C++:
    private:
        CAN canbus;
The error is:
Code:
rCAN.h:12:4: error: 'CAN' does not name a type
    CAN mycan;
 

janderkan

Well-Known Member
Licensed User
Longtime User
I figured it out, I do not need the declaration in my .h file.
I can just use CAN in my .cpp file.

Jan
 
Top