Looking for answers to this question did reveal a few replies, which made not much sense to me as a beginning C programmer.
I program Arduinos, most of which are connected to a network, and most of which include a temperature sensor to derive a MAC address from. They all publish messages via MQTT.
My .cpp file looks like this:
/*
Some description
*/
/* --------------- Configuration file --------------------------------------- */
#ifndef CONFIG_H
# include "config.h"
#endif
/* --------------- Libraries ------------------------------------------------ */
#include <Arduino.h> // basic Arduino definitions
#include <avr/wdt.h> // watch dog
#include <stdbool.h> // boolean definition
#include <SPI.h> // Serial Peripheral Interface Bus
#include <Ethernet.h> // Ethernet library for EtherShield
#include <PubSubClient.h> // MQTT client library
#include <OneWire.h> // OneWire library for Dallas DSB etc.
// ----- global variables
...
/* --------------- Instantiate global objects ------------------------------- */
EthernetClient ethernet_client; // Ethernet
PubSubClient mqtt_client (ethernet_client); // instantiate MQTT with ethernet_client
/* --------------- Prototypes ----------------------------------------------- */
// common static code modules
void mac_address_build (uint8_t pin);
void ethernet_connection_maintain ();
void bicolour_led_set (uint8_t ledState);
char *deblank (char *str);
void mac_address_publish ();
void message_publish (const char *topic, const char *msg);
void error_publish (uint8_t error_type, char *error_msg);
void temperature_collect ();
float temperature_get (uint8_t pin);
float temperature_validate (uint8_t pin, float temp_sensed);
void temperature_publish (float temp);
// common code but modify as required
bool b_connected_to_broker ();
void mqtt_call_back (char *topic, byte *payload, uint16_t length);
// controller specific functions
void meter_pulse_counter_isr();
void meter_values_calculate();
void meter_values_publish (float litres_total);
void soil_permittivity_measure();
/* --------------- Function declarations ------------------------------------ */
// as prototype above
void setup ()
{
}
void loop ()
{
}
I thought that simply putting the "common static code modules" and put them into a standard_functions.cpp, and include it after the config.h, like so:
/* --------------- Standard functions --------------------------------------- */
#ifndef STD_FUNCTIONS
#define STD_FUNCTIONS
# include "standard_functions.cpp"
#endif
However, I then get error messages that the identifiers are not declared.
What I'd like to achieve is to have these common modules in a separate file, which I include in main.cpp, so that I can maintain a single include file for all other programs I might write.
I read: Should I use #include in headers? How to split this into header and source files? How to split code into multiple files
... which I could not apply to my 'problem'. One even suggesting I need to create another .h file (in addition to the .cpp).
As for terminology, I assume .h stands for header file, but which can have any other name as long as it ends in .h?!
Is it feasible, common practice to have my 'standard_functions' contained in one file (.cpp or .h) or do I need a standard_functions.h and .cpp? What is in each? When I create the standard_function.cpp and put the functions in I get some 70 identifier not defined messages. However, when I rename the standard_functions.cpp to .h I get only 1 error messages, which seems to indicate this to be the better name. The error is variable or field 'mac_address_build' declared void; though the code compiles error free if all is on one file. However, when moving this function to second place, more errors pop up about unidentified identifiers.
I split the files various ways and the error messages kept piling up. So yes, I didn't 'get it'.