Compilation warnings with Keil MDK-ARM

petersmith27 wrote on Thursday, March 03, 2011:

Keil MDK-ARM 4.14

Not sure what the following compiler warning implies, it may be a simple bug or something I am doing.

The warning is:-
…\…\…\Libraries\FreeRTOS/include/task.h(111): warning:  #368-D: class “xTASK_PARAMTERS” defines no constructor to initialize the following:

The task.h file is included from a C++ source file

Don’t understand as the extern “C” appears to be in place in the source files.

davedoors wrote on Thursday, March 03, 2011:

Do the FreeRTOS files compile ok? It might be that something should be compiling as a C file but is being incorrectly compiled as a C++ file.

richard_damon wrote on Thursday, March 03, 2011:

Sounds like the compiler is being over picky (I do note that it is just giving a warning and not an error, compilers are allowed to warn about anything they want)

structs and classes are allowed to not have constructors, and to be compatible with C, they can not have constructors (and be structs). The compiler is saying that this isn’t a good idea as the members will not be automatically initialized, and it considers this not a good practice so it wishes to warn you about it.

There are a couple of options, one is to just turn off the warnings in the compiler options. A second, if you don’t want to globally disable that warning, is to edit the file to add the appropriate pragma (assume the compiler provides one) to disable and restore the error around the piece of code giving the problem. This could be done in task.h, and need to be redone every time you update FreeRTOS, or in your source files just before including task.h, and need to be done in every file that does so (it might be worth creating a task.hpp that just does this and then includes task.h)

cpns wrote on Friday, August 26, 2011:

The problem is the

const signed char * const pcName;

member.  The complete error message in uVision is missing because it does not handle multi-line messages from its own compiler!  You need to look in the generated .lst file to see the fill message.

The semantics of const in C++ differ from C, but I am not sure why this generates a warning, it is a variable pointer to const data.  That is not to say that the data is truly constant, but that it cannot be modified through this pointer.  I think this warning can be safely disabled.

#pragma diag_suppress 368

richard_damon wrote on Friday, August 26, 2011:

The issue is that extern “C” says to use C linkages, but DOESN’T compile the contents as C code. Compilers are also free to warn about anything they feel about.

In this case, the compiler sees a structure with a const member and no constructor. It expects that the proper way to initilize the structure would be in the constructor, but you can also use direct initilzation like in C. Because you don’t have the constructor the compiler thinks it as suspicious, and so give the warning. (Note that it is defined as a const pointer to const signed char, so the element of the struct, the pointer, is const, thus you couldn’t set it with something.pcName = “foo”)