Skip to main content Skip to local navigation

C++ and the LPC802: type casting

It appears that the C++ compilers in MCUXpresso don't like the C-style type casting that are found in some macros used by the SDK for the LPC802.

One of our students, Richard, discovered that a change needs to be done to a pair of macros that reside inside of fsl_clock.h in order for C++ to compile the examples we discussed in class this week.

It appears that C++ wasn't liking the C-style casting to 32 bit unsigned integer within the macros (http://www.cplusplus.com/doc/tutorial/typecasting/).  Thanks Richard for looking into this!  Here is his note:

Using the latest version of MCUExpresso and the latest SDK for the 804 board, I noticed any C++ project was not able to compile due to errors in `fsl_clock.h`. I was able to resolve it by replacing the line
#define CLK_MUX_DEFINE(reg, mux) (((((uint32_t)(&((SYSCON_Type *)0U)->reg)) & 0xFFU) << 8U) | ((mux)&0xFFU)) With #define CLK_MUX_DEFINE(reg, mux) (((offsetof(SYSCON_Type, reg) & 0xFFU) << 8U) | ((mux)&0xFFU)) And replacing #define CLK_DIV_DEFINE(reg) (((uint32_t)(&((SYSCON_Type *)0U)->reg)) & 0xFFFU) With #define CLK_DIV_DEFINE(reg) (offsetof(SYSCON_Type, reg) & 0xFFU) Which resolved all compilation errors due to the built in offsetof macro.