Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 12, 2009, 01:50 PM   #1
uaecasher
macrumors 65816
 
uaecasher's Avatar
 
Join Date: Jan 2009
Location: Stillwater, OK
Send a message via AIM to uaecasher Send a message via MSN to uaecasher Send a message via Yahoo to uaecasher
beginner help in C compiling

hello,

I was writing a simple hello world program in C, i have compiling the program in terminal using the code

$ gcc project.c

i got the file a.out

now i want to run it i use the code

$ a.out

but i get this error:

-bash: a.out: command not found


here is the source code for hello world program:

Code:
#include <stdio.h> 
int main (void) 
{ 
	printf ("Programming is fun.\n"); 
	return 0; 
}
uaecasher is offline   0 Reply With Quote
Old Jun 12, 2009, 02:09 PM   #2
rowsdower
macrumors 6502
 
Join Date: Jun 2009
Try this:

Code:
./a.out
The current directory normally isn't in the path for security reasons. Imagine if someone created a virus and named the executable "ls".
rowsdower is offline   0 Reply With Quote
Old Jun 12, 2009, 03:26 PM   #3
uaecasher
Thread Starter
macrumors 65816
 
uaecasher's Avatar
 
Join Date: Jan 2009
Location: Stillwater, OK
Send a message via AIM to uaecasher Send a message via MSN to uaecasher Send a message via Yahoo to uaecasher
it worked but i didn't understand what was wrong

thanks
uaecasher is offline   0 Reply With Quote
Old Jun 12, 2009, 03:53 PM   #4
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
You gave no full path for the file you wanted to run, so the system searched through the directories in your PATH environment variable. The current directory, referred to as ., is not in PATH by default for security reasons. This means that if you want to run a program that is in your current directory, you need to give an explicit path.
./a.out
Is a path to the file a.out in the current directory.

-Lee
lee1210 is offline   0 Reply With Quote
Old Jun 12, 2009, 04:02 PM   #5
Sander
macrumors 6502
 
Join Date: Apr 2008
To make "security reasons" a bit more concrete, the idea is that if the current directory would be in the path, there could be malicious programs in certain directories which would have the name of a popular unix command, but misspelled (for instance, "mroe" instead of "more"). I you happened to be in that directory and you'd type in "mroe", instead of getting "-bash: mroe: command not found", the malicious program would run.

Even worse would be to have the current working directory as one of the first directories in the path, since even correctly spelled unix commands could be "hijacked" that way.
Sander is offline   0 Reply With Quote
Old Jun 12, 2009, 04:08 PM   #6
rowsdower
macrumors 6502
 
Join Date: Jun 2009
On UNIX, every command is a file somewhere. This is different on DOS/Windows, where some commands (like DIR) are built in to the shell. When you type a command on UNIX, the system has to know where to look for the file. Try typing this in the terminal:

Code:
echo $PATH
You will get a list of directories where the system will look if you type a command without a full path.

Code:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
For example, when you type "ls", it will try "/usr/bin/ls" and find that that file does not exist. Then it will try "/bin/ls" and find that that file does exist, so it will run it.

The current directory is always represented by the dot. Since there isn't a "." in the $PATH variable, the system doesn't know where to find your file. The reason that there is not "." in the $PATH is that you always want to know exactly what command you are running. Consider what would happen if you set your path to this:

Code:
.:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
Now, since the first directory in the path is ".", you could just type "a.out" to run your program. However, I could create a program that erases your entire hard drive, and call the file "ls". Then if you happened to be in the directory where I put that file and tried to get a directory listing, my program would run instead of the system "ls" that you were expecting.

(I just noticed that a couple others replied as I was typing this, so most of this has been said already in one way or another.)
rowsdower is offline   0 Reply With Quote
Old Jun 13, 2009, 04:04 AM   #7
uaecasher
Thread Starter
macrumors 65816
 
uaecasher's Avatar
 
Join Date: Jan 2009
Location: Stillwater, OK
Send a message via AIM to uaecasher Send a message via MSN to uaecasher Send a message via Yahoo to uaecasher
ok now i got it, thanks for the help
uaecasher is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Need help compiling c files Paulie87 Mac Programming 27 Mar 11, 2014 08:00 PM
Compiling for Powerpc, how to? skinniezinho PowerPC Macs 12 Jun 21, 2013 04:10 AM
Cross compiling DenBeke Mac Programming 8 May 18, 2013 02:08 AM
compiling VTK using Xcode ilantal Mac Programming 7 Jan 2, 2013 10:43 AM
C++ compiling issues Marcus89 Mac Programming 2 Sep 20, 2012 08:24 AM

Forum Jump

All times are GMT -5. The time now is 09:44 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC