From b0af33a61b93eec61242b45422529dd466147e9e Mon Sep 17 00:00:00 2001 From: Nyq <623434+nyq@users.noreply.github.com> Date: Tue, 15 Aug 2023 04:50:07 -0400 Subject: [PATCH] Update miniz_tdef.c to enable compiling in forced-C++ mode Summary: Merged definition of static const mz_uint s_tdefl_num_probes[11] with its declaration to avoid compilation error when compiling in forced-C++mode Details: When miniz_tdef.c is compiled in C++ mode (either by forcing the compiler to treat the input as C++ or by renaming the file into miniz.cpp), MSVC17 produces the following error: ``` miniz_tdef.cpp(2113,22): error C2086: 'const mz_uint s_tdefl_num_probes[11]': redefinition miniz_tdef.cpp(1254,22): message : see declaration of 's_tdefl_num_probes' ``` This happens because in miniz_tdef.c we have the following: ``` /*Line 606:*/ static const mz_uint s_tdefl_num_probes[11]; /*Line 1465:*/ static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 }; ``` While miniz_tdef.c is a C source file and not C++, sometimes it is used in C++ projects where settings are such that mixed C/C++ compilation is not allowed and all input source files are treated as forced C++. So there would be no harm to make a small adjustment so that the source code is conformant with both C and C++ requirements. There are two ways it can be done: Option 1: change line 606 from `static const mz_uint s_tdefl_num_probes[11];` to `extern const mz_uint s_tdefl_num_probes[11];` Option2: eliminate line 1465 entirely and move line 2113 into line 1254 so that the code looks like this: ``` /*Line 606:*/ static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 }; /*Line 1465:*/ //Nothing here ``` Either option works for both C and C++ and really there is no harm in simply moving the full definition up like in option B and avoid duplication. This change implements option B. --- miniz_tdef.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/miniz_tdef.c b/miniz_tdef.c index b7ea63a..1dfcc76 100644 --- a/miniz_tdef.c +++ b/miniz_tdef.c @@ -603,7 +603,7 @@ static mz_bool tdefl_compress_block(tdefl_compressor *d, mz_bool static_block) return tdefl_compress_lz_codes(d); } -static const mz_uint s_tdefl_num_probes[11]; +static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 }; static int tdefl_flush_block(tdefl_compressor *d, int flush) { @@ -1462,8 +1462,6 @@ size_t tdefl_compress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void return out_buf.m_size; } -static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 }; - /* level may actually range from [0,10] (10 is a "hidden" max level, where we want a bit more compression and it's fine if throughput to fall off a cliff on some files). */ mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy) {