Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile a c++ program in Linux?

Tags:

c++

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?

like image 227
Chankey Pathak Avatar asked Aug 10 '11 03:08

Chankey Pathak


People also ask

Can I write C program in Linux?

GCC compiler is used to compile and run a C program on the Linux operating system. Visual Studio Code editor comes with a pre-integrated terminal, so it is easy to run and compile C programs in Linux on the Visual Studio Code editor.

How to compile and run C program in Linux?

Step 1: You write your program and save the file with a .c extension. For example, my_program.c. Step 2: You compile the program and generate the object file using gcc compiler in a terminal like this: Step 3: You run the generated object file to run your C program in Linux: This was just the quick summary on how to compile ...

Is it easy to write C programs in Linux?

Therefore, if you know the C language, it is much easier to learn, write programs and run other programming languages ​​on Linux operating systems such as C ++, Java, Perl, or PHP, as the languages ​​have certain similarities. H ere we will show the steps to install GCC compiler and how to write, compile and run a C program in Linux.

What is a compiler in Linux?

A compiler (C or C++ Compiler, etc.) is a computer program that converts one programming language i.e., C/C++ code written with text into executable machine code with a linker. GNU C/C++ compilers are the one of most popular programming language in Linux operating system, but there are different programming languages for Linux too.

How to run a C program in GCC on Linux?

g++ is the GNU C++ Compiler from the GCC. In order to run a C Program, You must have installed Development Tools packages on your Linux system. Run one of the following commands to install development tools packages as per your operating system. For the example, I have selected C hello world program.


1 Answers

Use g++

g++ -o hi hi.cpp 

g++ is for C++, gcc is for C although with the -libstdc++ you can compile c++ most people don't do this.

like image 94
Jesus Ramos Avatar answered Oct 04 '22 10:10

Jesus Ramos