C programming on MAC OS X

amnesiac1984

macrumors 6502a
Original poster
Jun 9, 2002
760
0
Europe
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.
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
37,273
3,365
Los Angeles
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?
 

kperry8

macrumors newbie
Apr 4, 2002
12
0
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.
 

amnesiac1984

macrumors 6502a
Original poster
Jun 9, 2002
760
0
Europe
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.
 

Taft

macrumors 65816
Jan 31, 2002
1,319
0
Chicago
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
 

macktheknife

macrumors 6502a
Jan 24, 2002
639
0
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.
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
37,273
3,365
Los Angeles
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
 

amnesiac1984

macrumors 6502a
Original poster
Jun 9, 2002
760
0
Europe
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
 

Bradcoe

macrumors regular
Apr 25, 2002
134
0
Northeast U.S.
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.
 

Taft

macrumors 65816
Jan 31, 2002
1,319
0
Chicago
Originally posted by amnesiac1984
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
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
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
37,273
3,365
Los Angeles
Originally posted by amnesiac1984
i was taught this week to do
void main (void)
what works on my mac is
int main (void)
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.

i was taught to end a program with
getch();
For what reason? I've never heard of that.

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?
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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
13,979
3
Gone but not forgotten.
Originally posted by amnesiac1984
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;

will
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.
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
37,273
3,365
Los Angeles
Originally posted by bousozoku
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.
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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
13,979
3
Gone but not forgotten.
Originally posted by Doctor Q

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.
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.
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
37,273
3,365
Los Angeles
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

:)
 

amnesiac1984

macrumors 6502a
Original poster
Jun 9, 2002
760
0
Europe
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
 

bousozoku

Moderator emeritus
Jun 25, 2002
13,979
3
Gone but not forgotten.
Originally posted by Doctor Q
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

:)
Certainly not in a DOS programme, but it could work handily on a 6502-based computer and make for one infinite loop. :)
 

kumichou

macrumors newbie
Jul 19, 2002
9
0
NE, Ohio
Re: C/C++ Compiler

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.
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