Compiling my first C program

Discussion in 'Mac Programming' started by cables, Jan 5, 2008.

  1. cables macrumors newbie

    Joined:
    Jan 5, 2008
    #1
    Well, first C program on a Mac, anyway.

    My program starts with a line #include <stdio.h>. The rest is a standard program that works on the HP-UX systems at work. I try to compile with the command:

    ./gcc-4.0 myprog -o myprog.c

    and get the error message:

    /usr/bin/ld: myprog bad magic number (not a Mach-0 file)
    collect2: ld returned 1 exit status

    The message suggests a fault with the file type. Any ideas how to fix this? Help would be greatly appreciated.

    Mark
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Do you not want gcc -o <outputfile> <sourcefiles> so

    gcc -o myprog myprog.c
     
  3. cables thread starter macrumors newbie

    Joined:
    Jan 5, 2008
    #3
    Thanks for the suggestion.

    I've just tried that and got the same error message.

    Mark
     
  4. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #4
    Try just doing gcc myFile.c this then creates a file called a.out, which you run with ./a.out

    The -o myFile just creates an output file called myFile, which doesn't help really. Also I compile all my source for test purposes without a -o myFile bit.

    EDIT: Aside from my suggestion being simpler that RobbieDuncan's there is nothing wrong with his suggestions as far as I can see so I'm puzzled as to why you are still having problems.
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    I've had a though (dangerous I know). Is there already a file called myprog in the folder that was compiled on an HP-UX system that gcc can't overwrite? Try copying the source only to a new folder and compile there...
     
  6. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #6
    Is the Mac Intel or PPC? If you are doing bitwise operations this can make a difference if the Mac is Intel (as Intel is Little Endian, vs Big Endian for Unix), though compilation should be unaffected.
     
  7. cables thread starter macrumors newbie

    Joined:
    Jan 5, 2008
    #7
    Fixed it!

    It turns out all I needed to do was put .c on the end of my program name.

    Thanks to everyone for the suggestions.

    Mark
     
  8. bokbosky macrumors newbie

    Joined:
    Jul 29, 2008
    #8
    Please help

    Hi guys,

    I want to start programming in C on my MacBook. I tried all the ways I can, to compile the very basic program, but failed. Please help me.

    #include <stdio.h>
    main()
    {
    *printf("Hello world \n");
    }

    Error Message:
    dhcp-018-125:Cfiles ram$ gcc -o rn1 world.c
    world.c: In function ‘main’:
    world.c:4: error: stray ‘\194’ in program
    world.c:4: error: stray ‘\160’ in program

    Methods followed: 3

    1.

    I tried writing this code in Textwrangler and then compiled using the option 'Run in the terminal' by adding 'shebang' (#! /bin/sh), code is compiled but could not find the output of printf statement,

    Output:
    Last login: Wed Jul 30 01:03:19 on ttys001
    /var/folders/bM/bMtn-hXkGOmPCW7lipJokk+++TI/Cleanup\ At\ Startup/helloWorld-239090206.981.c.command ; exit;
    dhcp-018-125:~ ram$ /var/folders/bM/bMtn-hXkGOmPCW7lipJokk+++TI/Cleanup\ At\ Startup/helloWorld-239090206.981.c.command ; exit;
    logout

    [Process completed]

    Method 2 & 3:
    Both of these 2 methods are similar in the way of compiling, in both the ways I used terminal, but the codes were written in textwrangler & textedit.

    command in the terminal:
    dhcp-018-125:Cfiles ram$ gcc -o rn1 world.c

    Got the same error message:

    world.c: In function ‘main’:
    world.c:4: error: stray ‘\194’ in program
    world.c:4: error: stray ‘\160’ in program

    Guys, sorry for the long message, but all day I tried to compile with all possible ways, but no luck.

    Any help will be appreciated.
     
  9. MacDonaldsd macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #9
    Did you put the * in front of printf or is that the compiler ? if so that is the error.

    Without it that works fine on my mac
     
  10. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    Don't use a word processor for programming. It looks like someone added some characters that are not part of the C character set. Did you re-type the program or did you actually copy what the compiler tried to compile and paste it in here?

    I think you can make life a lot easier for yourself if you just use XCode.
     
  11. bokbosky macrumors newbie

    Joined:
    Jul 29, 2008
    #11
    Hey! how did This came up!

    Thank you very much for your replies guys.
    Sorry guys I could not see that * in the editor. Anyways I wrote that again, here is the error message again,

    #include <stdio.h>
    main()
    {
    printf("Hello World!\n");
    }

    Error Message:

    /usr/bin/ld: /usr/lib/gcc/i686-apple-darwin8/4.0.1/../../../libSystem.dylib unknown flags (type) of section 6 (__TEXT,__literal16) in load command 0
    collect2: ld returned 1 exit status

    see anything?
     
  12. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #12
    In an editor like vi or pico, enter:
    Code:
    int main(int argc, char *argv[]) {
      return 0;
    }
    
    Save this file as test.c.

    From the terminal, in the directory you saved this file, run:
    Code:
    gcc -o test test.c
    
    Does this compile? Is there an executable file named test in the directory?

    If not, what errors do you get? Do not save this file as rich text using some other editor. Use a command line based editor, or if you cannot, find a graphical programmer's text editor (others might have some recommendations). Chances are things are being saved as rich text, and odd things (unicode, high ascii, etc.) are being saved in your source file, and the compiler or linker is choking on them.

    -Lee
     
  13. cruzrojas macrumors member

    cruzrojas

    Joined:
    Mar 26, 2007
    Location:
    USA
    #13
    I believe the error is in the second line. main have to be a function that return an integer. so change main() to int main() and maybe add a return 0; after printf but that is not necessary.

    Hope this helps. best
     
  14. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #14
    As I said, XCode would make your life easier.
     
  15. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #15
    How so? You still get the same error messages.
     
  16. Berlepsch macrumors 6502

    Berlepsch

    Joined:
    Oct 22, 2007
    #16
    The good thing is, that the compiler is happy with your program now. The bad news is, the linker ("ld") is complaining now. My guess is, that it has found the compiled code for your main() function, and it would like to reserve memory for the return value of your function now. The problem is, you didn't tell what kind of variable your return value should be, so the compiler skipped this part, and the linker can't find it out on its own.
    With all the answers in this thread, it should be easy for you to correct this mistake, so I don't have to spell it out again.
     
  17. JVene macrumors newbie

    Joined:
    Jul 22, 2008
    #17

    In one of the 'standard' books on 'professional' programming, they cover this topic with a very valid viewpoint: use a good IDE (and XCode definately qualifies).

    With the command line tools, even with multiple terminal windows to avoid the 'leave the editor to compile-return to editor to correct' workflow, one is still in a rather direct drive approach to the compiler and tools. There's way too much to learn for the novice (lots of switches, libraries to include, etc.) - even the professionals don't want to have to think about these things.

    A good IDE will solve this problem by presenting an 'interface' to the project that deals with the issue of compiling and linking, even presenting error messages in a more convenient fashion. Learning is a bit faster, and working is quite a bit faster, than with command line tools.

    I worked in Unix for years, even before Mac or Windows, and before the common advent of the IDE. I'm very glad to put that experience behind me. Familiarity with command line tools can be of some value, but in the last 10 years I can't recall when I ever needed to resort to it.

    I would consider it a separate study for the student, and rely on the IDE to 'package' the experience of compiling and linking into something more productive and gentler.
     
  18. theg sprank macrumors newbie

    Joined:
    Jul 30, 2008
    #18
    No. The default return value when one is unspecified is int. His program is perfectly valid, and compiles and runs on my machine.
     
  19. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #19
    Heh, I think the complete opposite. The IDE makes everything much harder as you don't know what it is doing explicitly. As a beginner that makes it extremely hard to understand what is going on as everything is hidden from them. With the command line tools everything is so much simpler, you enter a command you know what it does and you know what the results are meant to be therefore it is easier to debug. In an IDE you are never quite sure what it is actually doing under the hood, so you don't know if it is a problem with your code or if you have made a mistake in setting up the IDE.

    But as with most things this is a matter of opinion. I learnt using the command line tools. I'm still not comfortable in Xcode really to be truthful because it hides so much of what it is doing.

    No it isn't. C99 expressly forbids implicit return types.
     
  20. theg sprank macrumors newbie

    Joined:
    Jul 30, 2008
    #20
    C89 doesn't, what's your point?
     
  21. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #21
    C89 is no longer the C standard therefore it is not valid code.
     
  22. theg sprank macrumors newbie

    Joined:
    Jul 30, 2008
    #22
    There is no one C standard. There is more than one. C89 is the previous (and arguably outdated) one, but is still used.
     
  23. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #23
    By definition there can only ever be one standard.
     
  24. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #24
    There's the C Standard, and there's the language that the gcc compiler compiles. They are not the same thing. The default for the gcc compiler is some mixture of C90 plus C99 features but with plenty of backwards compatibility. Using compiler switches you can make it more like C99, but not one hundred percent.

    Anyway, if you use XCode, create a new project, choose "Command Line Utility", then "Standard Tool", you won't have to figure out strange linker problems. You'll also find it a lot easier to create a debugging version, actually use the debugger, do things like creating a PowerPC or 64 bit version, etc. etc. etc.
     
  25. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #25
    Of course. I never claimed GCC supported C99 in its entirety. All I said was that claiming it was valid code is incorrect, because in order for it to be valid it must adhere to the current C standard.

    I only know of one C compiler that supports C99 properly and that is the Intel compiler.
     

Share This Page