Compiling ArduPilot with more recent compilers?

Ardupilot traditionally relies on a very mature 4.9 version of gcc/c++. Since then, gcc has improved vastly when looking at warnigns generated for common coding errors. Personally, I would really like to profit from these improvements, but this is currently not possible, as AP simply doesn’t compile when using e.g. gcc 7.1.0.

The reasons are mostly due to some marginal code style issues. So most libraries include cmath instead of math.h, but still use isinf() or isnan() without caring about namespace prefixing (they are in std:: now).

Are there any plans (or even work in progress) to improve the situation here? If not - are PRs welcome about this topic?

Regards,
Holger

1 Like

PR are always welcome !
And it is more that we use math.h instead of cmath :stuck_out_tongue_winking_eye:
In fact, ardupilot should compile on gcc 7 , that is the default for chibios version I think.
But the blocking point is the Nuttx version that doesn’t compile with gcc > 4.9

Newer NuttX works fine with GCC 5, 6, 7 and Clang. PX4 NuttX is using GCC 7 (GNU Tools for Arm Embedded Processors 7-2017-q4-major) for release builds.

Yes but Nuttx on ardupilot is something like 2-3 years old, that is why it doesn’t work. ^^

Yes, you’d have to bump it, but it wouldn’t be that hard to do. It’s also more like 4-5 years old corresponding to the base NuttX.

Just looked into the NuttX thing and I was able to build the px4-v3 target with GCC 7.1 here.

There are three reasons for the PX4 compile trouble, two of them coming from NuttX:

  1. one missing “const” in the vsscanf prototype, that conflicts with an internal prototype of gcc (very easy to fix locally)
  2. strtof and strtold functions are missing in our old NuttX fork, but are referenced by headers brought with gcc 7
  3. two implicit fall throughs in PX4 (fmu.cpp) (wrong comments - never mind that gcc really parses comments…)

Changes 1. and 3 are no-brainers, 2. requires to backport of the missing str2x functions, i.e. adding two C files plus two prototyes to NuttX.

How to proceed? Is there a way to make a PR to our local forks of NuttX and PX4Firmware?

Just submitted some PRs against the main ArduPilot repo (https://github.com/ArduPilot/ardupilot/pull/7947), as well as two more against our forks of PX4Firmware (https://github.com/ArduPilot/PX4Firmware/pull/85) and PX4Nuttx (https://github.com/ArduPilot/PX4NuttX/pull/13).

Compiling with a more recent gcc has turned up a couple of incorrect usages of snprintf() on too small buffers, which I have fixed in the PR.

There is one more issue which I did not fix (yet), because I do not really understand the intention of the author (no break in libraries/AP_Beacon/AP_Beacon_Marvelmind.cpp:330).

After resolving these issues, ArduPilot compiles fine for all vehicles with GCC 7.2.1 on both ChibiOS and PX4-NuttX.

nice ! thanks for that, I will try to review that as soon as possible. I just remember another thing that prevent us to use more recent gcc, it was the binary size. gcc > 5.4 tend to produce bigger binary and we didn’t want to get ouside the 1mb limit of some old pixhawk. This was solve recently with removal of some optional feature on px4-v2.
However, changing default gcc target will be subject on big talk I think… but it is good to be able to fix the compilation. Thanks again for looking into that

Regarding binary size: I was very curious how the newer compilers perform here. Therefore I have built for all vehicles and for both px4 and ChibiOs and recorded the output:

arm-none-eabi-gcc-4.9.3 fmuv3

BUILD SUMMARY
Build directory: /home/holger-local/projects/uav/ardupilot-master/build/fmuv3
Target               Text    Data  BSS     Total  
--------------------------------------------------
bin/ardurover        789816  3452  193776   987044
bin/antennatracker   662472  4608  192624   859704
bin/arducopter       938280  3468  193768  1135516
bin/arducopter-heli  923928  3468  193768  1121164
bin/arduplane        922024  3456  193776  1119256
bin/ardusub          800072  3480  193752   997304


arm-none-eabi-gcc-7.1.0 (fc27) fmuv3

BUILD SUMMARY
Build directory: /home/holger-local/projects/uav/ardupilot-master/build/fmuv3
Target               Text    Data  BSS     Total  
--------------------------------------------------
bin/ardurover        778492  3716  193516   975724
bin/antennatracker   650636  4872  192364   847872
bin/arducopter       925468  3732  193508  1122708
bin/arducopter-heli  911004  3732  193508  1108244
bin/arduplane        909340  3720  193516  1106576
bin/ardusub          788284  3744  193492   985520


arm-none-eabi-gcc-7.2.1 (arm) px4-v3

BUILD SUMMARY
Build directory: /home/holger-local/projects/uav/ardupilot-master/build/fmuv3
Target               Text    Data  BSS     Total  
--------------------------------------------------
bin/ardurover        777884  3724  193516   975124
bin/antennatracker   650028  4880  192364   847272
bin/arducopter       924860  3740  193508  1122108
bin/arducopter-heli  910396  3740  193508  1107644
bin/arduplane        908732  3728  193516  1105976
bin/ardusub          787676  3752  193492   984920







arm-none-eabi-gcc-4.9.3 px4-v3

BUILD SUMMARY
Build directory: /home/holger-local/projects/uav/ardupilot-master/build/px4-v3
Target               Text     Data  BSS    Total  
--------------------------------------------------
bin/ardurover         931776  2868  52280   986924
bin/antennatracker    811576  4024  49720   865320
bin/arducopter       1074536  2876  58680  1136092
bin/arducopter-heli  1060160  2876  58552  1121588
bin/arduplane        1064608  2872  57400  1124880
bin/ardusub           942752  2892  52408   998052


arm-none-eabi-gcc-7.1 (fc27) px4-v3

BUILD SUMMARY
Build directory: /home/holger-local/projects/uav/ardupilot-master/build/px4-v3
Target               Text     Data  BSS    Total  
--------------------------------------------------
bin/ardurover         919984  2868  52348   975200
bin/antennatracker    799744  4024  49724   853492
bin/arducopter       1060792  2876  58748  1122416
bin/arducopter-heli  1046288  2876  58556  1107720
bin/arduplane        1051992  2872  57468  1112332
bin/ardusub           930640  2892  52412   985944


arm-none-eabi-gcc-7.2.1 (arm) px4-v3

BUILD SUMMARY
Build directory: /home/holger-local/projects/uav/ardupilot-master/build/px4-v3
Target               Text     Data  BSS    Total  
--------------------------------------------------
bin/ardurover         918968  2868  52348   974184
bin/antennatracker    798744  4024  49724   852492
bin/arducopter       1059776  2876  58748  1121400
bin/arducopter-heli  1045248  2876  58556  1106680
bin/arduplane        1050976  2872  57468  1111316
bin/ardusub           929616  2892  52412   984920

@hsteinhaus Great work. Thanks.

I’ve tagged the main issue for the next dev call. I know it’s a lousy time, but you’re always welcome to the dev call.

The cherry on the top of these PR’s would be a PR to also update the wiki, and another to update the set up scripts for each platform :wink: :slight_smile: :wink:

Hi Luis,

thanks for the invitation to the dev call, but I am afraid I cannot attend (at least this time) without seriously affecting my day job :wink:

Will look after the scripts and the wiki in the next days.

Holger

Was talking with @tridge about this, and we need some extensive flight testing to clear out any “possible” runtime issues :wink:

thanks for your work on this Holger.
I prefer using the newer gcc versions with ChibiOS as gdb support is so much better, so this makes the transition to ChibiOS easier as developers don’t need to switch compilers

I did a few flights today, one of them for about 40mins and containing lots of flight modes including a longer Auto section.

Configuration notes

  • cube 2.1
  • Copter, fmuv3 binary (ChibiOS)
  • motors connected to FMU ports (quad)
  • S.Bus radio (Crossfire) and telemetry
  • Tested modes: Stabilize, AltHold, PosHold, (New) Loiter, Auto

Issues:

  • no signal tones on flight mode switching, compass calibration and on reached way points (probably not yet implemented in ChibiOs build)
  • somewhat strange waypoint turning behaviour - probably a tuning issue of my crash-test airframe
  • NLon = 3 at one occasion while arriving at a waypoint (0 long lops otherwise after run up completed)

Log available on request, as I accidentially enabled high rate IMU logging (400MB). BTW, there seems to be zero log drops with high rate IMU logging on a normal TransCend consumer grade 16GB card - the first time ever!

Holger