g++ stops with 'Killed (program cc1plus)' during make -j
g++: internal compiler error: Killed (program cc1plus)Short answer
Killed means the kernel ended the compiler because the machine ran out of memory, so it is not a bug in your code. Many parallel cc1plus processes each use a lot of RAM. Build with fewer jobs, add swap space, or compile the heavy files with less optimisation.
Fix it fast
1Find out which cause you have
Check the kernel log for an out-of-memory kill at the time the build died, and see how much memory and swap the machine has.
sudo dmesg | grep -i -E 'out of memory|killed process' free -h2Match what you see, then jump to its fix
What does the error look like?
g++: internal compiler error: Killed (program cc1plus)Killed (program cc1plus)
Cause 1 of 3
Too many parallel compile jobs for the RAM
1Check
make -j N runs N recipes at once, and each one starts its own cc1plus. With no number, make sets no limit. The kernel's OOM killer picks a large memory user to sacrifice when memory runs out.
nproc
free -h2Fix
Run fewer jobs, or let make stop starting new jobs while the load average is high with -l.
make -j2
make -j8 -l 43Verify
Repeat the build and watch the kernel log stay quiet.
make -j2
sudo dmesg | grep -i 'killed process' || echo noneConfirmed by gnu.org
Cause 2 of 3
There is no swap to absorb a spike
1Check
The kernel calls the OOM killer only when reclaiming pages fails, and having swap left is one of the checks that can rule it out. A small VM with no swap hits it quickly.
swapon --show2Fix
Create a swap file with mkswap --file, then enable it with swapon. Check swapon(8) for the restrictions on swap files.
sudo mkswap --size 4G --file /swapfile
sudo swapon /swapfile3Verify
Swap now shows in the listing.
swapon --showConfirmed by man7.org
Cause 3 of 3
One huge source file needs more memory than the rest
1Check
Optimising compilation takes a lot more memory for a large function, and -fipa-pta can use excessive memory on large compilation units. Check the flags in the build.
grep -rn -E -- '-O[0-9s]|-fipa-pta' Makefile* CMakeLists.txt 2>/dev/null | head2Fix
Build that file with a lower optimisation level, and remove -fipa-pta if the build sets it.
make CXXFLAGS='-O1'3Verify
The file compiles to the end.
make -j1Confirmed by gcc.gnu.org
How do I stop it coming back?
- Pick -j from the memory you have, not only from the core count.
- Keep a swap file on small build machines.
- Read dmesg after a Killed message before you change any code.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Servers
yarn build is killed with exit code 137 in Bitbucket Pipelines
exit code 137
Exit code 137 means the build process was killed, and in Pipelines the usual reason is memory.
3 causes, each with a fixServers
dnf cannot install MySQL on Amazon Linux 2023
dnf cannot find a mysql-server package on Amazon Linux 2023
Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.
3 causes, each with a fixServers
Ansible hangs, then times out waiting for the sudo prompt
a playbook using become seems to hang
Ansible ran sudo on the target and never saw the answer it expected.
3 causes, each with a fixServers
Apache on Ubuntu returns 404 Not Found for my site
The requested URL was not found on this server.
On Ubuntu a 404 from Apache usually means the request maps to a path that has no file.
3 causes, each with a fixServers
Apache returns 504 Gateway Timeout when PHP takes too long
Quick requests work, slow ones end in a 504 after a fixed wait
A 504 from Apache means the backend did not answer within the proxy timeout.
2 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fix