简体   繁体   English

在Linux中运行C程序

[英]Running a C program in Linux

Can someone explain to me why, in particular, we are using ./a.out to run a program? 有人可以向我解释为什么,特别是我们使用./a.out来运行程序?

Is there any meaning behind this? 这背后有什么意义吗?

Can someone please provide an explanation? 有人可以提供解释吗?

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 $PATH therefore the path to the executable must be explicitly given. 你需要./的原因是因为当前目录( . )不在$PATH因此必须明确给出可执行文件的路径。

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). Windows默认情况下将当前目录附加到PATH,这是不好的(存在DLL注入的风险,依此类推)。 If you mean a.out part, it's just a name (which came from name of format a.out), which you can change by modifying gcc -o parameter. 如果你的意思是a.out部分,它只是一个名称(来自格式a.out的名称),你可以通过修改gcc -o参数来改变它。

When running an executable like a shell like bash the executable must be in your PATH environment variable for bash to 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 gcc if no output filename is specified.] [对于a.out (“汇编程序输出”的缩写),如果没有指定输出文件名,它是gcc编译器的默认可执行输出。

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 #define and #include are loaded (eg into memory). 本质上,您的源代码被发送到预处理器 ,其中加载了#define#include等指令(例如,加载到内存中)。 So any libraries you want to use are loaded, eg 因此,您要使用的任何库都会被加载,例如

#include <math.h>

will basically 'paste' the contents of math.h into source code at the point at which it is defined. 基本上将math.h的内容“粘贴”到源代码中定义它的位置。

Once all this stuff has been expanded out, the compiler turns your source code into object code , which is your source in binary code. 一旦扩展了所有这些东西, 编译器就会将您的源代码转换为目标代码 ,这是二进制代码的源代码。 a.out is the default name for output if you do not specify a build name. 如果未指定构建名称,则a.out是输出的默认名称。

gcc -o mynewprogram mynewprogram.c

a.out is the default name for the compiler. a.out是编译器的默认名称。 AFAIK it is because the linking process is skipped and it is not compiled as an object or library. AFAIK是因为跳过了链接过程而且它没有被编译为对象或库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM