Ubuntu17.04下用CMake编译LLVM/Clang3.4.2时GCC崩溃求助
cc1plus被Killed的问题 Hey there! That c++: internal compiler error: Killed (program cc1plus) error you're hitting is super common when compiling big projects like LLVM, and it almost always boils down to your system running out of memory.
为什么会出现这个错误?
When you use make -j without specifying a number, it spins up as many compile threads as your CPU has cores. Compiling LLVM (especially older versions like 3.4.2) is memory-heavy—each cc1plus process (GCC's C++ frontend) eats up a lot of RAM. Once your system's physical memory is maxed out, Linux's OOM (Out of Memory) Killer kicks in and terminates the most memory-hungry process to save the system, which in this case is that cc1plus instance.
解决方法,按优先级排序:
限制编译线程数
别直接用make -j,根据你的可用内存指定一个更小的线程数。比如如果是4GB内存,试试make -j2;如果是8GB内存,make -j4会更稳妥。这样能避免系统瞬间被内存占满。添加临时交换空间
如果物理内存不足,创建交换文件可以作为内存的补充。以下是创建4GB交换文件的步骤(可根据需求调整大小):sudo fallocate -l 4G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile编译完成后,你可以清理掉这个交换文件:
sudo swapoff /swapfile sudo rm /swapfile编译前释放系统内存
关闭后台不必要的程序——比如开了几十个标签页的浏览器、IDE、媒体播放器等。每一点空闲内存对编译大型项目都有帮助。用更低的优化等级编译(最后备选)
这会让编译出的LLVM运行速度变慢,但能减少编译时的内存占用。执行CMake时指定调试或半优化的构建类型:cd ${LLVM_BUILD_DIR} && cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ${LLVM_SRC_DIR}(RelWithDebInfo模式会保留一些优化和调试信息,内存占用比完全的Release模式低很多)
额外提示
LLVM 3.4.2是比较老的版本,虽然它和Ubuntu 17.04自带的GCC 6应该能兼容,但可能存在一些小的兼容性问题——不过你当前的错误肯定是内存相关的。先解决这个问题,之后如果遇到其他编译错误,再针对性排查即可!
内容的提问来源于stack exchange,提问作者OrenIshShalom

