6

I'm trying to use gcc compiler in Keil IDE for stm32f103 microcontroller. I'm compiling a relatively small project with a bit of template code and a couple of pure virtual classes. No fancy C++11 stuff. So far so good.

When I compile with -w or -pedantic, project compiles just fine. But when I put -Wall I have compilation error in this part:

template <typename T, typename U>
T & round(T & value, U roundStep)
{   
    UMBA_ASSERT(roundStep > 0);

    UMBA_STATIC_ASSERT( std::numeric_limits<T>::is_integer );
    UMBA_STATIC_ASSERT( std::numeric_limits<U>::is_integer );

    T temp = value / roundStep;
    T remainder = value - temp*roundStep;

    if(remainder < roundStep/2)
    {
        value = temp*roundStep;
    }
    else
    {
        value = (temp+1)*roundStep;
    }

    return value;
}

UMBA_STATIC_ASSERT is a typical "C static assert":

#define UMBA_STATIC_ASSERT_MSG(condition, msg) typedef char umba_static_assertion_##msg[(condition)?1:-1]
#define UMBA_STATIC_ASSERT3(X, L) UMBA_STATIC_ASSERT_MSG(X, at_line_##L)
#define UMBA_STATIC_ASSERT2(X, L) UMBA_STATIC_ASSERT3(X, L)

#define UMBA_STATIC_ASSERT(X) UMBA_STATIC_ASSERT2(X, __LINE__)

The funny part is that I can't even understand the error:

compiling common_functions.cpp...
src/Common_Functions/common_functions.h: In function 'T& common_functions::round(T&, U)':
./src/Global_Macros/global_macros.h(99): warning: typedef 'umba_static_assertion_at_line_131' locally defined but not used [-Wunused-local-typedefs]
 #define UMBA_STATIC_ASSERT_MSG(condition, msg) typedef char umba_static_assertion_##msg[(condition)?1:-1]
./src/Global_Macros/global_macros.h(100): error: note: in expansion of macro 'UMBA_STATIC_ASSERT_MSG'
./src/Global_Macros/global_macros.h(101): error: note: in expansion of macro 'UMBA_STATIC_ASSERT3'
./src/Global_Macros/global_macros.h(104): error: note: in expansion of macro 'UMBA_STATIC_ASSERT2'
src/Common_Functions/common_functions.h(131): error: note: in expansion of macro 'UMBA_STATIC_ASSERT'

It differs from static assertion error which is something like 'error: size of array 'umba_static_assertion_at_line_21' is negative'. And, as far as I can tell, round function is not even called anywhere in the project.

Here are all compiler options just in case; includes to Keil folder are put there automatically by IDE:

-c -mcpu=cortex-m3 -mthumb -gdwarf-2 -MD -Wall -O0 -I./src -I./src/Modules_Config -I./src/CMSIS -I./src/SPL/inc -ffunction-sections -fdata-sections -fno-exceptions -fno-rtti  -mcpu=cortex-m3 -IC:/Keil4.72/ARM/CMSIS/Include -IC:/Keil4.72/ARM/Inc/ST/STM32F10x -DUSE_STDPERIPH_DRIVER -DUSE_FULL_ASSERT -Wa,-alhms="./lst/*.lst" -o *.o

I'm not sure what to do with this.

Amomum
  • 6,217
  • 8
  • 34
  • 62
  • @bolov - in the context of your 'duplicate', the `<` and `>` problem for the pre-processor is only relevant when commas are also present, which they're not here. – Smeeheey May 27 '16 at 12:24
  • I tried putting expression inside the UMBA_STATIC_ASSERT in (); that didn't help. And why would it appear only with -Wall? – Amomum May 27 '16 at 12:25
  • Something is very wrong here. While `-Wall` should cause all of those lines in your output to be added, the `error: note: ...` part should just be `note: ...`. They're a note attached to the warning, not any error, so those notes shouldn't be causing the compilation to fail. Which version of GCC is this? If you reduce the source code to just `#define MACRO typedef char a[];` / `void f() { MACRO }`, does that give an error on the note about the macro expansion too? –  May 27 '16 at 12:31
  • @hvd arm-none-eabi-gcc.exe (GNU Tools for ARM Embedded Processors) 5.2.1 20151202 (release) [ARM/embedded-5-branch revision 231848]. Yes, your example also produces error `src/main.cpp: In function 'void f()': src/main.cpp(21): warning: typedef 'a' locally defined but not used [-Wunused-local-typedefs] src/main.cpp(26): error: note: in expansion of macro 'MACRO'` – Amomum May 27 '16 at 12:35
  • 2
    It looks like you also have `-Werror`, treating warnings as errors. `-w` inhibits all warnings, so it's not surprising that you don't get this warning when using it. (There's no `-Wall` in your command line - did you copy it correctly?) – molbdnilo May 27 '16 at 12:40
  • @molbdnilo I copied command line that doesn't produce error; changing -pedantic with -Wall produces it. There is no -Werror in there, I'm pretty sure. There are other warnings and they doesn't produce error. I shall probably change post so there won't be any confusion. – Amomum May 27 '16 at 12:43
  • @hvd I have just tried the latest available build for win32 (5.3.1 20160307 (release) [ARM/embedded-5-branch revision 234589]); the error is still there. – Amomum May 27 '16 at 12:47
  • @Amomum The command line that fails is much more relevant to the failure than one that doesn't. (Copy and paste and be certain, don't describe or assume or be "pretty sure".) – molbdnilo May 27 '16 at 12:49
  • @molbdnilo I see your point; I have updated my post. – Amomum May 27 '16 at 12:51
  • But aside from all that: `-Wall` enables `-Wunused-local-typedefs` and, as the message says, the macro introduces an unused local typedef, so... – molbdnilo May 27 '16 at 12:55
  • Have you tried invoking the compiler from the command line and seeing if this still happens? I've had IDE bugs where it mistakes GCC's warnings for errors. – feersum May 27 '16 at 13:11
  • Trying with the compiler from [GCC ARM Embedded in Launchpad](https://launchpad.net/gcc-arm-embedded), which reports itself as exactly "gcc version 5.3.1 20160307 (release) [ARM/embedded-5-branch revision 234589] (GNU Tools for ARM Embedded Processors)" like you said, and the exact command line from your question, and my two-line source file from the comments here, it doesn't compile, because the `"./lst/*.lst" -o *.o` isn't valid, that may be misleading output by the IDE. When changing that to spell out the file names, compilation succeeds. –  May 27 '16 at 13:11
  • @hvd compiling from command line indeed produces a warning but no error. It seems to me that feersum is right: IDE output doesn't parse compiler output correctly. – Amomum May 27 '16 at 13:30
  • @feersum you appear to be right. Would you mind making your comment and answer so I can accept it? – Amomum May 27 '16 at 13:39

2 Answers2

3

Check whether the error persists when the compiler is invoked from the command line. Some IDEs may parse the compiler's output correctly and mistake warnings for errors.

feersum
  • 658
  • 4
  • 11
0

The reason for the error is pretty straightforward:

./src/Global_Macros/global_macros.h(99): warning: typedef 'umba_static_assertion_at_line_131' locally defined but not used [-Wunused-local-typedefs]

Your classic C style static assert macro works by making a typedef which will be ill-defined if the assert fails, or just unused if the assert passes. However -Wall includes -Wunused-local-typedefs, which generates a warning if you create a typedef, but don't use it. I suspect you've also turned on option to treat warnings as errors.

Stephen C. Steel
  • 4,380
  • 1
  • 20
  • 21
  • You are partially right. The reason seems to be that multiline warning message is not parsed correctly by my IDE. Compiling from command line with the same compiler option produces the same test, only "error: note" is simply "note:". There is no option for treating warnings as errors since other warnings do not produce errors. – Amomum May 27 '16 at 22:54