error using gcc

Discussion in 'Mac Programming' started by alexmm, May 5, 2008.

  1. alexmm macrumors newbie

    Joined:
    May 5, 2008
    #1
    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.
     
  2. alexmm thread starter macrumors newbie

    Joined:
    May 5, 2008
    #2
    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.
     
  3. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #3
    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.
     
  4. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #4
    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?
     
  5. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #5
    For me, it worked on my mac with whatever combination gcc/g++ .c/.C/.cpp I gave it.

    The version of gcc installed is 4.0.1.
     
  6. alexmm thread starter macrumors newbie

    Joined:
    May 5, 2008
    #6
    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 ?
     
  7. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #7
    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.
     
  8. alexmm thread starter macrumors newbie

    Joined:
    May 5, 2008
    #8
    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 ?
     
  9. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #9
    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
     
  10. alexmm thread starter macrumors newbie

    Joined:
    May 5, 2008
  11. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #11
    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.)
     
  12. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #12
    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.
     
  13. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #13
    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.
     
  14. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #14
    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.
     

Share This Page