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.
This commit is contained in:
刘皓 2025-05-10 10:47:19 -04:00
parent 54d915860f
commit 215046d8b4
No known key found for this signature in database
GPG key ID: 7901753DB465B711

View file

@ -41,7 +41,7 @@ int main(int argc, char **argv) {
return 3;
}
outputf << "#include <stddef.h>\n#include <stdint.h>\nextern const uint8_t " << arrayname << "[] = {";
outputf << "#ifndef MKXPZ_EMBEDTOOL_" << arrayname << "\n#define MKXPZ_EMBEDTOOL_" << arrayname << "\n#include <stddef.h>\n#include <stdint.h>\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;
}