C programming on MAC OS X

Discussion in 'Mac Help/Tips' started by amnesiac1984, Oct 19, 2002.

  1. amnesiac1984 macrumors 6502a

    Joined:
    Jun 9, 2002
    Location:
    Europe
    #1
    Hi all, I've not posted for a while, been away at uni.

    Part of my course is called Software development for engineers, and this involves programming in C.

    basically we always include stdio.h as a library file but I don't seem to have this file on my mac.

    I have project builder and all the dev tools installed, but I just want to compile and run a simple C program like "hello world" the same way we do it at UNI with in the Turbo C IDE. If i copy the stdio.h header file to my mac and se it as an include, will gcc be able to compile code written at uni for my mac, and will I be able to do tasks on my mac that can be compiled and run on both the mac and the pc workstations at UNI?

    cheers for any help.
     
  2. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #2
    I wrote some C programs with the usual line:

    #include <stdio.h>

    and they compile and run just fine (Mac OS X with the standard developer tools installed). But the Finder says there is no file named stdio.h anywhere on the disk!?!?

    So go ahead and write helloworld.c. In the meantime, can anyone explain where/how conventional C header files are found by the compiler?
     
  3. kperry8 macrumors newbie

    Joined:
    Apr 4, 2002
    #3
    After you install the Developer Tools (I recommend the August Tools if you're using Jaguar), all the standard header files will be kept in /usr/include/. So for example, stdio.h will just be in /usr/include/stdio.h. In your code, as long as you have gcc and the rest of the dev. tools installed, #include <stdio.h> will work fine.

    The reason the Find File would not locate this file is because it is contained in an invisible folder (/usr). If you want to use Find File to locate these files, you must add the Invisibility flag to the find parameters.
     
  4. amnesiac1984 thread starter macrumors 6502a

    Joined:
    Jun 9, 2002
    Location:
    Europe
    #4
    cheers guys I will try this when I get home tomorrow,

    I had sort of tried this already.

    Basically the first task we had to do was literally copy out a printed program and compile it. This didn't work for me on my mac, the compile reported problems with commands like:

    void main (void)

    and getch()

    but knowing me, I probably forgot the ; after a few lines or something and I can't check until i get back to my ddr powermac in bristol as I don't have dev tools here. :(

    i will post more details if it doesn't work properly.
     
  5. Taft macrumors 65816

    Taft

    Joined:
    Jan 31, 2002
    Location:
    Chicago
    #5
    Just so you know, you can check to see if your system has the file. It should be located at:

    /usr/include/stdio.h

    Since gcc checks this path for include files automatically, you chould be able to include it in any compile with no problems.

    Check your codee again.

    Taft
     
  6. macktheknife macrumors 6502a

    macktheknife

    Joined:
    Jan 24, 2002
    #6
    I'll give you an example by creating a "Hello world!" program. Here's what you do:

    Open Project Builder and open a new "Empty File" file. A window should pop up, and paste the following text inside the window. This is where you code.

    Code:
    #include <stdio.h>
    int main(void)
    
    {
    printf("Hello world!\n");
    return 0;
    }
    
    When you save it, make sure you save it as something like "Hello.c". Just make sure you have ".c" as the extension.

    Open terminal and find where the file is located.

    Type "gcc Hello.c" in Terminal and your program should have compiled without any errors. It should also create a file called "a.out". "gcc" is your C complier command. "g++" is you C++ complier command.

    In Terminal, type "./a.out" and it should print a line "Hello world!" BTW, "./" is how you execute programs in Terminal.

    Let me know if this works for you.
     
  7. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #7
    Two more tips:

    1. The cc and gcc commands are aliases for each other, so you can type either one as your command to compile a C program.

    2. a.out is the default name for an executable program produced by the compiler. Rather than use the default, I recommend that you use the compiler's -o ("output file") switch to name the program you are compiling. The -o switch is followed by the name of the file you want to create.

    Using the default:
    cc Hello.c
    ./a.out

    Specifying the program name:
    cc -o Hello Hello.c
    ./Hello
     
  8. amnesiac1984 thread starter macrumors 6502a

    Joined:
    Jun 9, 2002
    Location:
    Europe
    #8
    thanks guys that worked.

    but does this mean that I will have to alter the way I do my university programming tasks in order to do them on my mac? The things that are different are:

    i was taught this week to do
    void main (void)
    what works on my mac is
    int main (void)
    i was taught to end a program with
    getch();
    what works on the mac is
    return 0;

    are they the only things that are different or as my tasks get more complicated am I going to find it harder to program on both platforms? I understand that once I start building proper applications it will be very different but why hsould these little code applets be so different? I have a C programming book which was suggested by my professor and that has commands like return 0 and int main (void), so maybe the way we do it at uni is just a little unorthordox and it really doesn't matter!?


    cheers for all the help guys, at least I can now make some executables on my mac.

    ---

    will
     
  9. Bradcoe macrumors regular

    Joined:
    Apr 25, 2002
    Location:
    Northeast U.S.
    #9
    C++ on Jaguar

    I've been taking a C/C++ course since the beginning of this quarter. I have an iBook with Jaguar, and I picked up a $75 wintel (19" monitor and all) from a friend to do some CAD, programming, even a little gaming on the voodoo 3.
    My question is how/where can I get a C/C++ compiler tool from apple? I figure if I'm going to be writing C++, why not do it on the mac. I'd like to try and start fooling around with some programming on the mac now that i've got some basics down. Thanks.
     
  10. Taft macrumors 65816

    Taft

    Joined:
    Jan 31, 2002
    Location:
    Chicago
    #10
    It all depends on the compiler you use. What kind of machines did you use at school??

    For example on Sun machines, there is a big difference between the CC compiler that Sun provides and gcc. When I wrote C++ code on Solaris, I usually preferred the gcc compiler. This also ensured that it was compatible with other gnu distributions.

    In a worst case scenario, you might have used a lot of compiler specific libraries not available (or easily available) on OS X. In the more likely scenario, you are probably missing a few functions or a library and the rest will be minor syntax problems like the one you just posted.

    I thought that 'void main()' would work on the gcc compiler. Maybe I'm wrong.

    Taft
     
  11. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #11
    Does it really matter? What happens when you use void? A warning message or something fatal?

    I think int is the "proper" choice. Depending on the compiler and operating system, main() can usually return a value, using either return or exit(), that the operating system can make available to the environment that called the program. For example, a shell script might invoke the program and test the returned value.

    For what reason? I've never heard of that.

    When Dennis Ritchie invented C in the 1970s, it was a marvel of platform independence. But the header files that any real program depends on evolved differently on different platforms due to differences in machine architecture and operating systems. As a result, you really do need minor differences in code to make a program compile and run across many platforms. Preprocessor directives (#if) are the usual solution.

    Newer programming languages addressed this problem, which is why Java runs on a virtual machine, making it much more likely that a single source file will be right for many platforms.
     
  12. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #12
    getch() is a function that's DOS only. In fact, you'll find the prototype in dos.h. It's basically getchar() or getc() but uses the BIOS function to get a character.

    You should be able to use void main(void) as long as you don't try to return anything at the end of main() and don't expect any arguments from the command line. You may receive a warning that it's not standard practice.
     
  13. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #13
    The question is WHY anyone would recommend putting getch() at the end of a program. If the purpose is to "clear the junk out of the input buffer", you'd want to use getch() in a loop until end of file is reached, not just execute it once.

    And why would you need to do such a thing anyway? Exiting a program closes all files and throws away any leftover data in input buffers.

    It doesn't hurt to do this getch(), but now I'm curious if anyone knows a good reason for such a recommendation.
     
  14. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #14
    When running a DOS programme in Windows, if you don't have something waiting for a response, the DOS window will close and you won't see your results.
     
  15. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #15
    Thanks for the explanation. That makes perfect sense. There are other ways to make a program stall so the DOS window won't close, but methods, such as getch(), that don't consume CPU time are indeed best.

    You wouldn't, for example, want to include something like this:

    LOOP: LDA #$01
    BNE LOOP

    :)
     
  16. amnesiac1984 thread starter macrumors 6502a

    Joined:
    Jun 9, 2002
    Location:
    Europe
    #16
    Thanks guys,

    I literally only jsut started learning C, and the course we are doing is designed to compliment the engineering part of our course, (my course is called, Music Systems Engineering).

    The more I learn about it, the more I am understanding the differences between writing on the two platforms.

    I need to emphasize that I am not planning to make full blown mac applications, at them moment I am literally writing < 30 lines of code per program, just doing excersises using if while and do while functions etc. next week we are going to learn about using prewritten functions.

    I am having broadband installed at home on monday so I will be able to post more frequently, thanks for all the advice tho
     
  17. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #17
    Certainly not in a DOS programme, but it could work handily on a 6502-based computer and make for one infinite loop. :)
     
  18. kumichou macrumors newbie

    Joined:
    Jul 19, 2002
    Location:
    NE, Ohio
    #18
    Re: C/C++ Compiler

    If you got Jaguar, you should have received the Developer Tools CD which has GCC - the GNU C compiler that handles C, C++ and Obj-C
     

Share This Page