From 215046d8b49b51f2c921a1313fd71113ccc9c31f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=9A=93?= Date: Sat, 10 May 2025 10:47:19 -0400 Subject: [PATCH] Only use `extern` in embedtool.cpp if language is C++ In C, variables defined as `const` have external linkage. In C++, variables defined as `const` have internal linkage, and we need to define them as `extern const` for them to have external linkage like in C. However, in C, defining a variable as `extern const` throws a compiler error, so we need to add `extern` only for C++ files. --- tools/embedtool.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/embedtool.cpp b/tools/embedtool.cpp index caa08df8..17bd3a15 100644 --- a/tools/embedtool.cpp +++ b/tools/embedtool.cpp @@ -41,7 +41,7 @@ int main(int argc, char **argv) { return 3; } - outputf << "#include \n#include \nextern const uint8_t " << arrayname << "[] = {"; + outputf << "#ifndef MKXPZ_EMBEDTOOL_" << arrayname << "\n#define MKXPZ_EMBEDTOOL_" << arrayname << "\n#include \n#include \n#ifdef __cplusplus\nextern\n#endif /* __cplusplus */\nconst uint8_t " << arrayname << "[] = {"; uint64_t len = 0; for (;;) { @@ -53,7 +53,7 @@ int main(int argc, char **argv) { ++len; } - outputf << "};\nextern const size_t " << arrayname << "_len = " << len << "ULL;"; + outputf << "};\n#ifdef __cplusplus\nextern\n#endif /* __cplusplus */\nconst size_t " << arrayname << "_len = " << len << "ULL;\n#endif /* MKXPZ_EMBEDTOOL_" << arrayname << " */\n"; return 0; }