Correct Methods for Compiling C++ Programs on Ubuntu Linux: Transitioning from gcc to g++

Dec 04, 2025 · Programming · 11 views · 7.8

Keywords: C++ compilation | Ubuntu Linux | g++ compiler

Abstract: This article provides an in-depth analysis of common linking errors encountered when compiling C++ programs on Ubuntu Linux systems and their solutions. Through examination of a typical compilation error case, it explains why using the gcc compiler for C++ code leads to undefined reference errors and introduces the proper use of the g++ compiler. The article also discusses the role of the make tool in simplifying compilation processes and offers practical guidance for avoiding common compilation pitfalls.

Compilation Error Analysis and Solutions

When compiling C++ programs on Ubuntu Linux systems, developers may encounter linking errors such as:

/tmp/cccRNW34.o: In function `__static_initialization_and_destruction_0(int, int)':
avishay.cpp:(.text+0x41): undefined reference to `std::ios_base::Init::Init()'
...
collect2: ld returned 1 exit status

These errors indicate that the linker cannot find relevant symbols from the C++ standard library. The root cause lies in using the wrong compiler tool.

Differences Between gcc and g++

gcc (GNU Compiler Collection) is a general-purpose compiler driver primarily used for compiling C programs. When processing C++ source code, gcc does not link the C++ standard library by default, leading to the aforementioned undefined reference errors.

The correct approach is to use g++, which is specifically designed as a compiler frontend for C++. g++ automatically handles C++-specific compilation and linking requirements, including linking necessary C++ standard libraries. For example, for a source file named avishay.cpp, the following command should be used:

g++ avishay.cpp -o avishay

This command generates an executable named avishay instead of the default a.out.

Simplifying Compilation with make Tool

For more complex projects, the make tool can be used to automate the compilation process. make recompiles only necessary parts based on file timestamps and dependencies. For a single source file, a simple make command can automatically invoke the correct compiler:

make avishay

This implicitly uses g++ to compile avishay.cpp and generate the executable. The make tool also allows defining more complex compilation rules and dependencies through Makefile files.

Code Example and Explanation

Consider the following C++ code example:

#include <iostream>
using namespace std;

class A
{
private:
   int _dmember;

public:
   void func()
   {
     cout << "Inside A!! " << endl;
     cout << _dmember;
   }
};

int main()
{
    A* a = NULL;
    a->func();
    return 1;
}

Although this code contains logical errors (calling member functions on null pointers), it requires g++ for proper linking of iostream-related components from the C++ standard library during compilation.

Compilation Options and Best Practices

In addition to basic compilation commands, several useful compilation options are available:

Complete compilation command example:

g++ -Wall -std=c++11 -g -O2 avishay.cpp -o avishay

By correctly using the g++ compiler and related tools, common linking errors can be avoided, improving development efficiency.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.