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

Similar threads

B4R Tutorial Inline C / C++
B4R Tutorial Creating libraries for B4R
B4R Library DHT Library
B4R Tutorial Using PROGMEM to save memory
B4R Question ESP8266

janderkan

Active Member
Licensed 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

Active Member
Licensed 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

Active Member
Licensed 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