0

I have an online generator used to create header files used to embed assets into firmware.

It is here: https://honeythecodewitch.com/gfx/converter

The issue is due to web considerations, it's problematic to generate both a header file and an implementation file for one bit of content.

To that end I've included the implementation of each in the header itself wrapped with a #define based guard.

// Add #define DISCONNECTED_ICON_IMPLEMENTATION
// to exactly one CPP file before including this file
#pragma once
#include <stdint.h>
#include <gfx.hpp>

extern const uint8_t disconnected_icon_data[];
extern gfx::const_buffer_stream disconnected_icon_stream;
extern const gfx::svg_doc disconnected_icon;

#ifdef DISCONNECTED_ICON_IMPLEMENTATION

const uint8_t disconnected_icon_data[] = {
... (omitted)
};
... etc
#endif // DISCONNECTED_ICON_IMPLEMENTATION

My question is two parts.

  1. Is there a better way to accomplish what I need than above and
  2. I was thinking of adding the following to the header
#ifndef DISCONNECTED_ICON_IMPLEMENTATION
#define DISCONNECTED_ICON_IMPLEMENTATION
#endif // !DISCONNECTED_ICON_IMPLEMENTATION

Such that it would automagically include the source in exactly one implementation file.

Can anyone see any issues with this in terms of causing problems with compilation or anything? Like any scenario that would break it?

  • 1
    Extremely long arrays in headers could slow down compilation, even if disabled with preprocessor. I would test it. And regarding (2), it wouldn't work, since macros are local to each translation unit. – HolyBlackCat Jul 11 '23 at 22:31
  • They do slow down compilation, but it's actually necessary to embed a lot of times under IoT, and different toolchains deal with embedded assets differently, while a header file works for all of them, so it's a livable compromise, especially considering the speed of modern PCs. Regarding 2. Doh. I should have seen that. – honey the codewitch Jul 12 '23 at 01:04
  • What I mean is, the array should probably be in a .cpp file. It should build a bit faster. – HolyBlackCat Jul 12 '23 at 06:38
  • That would be nice, but I cannot readily generate two files from a client side web app. If it was server side I could zip it, but it's not for reasons and generating only a CPP wouldn't work. – honey the codewitch Jul 12 '23 at 08:27

0 Answers0