Reputation: 6503
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.
Upvotes: 6
Views: 1006
Reputation: 4430
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.
Upvotes: 0
Reputation: 668
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.
Upvotes: 3