The error message is typical of the ran-out-of-memory condition.
It appears you are using ld as your linker? (it is the default on Ubuntu) Our usual next suggestion is to use gold or lld instead, as they consume less memory. There are instructions on the web for making gold your default linker.
If that is not practical, the other suggestion is to reduce the number of threads (the -j option to make or ninja). If you are doing a Debug build, likely you would need to use -j1.