[英]Executing a compiled C program
I compiled a silly little "hello world" C program called main.c: 我编译了一个名为main.c的愚蠢的小“hello world”C程序:
gcc main.c
As expected, a file called a.out appeared, which they say is an executable. 正如所料,出现了一个名为a.out的文件,他们说这是一个可执行文件。 From that same directory, if I type
如果我键入,从同一目录
a.out
and hit enter, it says "command not found". 并按Enter键,它说“找不到命令”。 But if I type
但如果我输入
./a.out
It says "hello world", as desired. 它根据需要说“你好世界”。 I've never seen an executable that requires a './' in front of it to run.
我从来没有见过一个可执行文件,它需要在它前面运行'./'。 Why now?
为什么现在?
All executables that aren't in your PATH
require an explicit path from root /
or the local directory ./
to run. 不在
PATH
所有可执行文件都需要从root /
或本地目录./
运行的显式路径。 A quick search turns up other threads with essentially the same question: 快速搜索以其他基本相同的问题呈现其他线程:
Why do you need ./ (dot-slash) before script name to run it in bash? 为什么在脚本名称之前需要./(dot-slash)才能在bash中运行它?
This also has the added benefit of helping with your auto completion in your shell (assuming it supports it). 这也有助于在shell中自动完成(假设它支持它)。 If you type just a Tab Tab then it will list every executable in your path that starts with "a".
如果只键入一个 选项 卡 选项 卡 ,它将列出路径中以“a”开头的每个可执行文件 。 However, if you type .
但是,如果你输入。 / a Tab it will probably just auto-complete as
a.out
since it will only look at executable files in the current directory starting with "a". / 制表它将可能只是自动为
a.out
,因为它会只在当前目录下的可执行文件从“a”。 So, looking at it that way, the "./" actually saves you typing a few keys! 因此,以这种方式看,“。/”实际上可以节省您键入几个键!
That's because a.out
is not in your $PATH
. 那是因为
a.out
不在你的$PATH
。
The command you provide is searched in the $PATH
( environment variable in linux ) by the shell. 您提供的命令由shell在
$PATH
( linux中的环境变量)中搜索。
$PATH basically is the list of directories. $ PATH基本上是目录列表。 When you provide the executable name, shell searches it in the directories provides by
$PATH
. 当您提供可执行文件名时,shell会在
$PATH
提供的目录中搜索它。
Since a.out
is not in your $PATH
, you've to explicitly provide the path to a.out. 由于
a.out
不在$PATH
,因此您必须明确提供a.out的路径。
It is standard practice in Unix and Linux not to have the current working directory in the path. 在Unix和Linux中,标准做法是不要在路径中包含当前的工作目录。 If you want to have MSDOS/Windows behavior, alter your PATH variable to include
.
如果要进行MSDOS / Windows行为,请将PATH变量更改为include
.
as the first directory. 作为第一个目录。
It's because the system is looking for a.out or any other exec. 这是因为系统正在寻找a.out或任何其他高管。 file in some special paths.
文件在一些特殊的路径。 And the current dir in not in that list by default (usually).
并且当前dir默认不在该列表中(通常)。
look at the list of such paths: 看一下这样的路径列表:
$ env|grep PATH
you can add such current dir to PATH env. 你可以添加这样的当前目录到PATH环境。 variable:
变量:
$ export PATH=$PATH:.
But you better avoid doing that and run ./a.out . 但你最好避免这样做并运行./a.out 。 Such tech.
这样的技术。 provides us understanding that we are running specified file from current dir, not the other file with the same name from another (potentially) dir.
让我们了解我们正在从当前目录运行指定文件,而不是从另一个(可能)目录运行具有相同名称的其他文件。 So, we know what we run exactly.
所以,我们知道我们到底跑了什么。
When you type something like a.out
into a Linux terminal, you're implying that you want to run a command called a.out
. 当您在Linux终端中输入类似
a.out
,您暗示您要运行名为a.out
的命令。 By default, the terminal does not look in the current directory for these commands, it looks in PATH
- a set of directories for executable programs. 默认情况下,终端不查看当前目录中的这些命令,它查找
PATH
- 一组可执行程序的目录。 It is usually these directories: 通常是这些目录:
/bin
/usr/bin
/usr/local/bin
echo $PATH
) echo $PATH
来检查它们) You have to specifiy the directory
directory of your program for it to run, if it is not in one of the directories of PATH
. 如果它不在
PATH
某个目录中,则必须指定程序的directory
目录才能运行它。 For example: 例如:
./a.out
works because .
./a.out
作用是因为.
refers to the directory you're in ../a.out
could work if a.out
is in a parent directory ( ..
refers to the parent) directory a.out
在父目录( ..
指的是父目录)中,则../a.out
可以工作 projectdir/a.out
also works, if your program is in the sub-directory, projectdir
projectdir/a.out
也适用,如果你的程序是在子目录, projectdir
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.