确定 C 可执行文件名称
当我们编译 C 程序时,输出存储在 a.out 中。 我们如何将编译后的输出重定向到另一个文件?
When we are compiling a C program the output is stored in a.out. How can we redirect the compiled output to another file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在C语言的发源地Unix中,C程序通常是逐模块编译的,然后将编译后的模块链接成可执行文件。 对于由模块
foo.c
和bar.c
组成的项目,命令将如下所示:(使用 -c,输出文件名将成为带有后缀替换为 .o.)
这允许您仅重新编译那些已更改的模块,这对于大型程序来说可以节省大量时间,但也可能变得相当棘手。 (这部分通常使用
make
自动化。)对于单模块程序,首先编译为 .o 文件,然后链接实际上没有任何意义,因此单个命令就足够了
:对于模块程序,习惯上将生成的可执行程序与不带 .c 后缀的 C 源文件一样进行调用。 对于多模块程序,对于输出是否以具有 main 函数的文件命名没有硬性规定,因此您可以自由地发明任何您喜欢的东西。
In Unix, where C originated from, C programs are usually compiled module-by-module, and then the compiled modules are linked into an executable. For a project that consists of modules
foo.c
andbar.c
, the commands would be like this:(With -c, the output filename becomes the source file with the suffix replaced with .o.)
This allows you to also re-compile only those modules that have changed, which can be a big time saver for big programs, but can also become pretty tricky. (This part is usually automated using
make
.)For a single-module program there's not really any point in first compiling to a .o file, and then linking, so a single command suffices:
For single-module programs, it is customary to call the resulting executable program the same as the C source file without the .c suffix. For multi-module programs, there is no hard custom on whether the output is named after the file with the main function or not, so you're free to invent whatever strikes your fancy.
使用
-o
选项。如果您的程序由单个文件组成,这是可以的。 如果您有更多文件,我建议使用
make
:创建一个Makefile
,然后运行命令make
。Makefile
是一个包含一些编译规则的文件。示例如下(
#
表示该行是注释):CXX
变量很快定义了您的编译器(gcc、g++),with CXXFLAGS
您可以为您的编译定义标志(即-std=c++11
)。 然后您可以包含并定义自定义(INC_PATH
和LIBS
:示例中未设置)。 使用SOURCEDIR
您可以指定源代码目录(其中*.c
文件所在)。然后SOURCES
基本上告诉您编译是所有扩展名为*.c
的文件。Makefile
包含一组规则,其结构如下:生成可执行文件的规则
相当于
gcc -Wall -Wextra $(OBJECTS) -o yourExecutableName
。$(OBJECTS)
是编译生成的目标文件。 当执行上述规则时,如果没有找到它们,make将继续扫描文件以找到生成它们的规则。 在这种情况下,生成这些文件的规则是:如果需要更多信息,请告诉我。
With the
-o
option.This is ok if your program consists of a single file. If you have more files I suggest using
make
: create aMakefile
and then run the commandmake
.A
Makefile
is a file containing some rules for compilation.An example can be the following (
#
means the line is a comment):Shortly
CXX
variable defines your compiler (gcc, g++),with CXXFLAGS
you can define flags for your compilation (i.e.-std=c++11
). Then you can include and define custom (INC_PATH
andLIBS
: not set in the example). WithSOURCEDIR
you can specify your source code directory (where*.c
files are).ThenSOURCES
is basically telling that the source files for the compilation are all the files having extension*.c
.The
Makefile
contains a set of rules whose structure is the following:The rule to generate your executable file is
which is equivalent to
gcc -Wall -Wextra $(OBJECTS) -o yourExecutableName
.$(OBJECTS)
are the object file resulting from the compilation. When the above rule is executed, if they are not found make will continue scanning the file to find a rule to generate them. In this case the rule to generate these files is:If further information is needed let me know.
如果
foo
将是您的可执行文件,而bar.c
是您的源文件,则命令为:If
foo
will be your executable andbar.c
is your source file then the command is:此命令将根据操作系统直接创建一个
outputfile.exe
或outputfile.out
。 我们可以输入路径来代替filename.c
或outputfile
,如下所示。This command will directly create an
outputfile.exe
ORoutputfile.out
according to operating system. In place offilename.c
ORoutputfile
we can enter path, as shown below.编译使用:
执行使用:
Compile using:
Execute using:
C 语言中根据用户选择给出 .exe 文件名称的格式
步骤 1 :- 运行 gcc(或编译器在终端 NB 上采用以下格式
:- 如果您正在运行“Vs Code”,请使用“Tab”键自动完成。
格式写下程序的名称!
第 2 步:- 按照您完成的
The format of giving the Name of .exe file according to the User Choice in C Language
step 1 :- Run the gcc (or the compiler you have) in the below format on the Terminal
NB:- If you are Running "Vs Code" Use the 'Tab' key for the Auto completion.
step 2 :- Write down the name of the program in format
you are done!
假设您在 ubuntu
第 1 步:使用以下命令运行 gcc 来编译
filename.c
gcc filename.c -o filename.out
第 2 步: 执行
filename.out
by./filename.out
step-3:等待输出,
就是这样,你就完成了
Assuming you are in ubuntu
step-1: run gcc with these commands to compile
filename.c
gcc filename.c -o filename.out
filename.out
will be created, (it might or might not be shown where the other files are stored)step-2: execute the
filename.out
by./filename.out
step-3: wait for the output
thats it , you are done
大多数 C 编译器为此提供了一个选项,例如
gcc
的-o
选项以及其他一些选项:Most C compilers provide an option for this, such as the
-o
option forgcc
and some others:-ofilename
将生成filename
而不是a.out
。-ofilename
will makefilename
instead ofa.out
.根据手册:
According to the manual: