Xcode Compiling Error?

Discussion in 'Mac Programming' started by Vaxuun, Feb 7, 2010.

  1. Vaxuun macrumors member

    Joined:
    Feb 6, 2010
    Location:
    Denmark
    #1
    Whenever I am trying to build and run this program, the log just keeps saying "Running..." and does not print out anything

    The program
    Code:
    #include <stdio.h>
    
    /* count digits, white space, others */
    main()
    {
    	int c, i, nwhite, nother;
    	int ndigit[10];
    	
    	nwhite = nother = 0;
    	for (i = 0; i < 10; ++i)
    		ndigit[i] = 0;
    	
    	while ((c = getchar()) != EOF)
    		if (c >= '0' && c <= '9')
    			++ndigit[c-'0'];
    		else if (c == ' ' || c == '\n' || c == '\t')
    			++nwhite;
    		else
    			++nother;
    	
    	printf("digits =");
    	for (i = 0; i < 10; ++i)
    		printf(" %d", ndigit[i]);
    	printf(", white space = %d, other = %d\n",
    		   nwhite, nother);
    }
    
    The log after Build and Run
    Code:
    
    [Session started at 2010-02-07 17:49:54 +0100.]
    GNU gdb 6.3.50-20050815 (Apple version gdb-1346) (Fri Sep 18 20:40:51 UTC 2009)
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "x86_64-apple-darwin".
    tty /dev/ttys001
    Loading program into debugger…
    Program loaded.
    run
    [Switching to process 78644]
    Running…
    
    [​IMG]

    [​IMG]

    Could someone also try to Build and Run the code? If this is a bug/error, how to correct it? Should I reinstall Xcode?

    P.S. For project, I used Command Line Tool, under Application
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You are, of course, typing something for getchar to return somehow? I don't know if you can do that on the XCode console. I think you may need to run the executable in Terminal...
     
  3. Vaxuun thread starter macrumors member

    Joined:
    Feb 6, 2010
    Location:
    Denmark
    #3
    What should I type for doing it in Terminal?

    Code:
    gcc io.c -o io
    doesn't work...

    I heard that gcc comes with Xcode and I have that installed.. I also said yes to get all packages when I had to install it
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    You need to be more descriptive. What doesn't work? What is the error. And I said run the executable: not compile in Terminal (which brings me to your thread title: I see no issue with XCodes compiling here at all).
     
  5. Vaxuun thread starter macrumors member

    Joined:
    Feb 6, 2010
    Location:
    Denmark
    #5
    When I drag and drop the main.c into Terminal and hit Enter, I get the following:

    [​IMG]

    Doing something wrong? How can I get permission?
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    You are trying to tell Terminal to execute a .c file. That's the source code. It can't be executed. You need to compile as release in XCode and the execute the output file in Terminal
     
  7. Vaxuun thread starter macrumors member

    Joined:
    Feb 6, 2010
    Location:
    Denmark
    #7
    What's the output file? Where to find it? How should I run it?

    Am I overseeing something, am I just stupid or is this more advanced?
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    The output file is the executable created by the compiler. Where it is will depend on your configuration. What it's called will depend on your project configuration. You could compile it using gcc on the command line and specify the output there.
     
  9. Vaxuun thread starter macrumors member

    Joined:
    Feb 6, 2010
    Location:
    Denmark
    #9
    Is it possible you could do new project, copy the project/program and show me how to launch in Terminal? Because I have no idea about all of this.. I don't know what the output file is?

    I would really appreciate if you would do that for me

    EDIT:
    I may have found the executable:
    [​IMG]

    but my Terminal says this:
    [​IMG]
     
  10. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    Have you considered reading the documentation? It would tell you most of these things. Anyway expand Targets on the source list, double click the target that you are building and look under the Build tab. It will tell you where Build Products are put.
     
  11. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #11
    Just copied it.

    I think you might find that getchar() doesn't work too well on OS X for some reason. I had to get use a carriage return for it to pick up the characters.

    Also need to type ^D for the EOF character

    Code:
    $ gcc cTest.c -o cTest
    $ ./cTest
    c
    
    digits = 0 0 0 0 0 0 0 0 0 0, white space = 0, other = 1
    
     
  12. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #12
    That's the normal line-buffering of a Posix-standard tty.
     
  13. Vaxuun thread starter macrumors member

    Joined:
    Feb 6, 2010
    Location:
    Denmark
    #13
    I think I finally compiled the main.c through Terminal!

    It did create another file, but I can't execute it.

    This is what I did in the Terminal:
    Code:
    $ gcc main.c -o main
    $ ./main
    c
    If I try to execute directly by double-tap the new icon:
    Code:
    $ /Users/Lauritz/main ; exit;
     
  14. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #14
    The two main things you're doing wrong are:

    1. You're randomly flailing around trying things without knowing what they are.

    2. You're not answering specific questions that have been asked of you by the people who are trying to help you.

    The way to fix this is:

    1. Stop randomly flailing.

    2. Answer the questions.


    In post #4, you were asked this:

    You then proceeded to quote that post in your response, and completely ignore the question, instead trying some other random thing that also didn't work because you didn't understand what it meant.

    This approach won't work.

    You must be systematic.

    Programming is all about being systematic.
     
  15. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #15
    Double-clicking the new icon won't work.

    The ./main command you executed is probably waiting for you to type things. Type them, then hold down the Control key and press D, which is the special key that means EOF in Terminal (control-D).
     
  16. Vaxuun thread starter macrumors member

    Joined:
    Feb 6, 2010
    Location:
    Denmark
    #16
    :bow: Thanks! Finally it executed and ran the code! :bow:

    I get the result
    Code:
    digits = 0 0 0 0 0 0 0 0 0 0, white space = 0, other = 2
    but in the book, it is
    Code:
    digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345
    but thanks! That's what needed. The Control-D
     
  17. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #17
    What do you think your output is telling you about what you typed?

    What do you think the book's output is telling you about what the author typed?
     
  18. Vaxuun thread starter macrumors member

    Joined:
    Feb 6, 2010
    Location:
    Denmark
    #18
    I don't know, I copied from book, line for line (it should be said it is an e-book, not paperback) but you can see pilotError got other = 1, I got other = 2..

     
  19. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #19
    And this is why I always end up saying copy and pasting is not programming and does not teach anyone anything. Before you try and run the code you should understand what it says and know what you expect to happen (not by reading sample output in a book but by logically following the code). Go back, read the bits of the book before the code and ensure you understand every single statement in the code before you try and run it.
     

Share This Page