eklmp
eklmp

Reputation: 201

Problem occurred while compiling a cpp program in gcc

My code is

#include<iostream>   

using namespace std;  
int main()  
{  
   cout <<"hi";  
   return 0;  
}

I compile it with:

gcc d.cpp -o d  

Error as follows

Undefined                       first referenced
 symbol                             in file
std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator [](unsigned int) const/var/tmp//ccyDVhhA.o
std::cout                           /var/tmp//ccyDVhhA.o
std::ios_base::Init::~Init()        /var/tmp//ccyDVhhA.o
std::ios_base::Init::Init()         /var/tmp//ccyDVhhA.o
__gxx_personality_v0                /var/tmp//ccyDVhhA.o
std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_tr aits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)/va r/tmp//ccyDVhhA.o
std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() c onst/var/tmp//ccyDVhhA.o
ld: fatal: Symbol referencing errors. No output written to d
collect2: ld returned 1 exit status

What is the mistake in the above code?

Upvotes: 2

Views: 1282

Answers (3)

Mahesh
Mahesh

Reputation: 34605

g++ d.cpp -o d 

Should use g++ for a c++ program compilation to link to C++ standard library.

Upvotes: 1

hhafez
hhafez

Reputation: 39750

that code should compile fine with a c++ compiler, however you are compiling it using gcc (a c compiler) need to use g++

Upvotes: 1

hrnt
hrnt

Reputation: 10142

You should compile your C++ programs with g++, not gcc. There is no mistake in the code, but if you use gcc with those parameters then your program will not be linked against the C++ standard library.

Upvotes: 3

Related Questions