I recently encountered this issue trying to build a fairly noddy app on an old Raspberry Pi2 (512MB RAM) - Internal Compiler Error
After waiting ages for a build to finish (>12 hours!!) only to see a Compiler error reported, I found that enabling a swapfile helped.
Raspberry Pi Forum - C++ internal compile error
Note, it’s never a good idea to have aswapfile on an SD card (it wares out their little write-cycles in no-time), but it’s OK to do it a few times.
Restrict your (re-)builds (I ended-up writing a little shell script to turn-on, make, then turn-of the swapfile)