Karan Talasila
Karan Talasila

Reputation: 459

ld returned 1 exit status linker error

I am trying to implement a simple c++ program which takes an input string with punctuation characters and return an output string removing those punctuations. The program is

#include<iostream>
#include<cctype>
using namespace std;
int main()
{
   int index=0;
   string sequence1,sequence2;
   cout<<"enter the sequence"<<endl;
   getline(cin,sequence1);
   for(index=0;index<20;++index)
      if(!ispunct(sequence1[index]))
         sequence2[index]=sequence1[index];
   cout<<sequence2<<endl;
   return 0;
}

It gives me an error saying ld returned 1 exit status.The total error is

progprac: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here
progprac: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
progprac:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
progprac: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
progprac: In function `__data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o:(.data+0x0): first defined here
progprac: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
/tmp/ccpOfw2A.o: In function `main':
progprac.cc:(.text+0x0): multiple definition of `main'
progprac:(.text+0xe4): first defined here
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
progprac:(.dtors+0x8): first defined here
/usr/bin/ld: error in progprac(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status

I checked online that it is a linker error where you declare a function and not define it. But I haven't made such an error. What is the error?

Upvotes: 1

Views: 3898

Answers (1)

Suvarna Pattayil
Suvarna Pattayil

Reputation: 5239

You missed the -o in compile line,

g++ -o progprac progprac.cc

After resolving 1st problem,

You have created a blank string sequence2 and in your for loop you make a if check to assign value to sequence2 and you use index as a subscript for it. In some cases, (where there are punctuation marks you don't assign anything to that character position of sequence2). That can be a source of trouble as the string is still "" .

If you use sequence2.at(index) it says out of range, which means the string does not exist[no characters] at those locations.

If you use + operator, you eliminate those issues, because you concatenate the characters to your existing string (starting with "")

         sequence2 += sequence1[index];

Upvotes: 1

Related Questions