[c++] How to compile a c++ program in Linux?

I made a file hi.cpp and I wrote the command given below:

#include <iostream>
using namespace std;
int main ()
{
  cout << "Hello World! ";
  cout << "I'm a C++ program";
  return 0;
}

then I ran it in my RHEL 6 machine with the following command

gcc hi.cpp

and I got some errors which are as follows:

[chankey@localhost ~]$ gcc hi.cpp
/tmp/cc32bnmR.o: In function `main':
hi.cpp:(.text+0xa): undefined reference to `std::cout'
hi.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, const char*)'
hi.cpp:(.text+0x19): undefined reference to `std::cout'
hi.cpp:(.text+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, const char*)'
/tmp/cc32bnmR.o: In function `__static_initialization_and_destruction_0(int, int)':
hi.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::Init()'
hi.cpp:(.text+0x51): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc32bnmR.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
[chankey@localhost ~]$ 

What do these errors denote? My code is correct then why am I getting errors?

This question is related to c++

The answer is


$ g++ 1st.cpp -o 1st

$ ./1st

if you found any error then first install g++ using code as below

$ sudo apt-get install g++

then install g++ and use above run code


As the other answers say, use g++ instead of gcc.

Or use make: make hi


  1. To Compile your C++ code use:-

g++ file_name.cpp -o executable_file_name

(i) -o option is used to show error in the code (ii) if there is no error in the code_file, then it will generate an executable file.

  1. Now execute the generated executable file:

./executable_file_name


You have to use g++ (as mentioned in other answers). On top of that you can think of providing some good options available at command line (which helps you avoid making ill formed code):

g++   -O4    -Wall hi.cpp -o hi.out
     ^^^^^   ^^^^^^
  optimize  related to coding mistakes

For more detail you can refer to man g++ | less.


For simple test project, g++ or make standalone are good options as already answered:

g++ -o hi hi.cpp

or

make hi

For real projects, however, the usage of a project manager is required. At the time I write this answer, the most used and open-source is cmake (an alternative could be QT qmake ).

Following is a simple CMake example:

Make sure you installed cmake on your linux distribution apt-get install cmake or yum install cmake.

Create a file CMakeLists.txt (the name is important) together with your source hi.cpp

project("hi")

add_executable( hi hi.cpp )

Then compile and run as:

cmake .
make
./hi

This allows the project to scale easily with libraries, sources, and much more. It also makes most IDEs to understand the project properly (Most IDEs accept CMake natively, like kdevelop, qtCreator, etc..)

Example of CMake integration with Qt-Creator

You could also generate Visual-Studio or XCode projects from CMake, in case you decide to port the software to other platforms in the future.

cmake -G Xcode . #will generate `hi.xcodeproj` you can load on macOS

Try this:

g++ -o hi hi.cpp

gcc is only for C


g++ -o foo foo.cpp

g++ --> Driver for cc1plus compiler

-o --> Indicates the output file (foo is the name of output file here. Can be any name)

foo.cpp --> Source file to be compiled

To execute the compiled file simply type

./foo