LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   GCC compiler error while compiling *.c file (https://www.linuxquestions.org/questions/programming-9/gcc-compiler-error-while-compiling-%2A-c-file-914284/)

ssvirdi 11-18-2011 02:23 PM

GCC compiler error while compiling *.c file
 
Dear Friends,

I write a program in C i.e. Hello.C

When I try to compile it by giving the following command

$ cc Hello.C
oR
$ gcc Hello.C

I always got this error

cc HELLO.C /tmp/ccB6cpfR.o: (.eh_frame+0x12): undefined reference to `__gxx_personality_v0' collect2: ld returned 1 exit status

Please help me to get out of it. Thanks

NevemTeve 11-18-2011 03:48 PM

I guess the compiler thinks your source is C++,
try the same thing with lowercase filename: hello.c

johnsfine 11-18-2011 04:01 PM

Quote:

Originally Posted by NevemTeve (Post 4527856)
compiler thinks your source is C++,

I never knew that before (.C is treated as .cpp not as .c) but I both tested it and looked it up and you are correct.

ssvirdi, if you have some reason to want to keep the name Hello.C instead of changing it to Hello.c you could compile it with
gcc -xc Hello.C

The -xc option tells gcc the type of the input file is "c". If it knows the type already, it doesn't decide the type based on the extension (the .C).

The confusing error message you got is because the gcc command invokes the linker for a .c program regardless of the type of the input file. So your .C file is compiled as a C++ module but linked as a .c program, resulting in that link time error.

That behavior must seem silly when operating on just one input file. But gcc can compile and link multiple files in one command including mixed language programs. So this behavior that seems silly with one input file makes sense for more complicated commands.

Aquarius_Girl 11-24-2011 11:28 PM

The "case" doesn't make any difference on gcc version 4.5.0 .
I just tried that with a file h.C, there weren't any problems
shown.

NevemTeve 11-25-2011 02:17 AM

But with gcc-4.3.2 it can be reproduced easily:

Code:


$ gcc hello.C -o hello
/tmp/cceGJBdd.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

$ gcc hello.C -o hello -lstdc++
(works)

$ g++ hello.C -o hello
(works)

$ ln -s hello.C hello.c
$ gcc hello.c -o hello
(works)


Aquarius_Girl 11-25-2011 02:20 AM

so, the crux is to shift to the newer gcc version. ;)

NevemTeve 11-25-2011 02:36 AM

Another option (if you are not root-user, for example) is following the standard: the extension is .c for C-language, not .C

MTK358 11-25-2011 08:24 AM

Quote:

Originally Posted by NevemTeve (Post 4533407)
Another option (if you are not root-user, for example) is following the standard: the extension is .c for C-language, not .C

Why is is only an option when you are not root?

The proper solution is to use a lowercase ".c" extension. Uppercase ".C" means C++, so don't use it for C source code.

Sergei Steshenko 11-25-2011 03:31 PM

Quote:

Originally Posted by NevemTeve (Post 4533407)
Another option (if you are not root-user, for example) is following the standard: the extension is .c for C-language, not .C

What does the issue have to do with (not) being 'root' ?

NevemTeve 11-25-2011 10:06 PM

(If you aren't superuser, then you are not supposed to install new versions of gcc, but you can still follow the naming standards and use the extension .c for C-language.)

Sergei Steshenko 11-26-2011 12:10 PM

Quote:

Originally Posted by NevemTeve (Post 4534112)
... If you aren't superuser, then you are not supposed to install new versions of gcc ...

Huh ?

I am routinely compiling from source not as 'root' user more than 370 targets and install them in a non-system directory of mt liking. Even though it's my personal machine and I have root access.

But what does it have to do with a wrong file extension in the first place ?

MTK358 11-26-2011 12:13 PM

Quote:

Originally Posted by NevemTeve (Post 4534112)
(If you aren't superuser, then you are not supposed to install new versions of gcc, but you can still follow the naming standards and use the extension .c for C-language.)

But if you are superuser, you should still change it to ".c" instead of ".C", because ".C" is the wrong extension for C code.

NevemTeve 11-26-2011 12:24 PM

(I don't think we should continue this argumentation, because basically we all suggest the same thing: using the correct file-extension.)


All times are GMT -5. The time now is 07:14 AM.