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?
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.
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 ...
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With