在 Linux 中运行 C 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5361483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:20:18  来源:igfitidea点击:

Running a C program in Linux

clinux

提问by Muthaiya

Can someone explain to me why, in particular, we are using ./a.outto run a program?

有人可以向我解释为什么特别是我们使用./a.out来运行程序吗?

Is there any meaning behind this?

这背后有什么意义吗?

Can someone please provide an explanation?

有人可以提供解释吗?

回答by Dr McKay

If you mean the ./part, it's for safety. Windows by default appends current directory to PATH, which is bad (there's a risk of DLL injection, and so on). If you mean a.outpart, it's just a name (which came from name of format a.out), which you can change by modifying gcc -o parameter.

如果您指的是./零件,那是为了安全。Windows 默认将当前目录附加到 PATH,这很糟糕(存在 DLL 注入等风险)。如果您的意思是a.out部分,它只是一个名称(来自格式 a.out 的名称),您可以通过修改 gcc -o 参数来更改它。

回答by Ignacio Vazquez-Abrams

The name stands for "assembler output", and was (and still is) the default name for the executable generated by the compiler. The reason you need ./in front of it is because the current directory (.) is not in $PATHtherefore the path to the executable must be explicitly given.

该名称代表"assembler output",并且是(现在仍然是)编译器生成的可执行文件的默认名称。您需要./在它前面的原因是因为当前目录 ( .) 不在,$PATH因此必须明确给出可执行文件的路径。

回答by Matt Whitworth

When running an executable like a shell like bash the executable must be in your PATHenvironment variable for bashto locate and run the program.

当运行像 bash 这样的 shell 之类的可执行文件时,该可执行文件必须在您的PATH环境变量中bash才能定位和运行程序。

The ./prefix is a shorthand way of specifying the full path to the executable, so that bash does not need to the consult the PATH variable (which usually does not contain the current directory) to run it.

./前缀是指定的完整路径可执行文件的简便方法,从而使bash中并不需要的咨询PATH变量(通常不包含当前目录)来运行它。

[For a.out(short for "assembler output"), it is the default executable output for a compiler like gccif no output filename is specified.]

[对于a.out(“汇编输出”的缩写),它是编译器的默认可执行输出,就像gcc没有指定输出文件名一样。]

回答by Gigamegs

gcc -o mynewprogram mynewprogram.c

a.out is the default name for the compiler. AFAIK it is because the linking process is skipped and it is not compiled as an object or library.

a.out 是编译器的默认名称。AFAIK 这是因为链接过程被跳过并且它没有被编译为对象或库。

回答by dianovich

It'd be worth you looking a bit more into C and the way that C programs are compiled.

值得您多研究一下 C 和 C 程序的编译方式

Essentially, your source code is sent to the preprocessor, where directives like #defineand #includeare loaded (e.g. into memory). So any libraries you want to use are loaded, e.g.

本质上,您的源代码被发送到预处理器,在那里加载像#define和这样的指令#include(例如到内存中)。因此,您要使用的任何库都已加载,例如

#include <math.h>

will basically 'paste' the contents of math.hinto source code at the point at which it is defined.

基本上将math.h在定义时将内容“粘贴”到源代码中。

Once all this stuff has been expanded out, the compilerturns your source code into object code, which is your source in binary code. a.outis the default name for output if you do not specify a build name.

一旦所有这些东西都展开了,编译器就会将您的源代码转换为目标代码,即二进制代码的源代码。如果您未指定构建名称,则a.out是输出的默认名称。