Reputation: 59
When I compile a program using gcc it should be run using prepending ./ e.g. gcc -Wall -o hello-world hello-world.c will produce binary hello-world which i have to run it as ./hello-world But I want to run it as any other system bimary just by typing hellow-world how to compile it ?
Upvotes: 3
Views: 499
Reputation: 1970
As a note to the answers of @BraveNewCurrency and @songziming to place the executable in the search path.
It is possible to execute the executable without adding the ./
. To do that you have to add the current directory to the search path. Like this:
export PATH='./:'$PATH
This is however generally frowned upon because it can have some unintended side effects. Some people even regard it as a safety risk.
See for instance https://superuser.com/questions/156582/why-is-not-in-the-path-by-default
Upvotes: 2
Reputation: 1346
You have to add the directory containing executable to $PATH
to run it without ./
. It has nothing to do with compilation.
Say file hello-world
is located in /root/hello
, you have to add it to PATH
like: export PATH=$PATH:/root/hello
, then you can run it with hello-world
.
Linux bash commands are just executable files, you can tell the location using which
:
$ which ps
/bin/ps
When you type a command without ./
, bash searches all directories listed in PATH
environment variable to see if it contains the executable you typed, and run it when found. On my machine, the content of PATH
is:
$ echo $PATH
/opt/bochs/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/opt/node-v5.1.1-linux-x64/bin:/opt/jdk1.8.0_65/bin:/opt/neovim/bin:/sbin:/opt/node-v5.1.1-linux-x64/bin:/opt/jdk1.8.0_65/bin
Upvotes: 1
Reputation: 13065
That has nothing to do with how it's compiled. Linux only runs things that are in your $PATH
. So you can copy the binary to /bin, /usr/bin, and sometimes /usr/local/bin. You can also add ~/bin to your path and put the binary there.
Upvotes: 1