strange error in c program with gcc

Asked by prasad.ram

sir my program is comparing data in two files and my program is executed in windows and when i am compiled it gives error like
"/tmp/ccVFUQap.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
 collect2: ld returned 1 exit status"
sir i did not know what is this error please tell me what should i do and my code is given below

#include<stdio.h>
#include<stdlib.h>
main()
{
FILE *f,*f2;
static int flag;
char c,c2,fil[20],fil2[20];
printf("ENTER FIRST FILE NAME\n");
scanf("%s",fil);
printf("ENTER SECOND FILE NAME\n");
scanf("%s",fil2);
f=fopen(fil,"r");
f2=fopen(fil2,"r");
if(f==NULL||f2==NULL)
{
printf("INVALID FILE NAME\n");
exit(0);
}
c=getc(f);
c2=getc(f2);
while(c!=EOF||c2!=EOF)
{
if(c!=c2)
{
flag++;
break;
}
c=getc(f);
c2=getc(f2);
}
fclose(f);
fclose(f2);
if(flag==1)
printf("THE TWO FILES ARE NOT EQUAL\n");
else
printf("THE TWO FIELS ARE EQUAL\n");
}

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu gcc-defaults Edit question
Assignee:
No assignee Edit question
Solved by:
Eliah Kagan
Solved:
Last query:
Last reply:
Revision history for this message
Eliah Kagan (degeneracypressure) said :
#1

The problem does not seem to be in your code. (I've read through it, and also compiled it successfully. I have not actually tried running it.)

How are you compiling it? If you're compiling it from the command-line, what command are you using?

By the way, your code does have three problems, but none would cause this error:

(1) You don't use whitespace effectively. Perhaps this is an artifact of the way you posted the code here on Launchpad. But if not: You should really indent blocks! See how much easier http://paste.ubuntu.com/606936/ is to read. You can also benefit from the use of blank lines to separate code at logical points, and to separate your #include directives from your C code, but the indentation is the main thing. I had to indent it myself in order to read through it and make sense out of it.

(2) You use implicit int for main(). It's better to write "int main()". It's a matter of opinion to say that implicit int is bad...but it's been removed from later versions of the C standard (starting with C99), and using it tends to lead to problem #3.

(3) Execution can reach the closing brace of the main() function, which is not of type void. Leaving off "int" makes it seem like it might be void, but main() is always of type int (some compilers support void main(), but it's nonportable, and you have to specify void explicitly). The best fix for the problem would probably be to add "return 0;" (without the quotes) as the last statement before the closing brace.

Revision history for this message
mycae (mycae) said :
#2

__gxx_personality_v0 is part of libstdc++ (ie the C++ standard library).

It seems as if you compiled this application with

gcc main.cpp -o main

but failed to provide the link against the c++ libs.

You need to be clear as to whether your are using C or C++. This code will compile in either, but if you do not explicitly provide the linker parameter to the standard libraary

gcc main.cpp -o main -lstdc++

then you will get the error you describe.

Please post the commands you are using to compile, rather than just the error message.

The guessing for c++/c using "gcc" is based upon (at least in part), the filename, so renaming the .cpp file to .c would make gcc invoke the c compiler, rather than the c++ compiler. doing this would mean you do not need to link against c.

Revision history for this message
Eliah Kagan (degeneracypressure) said :
#3

@mycae
This is a C program, which does not use any C++ feature or refer to any symbols requiring the C++ standard library. At least with gcc/g++ 4:4.5.2-1ubuntu3 [gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2] on Natty amd64, it compiles fine with any of the following (so long as the source code file is named accordingly):

gcc -o main main.c
gcc -o main main.cpp
g++ -o main main.c
g++ -o main main.cpp

So -lstdc++ is not (ever) necessary for compiling this program, at least with my system's GCC. It seems to me that if -lstdc++ were necessary, something would have to be seriously wrong with the environment. After all, -lstdc++ is also not (ever) required for building a C++ program that relies on the C++ standard library, on an Ubuntu system with system-provided development tools. (Just as the Standard C Library is automatically linked to C programs, C++ Standard Library is automatically linked to C++ programs.)

Revision history for this message
mycae (mycae) said :
#4

Not for me. Trying to save that in "tmp.cpp" then compiling gives me his exact error.

gcc tmp.cpp -o main
/tmp/cceNYrcX.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

Revision history for this message
Eliah Kagan (degeneracypressure) said :
#5

@mycae
Interesting. I guess this makes sense, considering that (1) a program with a .cpp extension is treated as a C++ program, so C library calls may be taken to refer to the corresponding calls in the C++ library, and (2) to compile a C++ program without manually specifying includes and/or libraries, g++ (rather than gcc) must be used. Are you using a different GCC version (or platform) from mine?

@prasad.ram
Given what mycae has just said, it seems that some versions of GCC on Ubuntu don't automatically link in the right libraries when you compile C programs with .cpp instead of .c extensions. It's not really correct to give a C program a .cpp extension (unless it is also a valid C++ program). As mycae has said, if your file ends in .cpp, you should rename it to end in .c, and that should fix the problem.

Revision history for this message
mycae (mycae) said :
#6

Eliah:
Im still on maverick.
$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Revision history for this message
Eliah Kagan (degeneracypressure) said :
#7

@mycae
I just tested this on a Maverick i386 system (but with gcc/g++ 4:4.4.4-1ubuntu2 [gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5]), and I get the same error you get.

@prasad.ram
So our guess (actually it's mycae who deserves full credit for this!) is that you're invoking the computer with the "gcc" command (rather than the g++ command), but that you've named your C source code file with .cpp (or .cxx, or .C). If that's the case, try renaming it with a .c extension and attempting to compile again. If that works, you can mark this question as Solved. If our guess is wrong, or that doesn't work, please post *all* the text from the Terminal associated with the compilation, starting with beginning of the line on which you issue the compilation command. (Or if you're compiling in some way other than from the command line, describe the way you're using, in detail.)

In the future, when posting questions about compile-time errors, please include the command you used start the compilation (or, if you're not doing it in the Terminal, include a detailed description of how you are compiling).

Revision history for this message
prasad.ram (prasad-ram126) said :
#8

sir about 3rd problem i add return 0 statement but of no use .And this is a
c program so i saved with .c extension and the command for compilation is
gcc <file name >
in my terminal

the program is
      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 int main()
      4 {
      5 FILE *f,*f2;
      6 static int flag;
      7 char c,c2,fil[20],fil2[20];
      8 printf("ENTER FIRST FILE NAME\n");
      9 scanf("%s",fil);
     10 printf("ENTER SECOND FILE NAME\n");
     11 scanf("%s",fil2);
     12 f=fopen(fil,"r");
     13 f2=fopen(fil2,"r");
     14 if(f==NULL||f2==NULL)
     15 {
     16 printf("INVALID FILE NAME\n");
     17 exit(0);
     18 }
     19 c=getc(f);
     20 c2=getc(f2);
     21 while(c!=EOF||c2!=EOF)
     22 {
     23 if(c!=c2)
     24 {
     25 flag++;
     26 break;
     27 }
     28 c=getc(f);
     29 c2=getc(f2);
     30 }
     31 fclose(f);
     32 fclose(f2);
     33 if(flag==1)
     34 printf("THE TWO FILES ARE NOT EQUAL\n");
     35 else
     36 printf("THE TWO FIELS ARE EQUAL\n");
     37 return 0;
     38 }
and compilation is
ramu@ramki:~$ gcc FILC.C
/tmp/cczSK3ri.o:(.eh_frame+0x12): undefined reference to
`__gxx_personality_v0'
collect2: ld returned 1 exit status
if it executes i use the command "./a.out" for output
so i think this information is enough please tell me what is the reason

On Fri, May 13, 2011 at 8:41 PM, Eliah Kagan <
<email address hidden>> wrote:

> Your question #157328 on gcc-defaults in Ubuntu changed:
> https://answers.launchpad.net/ubuntu/+source/gcc-defaults/+question/157328
>
> Eliah Kagan posted a new comment:
> @mycae
> I just tested this on a Maverick i386 system (but with gcc/g++
> 4:4.4.4-1ubuntu2 [gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5]), and I get the
> same error you get.
>
> @prasad.ram
> So our guess (actually it's mycae who deserves full credit for this!) is
> that you're invoking the computer with the "gcc" command (rather than the
> g++ command), but that you've named your C source code file with .cpp (or
> .cxx, or .C). If that's the case, try renaming it with a .c extension and
> attempting to compile again. If that works, you can mark this question as
> Solved. If our guess is wrong, or that doesn't work, please post *all* the
> text from the Terminal associated with the compilation, starting with
> beginning of the line on which you issue the compilation command. (Or if
> you're compiling in some way other than from the command line, describe the
> way you're using, in detail.)
>
> In the future, when posting questions about compile-time errors, please
> include the command you used start the compilation (or, if you're not
> doing it in the Terminal, include a detailed description of how you are
> compiling).
>
> --
> You received this question notification because you asked the question.
>

Revision history for this message
Best Eliah Kagan (degeneracypressure) said :
#9

As I explained, the absence of a return statement at the end of main(), while wrong in this case, was *not* the cause of your problem. There was no reason for you to think that adding "return 0" would solve this problem.

"i saved with .c extension"

No, you didn't.

You saved the file with a .C extension. Since Unix-like systems, such as Ubuntu, are case-sensitive (see http://en.wikipedia.org/wiki/Case_sensitivity), this is *not* the same as a .c extension. There is exactly one extension that GCC associates with C programs, and it's .c. There are three extensions that GCC associates with C++ programs: .cpp, cxx, and .C. You have identified your program as C++ source code by naming it .C.

Change its name from FILC.C to FILC.c -- then you should be able to compile it without any problems.

By the way, due to the same case sensitivity, you might want to make the letters before the .c extension lower case as well. If you do that, then the filename would be filc.c. This way, you don't have to use capital letters when you invoke the program. In addition, programs binaries are typically names in lower-case letters (or occasionally, with an initial and possibly medial capitals, see http://en.wikipedia.org/wiki/Pascal_case). This makes it easier to identify environment variables, which are typically named upper-case. (This should not be confused with lower/upper case issues in C source code itself, where the considerations are different--there, variables are typically lower case and macros are typically upper case.)

Revision history for this message
prasad.ram (prasad-ram126) said :
#10

thank you sir my problem is solved

Revision history for this message
Eliah Kagan (degeneracypressure) said :
#11

By the way, I had neglected to mention the fourth extension (besides .cpp, .cxx, and .C) that is associated with C++ source code files: .cc