Not only code, but placing DATA in header is also a bad idea.
You can use same style, but data may be duplicated.
For example, take attention to TEXT section in compiler's output below.
Header file with data:
#ifndef SAMPLE_DATA_H_
#define SAMPLE_DATA_H_
#include <stdint.h>
static const uint8_t const data_array[10240] = { 1, 2, 3 };
#endif /* SAMPLE_DATA_H_ */
Data file not used anywhere and excluded by compiler:
Invoking: Cross ARM GNU Print Size
arm-none-eabi-size --format=berkeley "test-f4.elf"
text data bss dec hex filename
13864 476 1588 15928 3e38 test-f4.elf
Finished building: test-f4.siz
Header file included in only one C-file:
#include "sample-data.h"
#include "stm32f4xx_hal.h"
void method1(void) {
uint8_t b = 0;
for (uint32_t i = 0; i < sizeof(data_array); i++) {
if (data_array[i] != 0) {
b = 1;
}
}
if (b == 1) {
__HAL_RCC_GPIOB_CLK_ENABLE();
}
}
Invoking: Cross ARM GNU Print Size
arm-none-eabi-size --format=berkeley "test-f4.elf"
text data bss dec hex filename
24192 476 1588 26256 6690 test-f4.elf
Finished building: test-f4.siz
22:10:51 Build Finished (took 1s.432ms)
Header file also included to second C-file:
#include "sample-data.h"
#include "stm32f4xx_hal.h"
void method2(void) {
uint8_t b = 0;
for (uint32_t i = 0; i < sizeof(data_array); i++) {
if (data_array[i] != 0) {
b = 1;
}
}
if (b == 1) {
__HAL_RCC_GPIOB_CLK_ENABLE();
}
}
Invoking: Cross ARM GNU Print Size
arm-none-eabi-size --format=berkeley "test-f4.elf"
text data bss dec hex filename
34520 476 1588 36584 8ee8 test-f4.elf
Finished building: test-f4.siz
22:13:04 Build Finished (took 1s.487ms)
And now only data's declaration in header:
#ifndef SAMPLE_DATA_H_
#define SAMPLE_DATA_H_
#include <stdint.h>
extern const uint8_t const data_array[10240];
#endif /* SAMPLE_DATA_H_ */
and data's definition in C source file:
#include "sample-data.h"
const uint8_t const data_array[10240] = { 1, 2, 3 };
Using this declaration in only one C file (same code) give next output:
Invoking: Cross ARM GNU Print Size
arm-none-eabi-size --format=berkeley "test-f4.elf"
text data bss dec hex filename
24192 476 1588 26256 6690 test-f4.elf
Finished building: test-f4.siz
22:21:10 Build Finished (took 1s.395ms)
Also include in second C source file:
Invoking: Cross ARM GNU Print Size
arm-none-eabi-size --format=berkeley "test-f4.elf"
text data bss dec hex filename
24280 476 1588 26344 66e8 test-f4.elf
Finished building: test-f4.siz
22:22:19 Build Finished (took 1s.268ms)
And now imagine for a minute that the data structure is a font size of 200 KB ...
Does the Arduino's compiler solve this duplication problem automatically?