36

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?

Alex
  • 14,973
  • 13
  • 59
  • 94
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
  • Good idea to add a couple of other flags when compiling. `-Werror -Wall -Wextra -pedantic` This will take warnings (logical errors in your thinking) and point them out to you. – Martin York Jul 08 '20 at 08:17

8 Answers8

68

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.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
  • That's dangerous. You shouldn't call a compiled file `*.o`. That's for object files and you didn't add the `-c` flag. The binary name should be `hi` in this case, not `hi.o`. – bitmask Apr 15 '12 at 12:18
  • @bitmask It's not necessarily dangerous as file extension has nothing to do with it, you can name it whatever you want. – Jesus Ramos Apr 15 '12 at 16:53
  • It was, because other build systems could mistake it for a proper binary. – bitmask Apr 15 '12 at 17:06
16

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

Or use make: make hi

Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75
6

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.

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 3
    Optimizing a "hello world" program with -O4? Really? Also `man g++` doesn't need to be piped through `less`. – user229044 Aug 10 '11 at 14:00
  • 3
    @meagar, OP doesn't want to just write and stop at hello world. I suggested him to use this for future learning. – iammilind Aug 10 '11 at 14:06
4

For a simple hello-world project, calling the compiler directly with g++ command or creating a make file are good options as already answered:

g++ -o hi hi.cpp

or

# After creating the makefile  
make hi

For serious 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 -B <path_to_build_folder> -S <path_to_source_folder>
cmake --build <path_to_build_folder>
cd <path_to_build_folder>; ./hi

This allows the project to scale easily with libraries, sources, unit-tests, 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
Adrian Maire
  • 14,354
  • 9
  • 45
  • 85
3

Try this:

g++ -o hi hi.cpp

gcc is only for C

dimme
  • 4,393
  • 4
  • 31
  • 51
0

$ 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

0
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
Siva Prakash
  • 4,626
  • 34
  • 26
0
  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