Build error: cannot cast char to uint16_t

I cannot build px4 even though I cloned fresh copy of both librobotcontrol and Firmware and followed instructions (Redirecting to latest version of document (master)) one by one.

env setup

export TOOLCHAIN_ROOT=$HOME/toolchain/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf
export CROSS_COMPILE=arm-linux-gnueabihf
export ARCH=arm 
export CC=arm-linux-gnueabihf-gcc
export CXX=arm-linux-gnueabihf-g++

NEWP=$(echo $PATH | tr ':' '\n' | grep -v "^$HOME" | tr '\n' ':') 


git clone git://
cd librobotcontrol
sed 's/gcc/arm-linux-gnueabihf-gcc/' -i `grep gcc * -Rl`
export DESTDIR=~/arm
make install
ls ~/arm


make beaglebone_blue_cross

[23/698] Building CXX object src/lib/DriverFramework/framework/src/CMakeFiles/df_driver_framework.dir/I2CDevObj.cpp.o

/home/mpletty/arm/usr/include/rc/mavlink/common/…/protocol.h: In function ‘uint16_t _MAV_RETURN_uint16_t(const mavlink_message_t*, uint8_t)’:
/home/mpletty/arm/usr/include/rc/mavlink/common/…/protocol.h:279:49: error: cast from ‘const char*’ to ‘const uint16_t* {aka const short unsigned int*}’ increases required alignment of target type [-Werror=cast-align]
{ return *(const TYPE *)(&_MAV_PAYLOAD(msg)[ofs]);}
/home/mpletty/arm/usr/include/rc/mavlink/common/…/protocol.h:281:1: note: in expansion of macro ‘_MAV_MSG_RETURN_TYPE’

Can anyone else replicate?

Looks like a warning leads to an error. Try building without -Werror here:

It should work. I found another workaround

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0bfcee70d8..fbe65b2e6a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -370,6 +370,7 @@ px4_generate_airframes_xml(BOARD ${PX4_BOARD})
 # DriverFramework
 px4_add_git_submodule(TARGET git_driverframework PATH "src/lib/DriverFramework")
 set(OS ${PX4_PLATFORM})

However, both seems awful. As far as I understand this issue comes from unused but existing function _MAV_RETURN_uint16_t which does that illegal cast from const char* to const uint16_t*. Function defined by macro _MAV_MSG_RETURN_TYPE in a conditional controlled by MAVLINK_ALIGNED_FIELDS and MAVLINK_NEED_BYTE_SWAP in librobotcontrol/include/rc/mavlink/protocol.h. Header included - never used.

Have not flight yet. Hope it works.

You’re right, it would be good to fix that in the mavlink header files if it isn’t already.

1 Like

Any news to this? I’m still getting the same error.

I mean if we don’t want to work-around. :slight_smile:

Which target are you compiling against? What toolchain is that using? I need to reproduce this again to check. Thanks.