LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2009, 05:50 PM   #1
BlueSkull
Member
 
Registered: Sep 2009
Posts: 73

Rep: Reputation: 17
Problem with compiling program in gcc


I compiled the following code:



#include<stdio.h>

#include<string.h>



main()

{

FILE *fi,*fo,*final;

char ch,nch,chf;

char com[100];

int i=0,count=0;

fi = fopen("chk.txt","r");

fo=fopen("two.txt","w");

ch = getc(fi);

while(ch!=EOF)

{

putc(ch,fo);

ch = getc(fi);

}

fi = fopen("chk.txt","r");

ch=getc(fi);

while(ch!=EOF)

{

while((ch>=65&&ch<=90)||(ch>=97||ch<=122))

{

com[i]=ch;

i++;

ch = getc(fi);

}

fo=fopen("two.txt","r");

nch=getc(fo);

i=0;

while(nch!=EOF)

{

if(com[i]==nch)

{

if((i+1)==strlen(com))

{

count++;

i=-1;

}



i++;

nch=getc(fo);

}

else

{

i=0;

nch=getc(fo);

continue;

}

}

final=fopen("output.txt","w");

chf=getc(final);

while(chf!=EOF)

{

chf=getc(final);

}

putc('\n',final);
for(i=0;i<strlen(com);i++)
putc(com[i],final);
putc('\t',final);
putc(count+48,final);
fclose(final);

}

}







but gcc return the error:

/tmp/ccgwEEdW.o.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status



I am using Ubuntu 9.04.
pls Help

Thanks in Advance

Aduait
 
Old 09-20-2009, 06:24 PM   #2
BlueSkull
Member
 
Registered: Sep 2009
Posts: 73

Original Poster
Rep: Reputation: 17
/tmp/ccCm7m3y.o.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
 
Old 09-20-2009, 06:25 PM   #3
BlueSkull
Member
 
Registered: Sep 2009
Posts: 73

Original Poster
Rep: Reputation: 17
/tmp/ccCm7m3y.oeh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
 
Old 09-20-2009, 11:42 PM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
use G++ mine did it too but using g++ instead of gcc fixed it
 
Old 09-21-2009, 03:01 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Please put your code between [code] and [/code]. Thanks.

Although smeezekitty's solution will work, your code is not C++ code and therefore I don't see the need to use g++.

Change the extension of your source to '.c' so gcc does not think that it's c++ code. Check man gcc for file extensions and their effects.

I've copied your code into a file xx.c and fixed two warning regarding the return value of main. Next I copied it to xx.cpp so both files are the same.
Code:
wim@btd-techweb01:~/progs$ ls -l xx*
-rw-r--r--  1 wim develop   891 2009-09-21 09:38 xx.c
-rw-r--r--  1 wim develop   891 2009-09-21 09:40 xx.cpp
wim@btd-techweb01:~/progs$ diff xx.c xx.cpp
wim@btd-techweb01:~/progs$ gcc -Wall xx.c -o xx1
wim@btd-techweb01:~/progs$ gcc -Wall xx.cpp -o xx2
xx.cpp: In function `int main()':
xx.cpp:53: warning: comparison between signed and unsigned integer expressions
xx.cpp:98: warning: comparison between signed and unsigned integer expressions
/tmp/ccewiL69.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
wim@btd-techweb01:~/progs$ g++ -Wall xx.cpp -o xx2
xx.cpp: In function `int main()':
xx.cpp:53: warning: comparison between signed and unsigned integer expressions
xx.cpp:98: warning: comparison between signed and unsigned integer expressions
wim@btd-techweb01:~/progs$ ls -l xx*
-rw-r--r--  1 wim develop   891 2009-09-21 09:38 xx.c
-rw-r--r--  1 wim develop   891 2009-09-21 09:40 xx.cpp
-rwxr-xr-x  1 wim develop 12118 2009-09-21 09:53 xx1*
-rwxr-xr-x  1 wim develop 12529 2009-09-21 09:53 xx2*
wim@btd-techweb01:~/progs$
As you can see, I did not have a problem compiling your code using gcc (xx.c). When I compiled xx.cpp with gcc it complained with the error that you have. And using g++ indeed solved that (I was too lazy to fix the 2 warnings).

Please note the extra size required when using C++.

Last edited by Wim Sturkenboom; 09-21-2009 at 03:06 AM. Reason: added ref to man gcc
 
Old 09-21-2009, 10:28 AM   #6
BlueSkull
Member
 
Registered: Sep 2009
Posts: 73

Original Poster
Rep: Reputation: 17
Now I m able to compile the file.
I compiled it using g++.

but when i run
./a.out

it gives
Segmentation fault

What sud i do.
pls Help.
 
Old 09-21-2009, 11:37 AM   #7
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Debug Either learn to use gdb or put some printf's or at strategic places (my approach).

Your problem is probably the strlen(com) as I don't see that com is a NULL terminated string.

Other mistakes that I quickly see
You don't test the value of fi and fo after calls to fopen; so if chk.txt does not exist you will happily continue.
You don't close fi and fo before re-using them.
And I think that there are a couple of other issues regarding the use of getc on a file that you open for writing. Read the man pages of all the functions that you use.

PS What is this code supposed to do?
 
Old 09-21-2009, 11:57 AM   #8
BlueSkull
Member
 
Registered: Sep 2009
Posts: 73

Original Poster
Rep: Reputation: 17
Its Incomplete Code.
Many changes r to be made yet.
I was working on a program to count the frequency of string in a file.
 
Old 09-21-2009, 09:07 PM   #9
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
i think this:
Code:
if((i+1)==strlen(com))
should be changed to this:
Code:
if(i == (strlen(com)-1))
for better readability
also remember return 0; at the end of main
and main should not be just main() it should be int main() or char main()
 
Old 09-21-2009, 09:10 PM   #10
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
i just relized something
you didnt close fi and fo
meaning you have a minor resource leak
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc errors while compiling my server.c program using MySQL linuxfan84 Programming 3 06-18-2008 02:26 AM
Problem compiling GCC 4.0.3 dustin_wielenga Linux From Scratch 3 01-15-2008 11:02 PM
gcc compiling problem chiahsin Linux - Software 2 12-02-2004 06:33 PM
GCC compiling problem webwolf70 Linux From Scratch 3 09-25-2004 08:51 PM
Compiling a program with gcc on Solaris (I think) phekno Programming 2 09-15-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration