Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running 'gcc' on C++ source file on Linux gives "cc1plus: out of memory allocating ..." error message

Tags:

c++

linux

gcc

I encountered a puzzling problem when compiling a C++ source file using 'gcc' on Ubuntu. Having solved the problem I would like to publish it here to save others the headache of solving it.

For the sake of this report we have the simplest possible C++ "Hello, World" program, stored in main.cpp:

#include <stdio.h>

int main (int argc, char *argv[])
{
    return 0;
}

When I run the command:

gcc main.cpp

I get the error message:

cc1plus: out of memory allocating 1677721600 bytes after a total of 475136 bytes

I verified I was compiling for the correct bittage (i.e., 32-bit). What was I doing wrong?

like image 355
Moshe Rubin Avatar asked Dec 21 '14 09:12

Moshe Rubin


People also ask

How do I compile a C program using GCC?

Syntax: gcc [-c|-S|-E] [-std=standard] Example: This will compile the source.c file and give the output file as a.out file which is default name of output file given by gcc compiler, which can be executed using ./a.out. gcc source.c. Most Usefull Options with Examples: Here source.c is the C program code file.

What are the GCC options in Linux?

GCC Options in Linux Environment Options Description Gcc –c Compiles source files to object files wi ... gcc –Idir Includes the directories of header files gcc –llib link the code with the library files gcc -o output file Build the output generated to output fil ... 3 more rows ...

What is the default name of output file given by gcc compiler?

Example: This will compile the source.c file and give the output file as a.out file which is default name of output file given by gcc compiler, which can be executed using ./a.out Most Useful Options with Examples: Here source.c is the C program code file.

How to check GCC compiler version in Linux?

To check the default version of gcc compiler in your system, you can use the command as –version in your Linux command prompt. Let us take a simple C program and execute in Linux with the help of Linux. To Execute a C program, we need to follow three steps.


2 Answers

It turns out I had saved the C++ source file as a UTF-16 Unicode-encoded file, complete with leading Unicode Byte Order Mark (BOM) bytes at the beginning of the file. The file was saved as UTF-16 on a Windows system, committed to a version control system, then checked out to Linux. gcc does support Unicode encoded as UTF-8, but not Unicode encoded as UTF-16.

The solution was to convert the source file back to a standard, non-Unicode encoding.

like image 109
Moshe Rubin Avatar answered Nov 04 '22 08:11

Moshe Rubin


I had the same problem in compiling a C++ program with a MAKEFILE.

I found an easy way to get rid of this problem and it is delete the existing binary file from the previous compile.

I could not identify the origin of the problem but That worked for me.

like image 25
Saam Avatar answered Nov 04 '22 08:11

Saam