Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

alexmm

macrumors newbie
Original poster
May 5, 2008
9
0
Hello, when i try to compile a simple "hello world!" program using printf i got a strange error :
Undefined symbols:
"___gxx_personality_v0", referenced from:
___gxx_personality_v0$non_lazy_ptr in ccJX35B2.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

i think it comes from printf because otherwise everything work fine (i have done the #include <stdio.h>)
Thanks a lot for your help.
 

alexmm

macrumors newbie
Original poster
May 5, 2008
9
0
i have found a solution in should have name my file containing the main *.c instead of *.C. Apparently it is a problem of namespace or something like that but i don't know anything about it. I just find the solution on the net. Can anybody explain me why i had this error ?
Thanks a lot.
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
What are you doing different from the following?

edit file hello.c:

#include <stdio.h>

int main ()
{
printf ("hello world\n");
}


compile:
gcc -o hello hello.c


Are you doing this in the terminal, or in Xcode? (It's much easier in the terminal).

Edit: It works equally well whether I define the hello source as a C++ file with a .C extension, or as a C file with a .c extension.
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
Edit: It works equally well whether I define the hello source as a C++ file with a .C extension, or as a C file with a .c extension.

Interesting: I just tried on a Linux box which happens to have GCC 3.4.3 on it (I don't have a Mac handy here), and I get the same symptom: When called hello.C, it's presumed to be C++, so it's compiled as C++ but not linked. When invoking with g++, it does work without errors.

Are you sure doing "gcc -o hello hello.C" works for you?
 

alexmm

macrumors newbie
Original poster
May 5, 2008
9
0
thanks for the quick answers !
i am doing it in the terminal.
gcc -o hello hello.C doesn't work for me i thought it would as i thought it was a problem coming at the linking stage...
to yeoren : when i first typed my hello.C it worked but when i changed it for ex trying to do printf("helllo %i\n",3); i got the error !! is it still working if you change your hello.C save it and try to compile it again ?
 

Cromulent

macrumors 604
Oct 2, 2006
6,812
1,100
The Land of Hope and Glory
to yeoren : when i first typed my hello.C it worked but when i changed it for ex trying to do printf("helllo %i\n",3); i got the error !! is it still working if you change your hello.C save it and try to compile it again ?

Why would you do that in a printf statement? Just put the 3 where the %i is. You can't call variables a number so that might throw an error I have no idea. But it certainly is incorrect usage.
 

alexmm

macrumors newbie
Original poster
May 5, 2008
9
0
yes sorry you are right. I should have done int i=3; and then i instead of 3 in my printf. But doing this i still have my problem if i name my file .C instead of .c ? This error is still very strange as printf should work in a c++ file (even if you'd probably use cout) am i right ?
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
Even though the stdio headers should work fine w/o qualification, you can wrap your #include <stdio.h> in an extern "C" tag as follows:


#ifdef _cplusplus
extern "C" {
#endif
#include <stdio.h>
#ifdef _cplusplus
}
#endif


The extern "C" tag specifies that the functions declared in stdio.h won't be name-mangled.

Alternatively, you could write (strictly C++)


#include <cstdio>
int main()
{
std :: printf("Hello World\n");
}



For more on C vs C++ linking, read up here:
http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.3
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
gcc -o hello hello.C doesn't work for me i thought it would as i thought it was a problem coming at the linking stage...

The command line you just gave compiles and links, producing an executable called "hello". If you want gcc to compile only, you need to specify the "-c" flag. That way, you end up with "hello.o". (You don't want to do this in your specific use case, but just thought I'd clear up where the linking stage came in.)
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
when i first typed my hello.C it worked but when i changed it for ex trying to do printf("helllo %i\n",3); i got the error !!
Why would you do that in a printf statement? Just put the 3 where the %i is. You can't call variables a number so that might throw an error I have no idea. But it certainly is incorrect usage.

Actually, the parameters you pass to the printf function (in fact, to any function) need not be variables. They can be any expression (including literals), so alexmm's code is correct usage.
 

Cromulent

macrumors 604
Oct 2, 2006
6,812
1,100
The Land of Hope and Glory
Actually, the parameters you pass to the printf function (in fact, to any function) need not be variables. They can be any expression (including literals), so alexmm's code is correct usage.

I stand corrected. Seems rather pointless though, I would have thought the primary usage for format specifiers was that they were used to print an unknown or variable value.
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
I stand corrected. Seems rather pointless though, I would have thought the primary usage for format specifiers was that they were used to print an unknown or variable value.

The format specifiers are there because printf() takes parameters of various types, and it has to know whether a certain set of bits is to be interpreted as a floating point value or an integer, for instance.

The value can't be unknown, otherwise printf couldn't print it :)

Like in any function call, the arguments to printf are evaluated before the function is called. The printf implementation only sees their values.

I agree it's slightly silly to put literals in printf arguments (and I would raise my eyebrows when seeing it in code) but it's fine as far as the compiler is concerned.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.