Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

After compiling how do I execute the program???? 7

Status
Not open for further replies.

bmayo

Technical User
Aug 16, 2001
4
0
0
US
I wrote a simple C program and compiled it (I think) by typing (gcc -o new new.c).

This is the program
#include <stdio.h>
main()

{
printf(&quot;HELLO!\n&quot;);
return 0;
}

Do I type &quot;new&quot; to run it or what.
 
Yes just type new. If you do not specify the output filename that the default is a.out. Wushutwist
 
if you installed it in your path, just type new, otherwise cd to the directory and type
./new

-John
---
John Hoke
 
You may also need to change the permissions on 'new' to make it executable.


chmod 555 new

Bluecrack
 
Somebody covered this above but didn't really explain it well. Unlike in DOS (which you might be more used to) Unix checks what you have set up in PATH when you enter a command. If the directory you are in is not part of the path command then programs wont execute.

It seems counter intuitive at first (at least it did to me) as I just assumed it would check whatever directory I was in in addition to what was in PATH but Unix doesn't. So unless the program is in a directory that part of PATH you need to specify what directory it is in.

The reason why you enter ./ is that the ./ stands for &quot;whatever directory I currently am in.&quot;

The first time I wrote a C program and tried running it in C I almost threw my chair through a window because of this issue. Between that and permissions I considered firebombing red hat :)
 
Another possibility is to put . in you path. Then you can just type new to run your program. I believe there are some security issues with doing this but I don't recall what they are.

CaKiwi
 
The command to add . in the path is
export PATH=$PATH:.
This will hold good only for the current console. To permanently set the path,
add the above line in the .bashrc file
 
There is an important security related caveat to the tip about adding &quot;.&quot; to your $PATH.

Let's say somebody manages to get a program onto your system and it's called &quot;ls&quot;. When you run it, it emails your /etc/passwd out somewhere (or does something much worse), and then does the same thing as a real &quot;ls&quot; command. You don't know that you are running a bad ls, and some bad guy somewhere has managed to do something to your system.

The safest way is to prepend your program names with &quot;./&quot; until you can install them into a standard directory such as /usr/local/bin. (My personal preference, as it keeps /bin, /usr/bin, and others, clean of my own strange binaries.)

It isn't much extra to type, and you soon get used to it.
 
AndyBo

The security issue can be overcome by overlooked if you add the present directory '.' at the end of your $PATH variable after all the standard bin directories. This is because bash looks for the first executable that can be found in the directories pointed to by $PATH and this search is sequential - the order in which you have given the directories.

uday
 
In the directury where the executeable type
./new

if new is the name of the executeable.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top