 |
 |
C? Just getting started, need help
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Dec 2001
Location: Texas
Status:
Offline
|
|
I need some help getting started with C programming. I am trying to teach myself C programming using the "Practical C Programming" book published by O'Reilly press. Unfortunately I can not get past the very first step. When I follow the steps for the "Hello World" program I get a different result than the book. The book does not go into detail about compilers, but I assume my problem is that my compiler setup is different that what the book assumes. In the books example you end up with an executable that you run by typing in "hello <Enter>". I end up with an a.out file and the error message "hello: Command not found." when I type in "hello <Enter>".
I am using OS 10.1.5
I am using the Dec 2001 Dev Tools.
I am using BBEdit Lite as my text editor.
I am compiling and running from ~/hello directory.
The compile syntax I used was cc -g -Wall -ohello hello.c
I know this may seem like a very trivial problem to anyone who has been programming, but I need help making my very first baby step.
Thank you,
Jesse
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
Navigate to the built product's folder, then type </font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">./hello</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">and enter at the command line. It's just the shell not recoginizing your program, because you didn't install it.
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: May 2002
Location: Australia
Status:
Offline
|
|
to compile you do this
cc -o nameofprogramme csourcecode.c
cc = to the compiler being used.
-o = says that you want to give the binary outputed a specific name.
csourcecode.c = is the source file you are trying to compile.
You will then end up with a binary called nameofprogramme you need to then type:
./nameofprogramme
to run the program, the ./ tell the shell that the program is in the current directory you are in. Or you could give it the full path to your program like this.
/User/jcopeland/Desktop/nameofprogramme
## a practical example
% cc -o HelloWorld helloworld.c
% ./HelloWorld
% Hello World
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Oct 2000
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by jcopeland:
<strong>The compile syntax I used was cc -g -Wall -ohello hello.c
</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Do you have a space between "-o" and "hello"?
cc -g -Wall -o hello hello.c
Does your program run correctly if you type "./a.out" and Enter?
The cc compiler will generate "a.out" executable file by default. As previously mentioned, the "-o" option will allow you to name the executable file with the name of your choice.
If you are short of storage space, if you let the computer use "a.out" every time, the file will be overwritten each time you successfully run the compiler and you won't keep generating executable files and fill up your hard drive.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Dec 2001
Location: Texas
Status:
Offline
|
|
Thank you, everyone for the replies. It appears I had two problems.
1. I did not know about the ./ before typing in the program name.
2. I did need a space after the -o switch. I typed it exactly as the book showed it. My fault for following instructions.
Once again thank you,
Jesse
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Oct 2000
Status:
Offline
|
|
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by jcopeland:
<strong>1. I did not know about the ./ before typing in the program name.
</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">"." refers to your current directory. ".." refers to its parent directory.
"/" is the delimiter for a directory.
I am currently taking a (mandatory) introductory C++ class, and yesterday they taught that the "./" is not necessary. I believe that assumes that you have a path set up in the initialization file for your shell, but I'm not sure how to do that now. (Maybe I'll ask the question today and have an answer tomorrow. Also, I'll try on the University's system and see if it works there.)
Followup: I've telnetted onto the University's system and got a program to run without the "./". It is a Sun Solaris 5.8 system and I didn't see anything in the shell initialization file.
<small>[ 06-13-2002, 01:40 PM: Message edited by: skipjack ]</small>
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status:
Offline
|
|
Skipjack, I've noticed it too. I've chalked it up to a difference between Solaris and BSD. things like that, they work the same way, but have different syntax.
Jessie, an advance word of warning... you're using command line compiling right now. I'd suggest you stick with it, instead of switching to Project Builder. PB does NOT play nicely with C. It has a tweak where even if all your code is correct, it won't output anything until you input all the values. for example:
#include <stdio.h>
main()
{
float i;
fprint("Hello, give a value for i: ");
fscan("%f",&i);
fprint("you typed %f",i);
}
your output in command line will besomething like
% Hello, give a value for i: 4.2
% you typed 4.2
where in Project builder, the exact same code would appear to do nothing, until you punched in a number, like this
>4.2Hello, give a value for i:
>you typed 4.2
::shrug:: this little bug drove me crazy, so I thought I'd give another newbie heads up on it!
(if my code's wrong, it's cuz i haven't actually coded in C in over a month, and I just woke up, so apologies! )
|
|
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Mar 2002
Location: Düsseldorf, Germany, Europe, Earth
Status:
Offline
|
|
Checked out ssh'ing into Solaris, and I also had the working directory in the executable path (i.e. you don't need ./ to run your new programs). This would be specified somewhere deep down in a default file; if you want to get the same effect in OS X add the line
set path = ( $path . )
to your .tcshrc file. This adds ., which denotes the working directory, to the list of directories where the shell looks for executable files. I'm sure you can find lengthy discussions on why this would be good/bad on alt.linux.totallysadgeeks or similar places.
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">I know this may seem like a very trivial problem</font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">These are the ones that are the worst to solve. If you have a proper problem, you can think about it for a long time and find a solution. This approach doesn't work for trivial problems. I promise that at some point you will spend hours looking for a bug that turns out to be caused by a '=' instead of a '=='. It's a completely trivial and utterly stupid mistake, but everyone makes it at some point.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|