variable 'j' set but not used
Building cryptopp-cmake v8.9.0 with cmake v3.22.1 under Android Studio on Windows 10 produces the following warning:
C/C++: ninja: Entering directory `[...]'
C/C++: [...]/cryptopp-cmake/cryptopp/validat1.cpp:1053:31: warning: variable 'j' set but not used [-Wunused-but-set-variable]
C/C++: 1053 | for (unsigned int j=0; !source.AnyRetrievable(); ++j)
C/C++: | ^
C/C++: 1 warning generated.
I'm not sure why for loop is used, probably it should have been while (!source.AnyRetrievable()) ?
It looks like this variable is only used by a human to debug if the test TestHuffmanCodes crashes. If you run the test with gdb and it crashes, you can inspect the stack to check the value of j to know the byte that caused the crash without having to print it.
Maybe this variable could be renamed to something like _j that would not trigger warning?
Sorry, I'm not familiar with your setup and it might depend on your configuration or software versions. I don't get the warning with g++ 11.4.0. Have you tried renaming the variable to _j?
Assuming the compiler you are using is also gcc/g++, if you look at gcc's documentation about the warning, I would say the compiler should not care about the specific name of the variable unless the warning is suppressed by an argument or variable attribute.
If you see the warning disappears depending on the spelling of the variable, then I guess your IDE has some sort of macro or script that changes your compiler flags depending on your code. In any case, it is safe to ignore this warning.
No, renaming j to _j didn't help, as it would in, e .g., Python.
Probably there are some C/C++ specific ways to suppress the warning?
As far as I understand Android NDK uses clang compiler for C/C++.
Probably there are some C/C++ specific ways to suppress the warning?
You have to come up with ways to re-write the code so that the compiler detects j is used... or remove the j variable.
For example, you can try to add a statement that uses j but does nothing like (void)j; in the loop body (not sure if this will work in your setup as I can't reproduce the warning with clang 14.0.0 in a mwe):
for (unsigned int j=0; !source.AnyRetrievable(); ++j) {
decoder.Decode(reader, val);
(void)j; // try to suppress warning: variable 'j' set but not used
}
or you could change the loop condition to use j as it seems it should be less than the size of data1 (0xfff):
for (unsigned int j=0; !source.AnyRetrievable() && j < sizeof(data1); ++j)
If none of those work... you have to be more creative than me.
Another option is to disable the warning from the compiler, perhaps you could add -Wno-unused-but-set-variable to the GNUMakefile to disable the warning if clang supports it (if your clang version is at least 13 according to this random github issue I found). I would try adding it to the TCOMMAND or try to find the line that compiles cryptopp/validat1.cpp and add the flag, but this Makefile is fairly complex so I can't help you more there.