PDA

View Full Version : Developer Newbie Q - C programming


hschickel
03-22-2002, 04:04 PM
I hope I'm putting this in the right place. Perhaps there could be a developer newbies forum otherwise.

[Editor: Perfect spot for it; no need to split a forum which is only one day old! -rob.]

BackOnTopic

I recently started trying to learn Cocoa and kept getting stymied because I did not know any C. So I picked up O'Reilly's, "Practical C Programming - 3rd Edition" and tried running the ubiquitous Hello World program. The program is supposed to look just like this:
[File: hello/hello.c]
#include <stdio.h>
int main()
{
printf("Hello World\n");
return (0);
}
The first line, [File: hello/hello.c] is supposed to tell the compiler which file to compile (hello.c) and what the output file is (hello). Running this way creates a number of errors with OSX's cc compiler. If I remove that line ([File: hello/hello.c]) everything works but I get output of a.out instead of hello. a.out is the executable and it works as expected (other than the name).

I'm sure there is a proper syntax for setting up C and Objective-C programs for command line execution in OSX - but I don't know what it is. Any help would be appreciated.

Thanks always in advance,
Hugh

hschickel
03-22-2002, 04:29 PM
OK - half the problem is solved. I had a syntax error in the compilation instruction. I was using

gcc -g -ohello hello.c

when I should have been using

gcc -g -o hello hello.c.

O'Reilly's book had a typo. In their defense, they claim to put an error in nearly every program in the book to get the student to catch it. I did not think they would try with the first one.

The a.out is OSX's version of gcc's standard output if no name switch is specified.

I'm still at a loss for the file header though. Every C program I've looked at (admittedly only a half dozen or so) has this. On the other hand - the programs created for IDE compilation don't have it. Maybe that's the issue?

Hugh

PS - Is this forum really only one day old? I could have sworn the sun set at least a couple of times since I first posted here. :)

mervTormel
03-22-2002, 04:32 PM
i dunno about that [file: file/file.c] syntax, prolly some preprocessor option; looks like Obj-C trickery to me.

but a.out is the default exe to create. you can control output with a switch to the compiler; i would explore up from the following page to see if you can discover the [file: x/x.x] syntax handler. as is always, compiler docs need to be put thru a babelfish translator for the target platform:

file://localhost/Developer/Documentation/DeveloperTools/Compiler/Compiler.3.html#pgfId=4578

--
Other output options

-o
-o file

Place output in file file . This applies regardless to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.

Since only one output file can be specified, it does not make sense to use -o when compiling more than one input file, unless you are producing an executable file as output.

If -o is not specified, the default is to put an executable file in a.out , the object file for source . suffix in source .o , its assembler file in source .s , and all preprocessed C source on standard output.
--

mervTormel
03-22-2002, 04:38 PM
urp: you slipped in under the wire...

In their defense, they claim to put an error in nearly every program in the book to get the student to catch it.
that's a lousy defense strategy and causes a lot of hardy people to just throw up their hands and go work in the food service industry. you think programming is tough, try serving the public ;]

look for some sort of preprocessor directive that the notation [file: file/file.c] , um, directs?

griffman
03-22-2002, 04:39 PM
PS - Is this forum really only one day old? I could have sworn the sun set at least a couple of times since I first posted here. Eh ... my bad! I thought this was sitting in the new OS X Developer forum ... which is where it's heading now :-).

-rob.

hschickel
03-22-2002, 04:40 PM
merv,

Thanks - I think I was in that very file whilst you were posting.

Rob,

U DA MAN!!! :D The new forum is great - but its visibility is not real high down where it is. Maybe it could be bumped or sym linked up to the help requests area.

Hugh

hschickel
03-22-2002, 04:49 PM
merv,

It may be just a comment header for all I know. All of the samples have it and cc/gcc pukes on it. Hopefully that mystery will be more fully explained in Chapter 3.

OS X's cc/gcc is somewhat specialized from the norm so that it can compile ObjC. I'm sure there are other oddities as well.

I'll let you know when I know.

Hugh

ps - I don't so much disagree with O'Reilly's way of doing things with errrors. That caused me to really look into the syntax of what I was typing. I do wish they would wait a few chapters though. In any case the errror should have been caught by anyone with a little unix under their belt. They merely eliminated the space between the -o switch and the file name. I thought it looked wacky when I first input it and that inclination was correct.

smeger
03-22-2002, 09:01 PM
To the best of my knowledge , [File: hello/hello.c] is not a part of the C specification and I don't know of any individual compilers that support such an option.

This wouldn't make much sense anyway since most C programs consist of many files that are linked together to form a single executable.

My guess is that O'Reilly is just putting that at the top of every program listing so that the reader knows what file and executable the text is referring to.

mervTormel
03-22-2002, 11:00 PM
i concur with smeger. i looked up the book and took a look at the samples and they're clean. did they stuff that [blah: xxx/xxx.c] header in front of actual c source listings? not too very bright, eh? since it looks like some sort of preprocessor directive.

hugh, you said that there was mention that that line is supposed to tell the compiler something. maybe they meant the 13 year old "compiler" they hired to paste source code into the [file: ] tags when they were making the book :D

oh well, consider that "frustration lite" when you get to dangling double dereferenced function pointers.

WillyT
03-23-2002, 08:37 AM
At first I thought this was an examples disk reference, but maybe it's supposed to be a makefile. make doesn't choke till line 3
[darwin:~/hello] williamt% make -f hello.c
hello.c:3: *** missing separator. Stop.
I don't know enough about make to fix it though.

mervTormel
03-23-2002, 01:12 PM
willy, you don't make a .c file

in OSX, you compile a .c file with cc

file://localhost/Developer/Documentation/DeveloperTools/Compiler/CompilerTOC.html

make is a facility for managing build dependencies of a large(r) programming project.

% man gnumake

why gnumake?

$ type make
make is /usr/bin/make

$ ll /usr/bin/make
lrwxr-xr-x 1 root wheel 7 Mar 9 17:33 /usr/bin/make -> gnumake*

make usually gets its input from a file named makefile in the programming project directory. but you can tell make to use a named file with the -f switch, provided the file is a valid make file.

here's a hello.c compile/run session:

$ ll
total 4.0k
-rw-r--r-- 1 merv wheel 88 Mar 23 10:04 hello.c

$ cat hello.c
#include <stdio.h>
int main()
{
printf("Hello World\n");
return (0);
}

$ cc hello.c

$ ll
total 16k
-rwxr-xr-x 1 merv wheel 9.3k Mar 23 10:06 a.out*
-rw-r--r-- 1 merv wheel 88 Mar 23 10:04 hello.c

$ cc -o hello hello.c

$ ll
total 28k
-rwxr-xr-x 1 merv wheel 9.3k Mar 23 10:06 a.out*
-rwxr-xr-x 1 merv wheel 9.3k Mar 23 10:07 hello*
-rw-r--r-- 1 merv wheel 88 Mar 23 10:04 hello.c

$ ./a.out
Hello World

hschickel
03-23-2002, 07:28 PM
smeger and merv,

I believe you guys are right about [File: blah/blah.c]. The place it in every program but I'm up to chapter 5 and they still haven't given a reason for having it. In any case, everthing works great without it.

Thanks for the help,
Hugh

WillyT
03-24-2002, 07:13 AM
merv

This is the first program i've compiled on osx using "cc" at the terminal.

fink makes it all look easy project builder makes it almost easy and the ./configure - make - make install sometimes actually works.

i've written a couple of gui apps for the Amiga and lots of command line thingies but never anything I needed a makefile for.

but hugh saidThe first line, [File: hello/hello.c] is supposed to tell the compiler which file to compile (hello.c) and what the output file is (hello). and that sounds like a makefile. So is there something(makefile) in the appendix that it should be pasted to? or am i shooting at a horse thats already dead or just my foot.

willy

hschickel
03-25-2002, 03:16 AM
Willy,but hugh said
------------------------------------------------------------------------
The first line, [File: hello/hello.c] is supposed to tell the compiler which file to compile (hello.c) and what the output file is (hello).
------------------------------------------------------------------------This was a confusion on my part with the book's convention of placing [File: blah/blah.c] at the beginning of every example and the actual command to compile the file, cc -g -o blah blah.c.

Placing [File: blah/blah.c] at the beginning of the file does not seem to do anything other than cause the compiler to fail. I'm not sure why the book throws that into every example. Maybe its necessary for some types of compilers, or more likely, it's part of the book's filing convention that should have been commented out or done in a different font for clarity.

Hugh