确定 C 可执行文件名称

发布于 2024-07-15 08:52:52 字数 56 浏览 4 评论 0原文

当我们编译 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

谁的新欢旧爱 2024-07-22 08:52:53

在C语言的发源地Unix中,C程序通常是逐模块编译的,然后将编译后的模块链接成可执行文件。 对于由模块 foo.cbar.c 组成的项目,命令将如下所示:(

cc -c foo.c
cc -c bar.c
cc -o myprog foo.o bar.o

使用 -c,输出文件名将成为带有后缀替换为 .o.)

这允许您仅重新编译那些已更改的模块,这对于大型程序来说可以节省大量时间,但也可能变得相当棘手。 (这部分通常使用 make 自动化。)

对于单模块程序,首先编译为 .o 文件,然后链接实际上没有任何意义,因此单个命令就足够了

cc -o foo foo.c

:对于模块程序,习惯上将生成的可执行程序与不带 .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 and bar.c, the commands would be like this:

cc -c foo.c
cc -c bar.c
cc -o myprog foo.o bar.o

(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:

cc -o foo foo.c

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.

浊酒尽余欢 2024-07-22 08:52:53

使用 -o 选项。

gcc main.c -o myCoolExecutable.o

如果您的程序由单个文件组成,这是可以的。 如果您有更多文件,我建议使用 make:创建一个 Makefile,然后运行命令 make

Makefile 是一个包含一些编译规则的文件。
示例如下(# 表示该行是注释):

CXX = gcc

#CXXFLAGS = -std=c++11
#INC_PATH = ...
#LIBS = ...

SOURCEDIR := yourSourceFolder
SOURCES := $(wildcard $(SOURCEDIR)/*.c)
OBJDIR=$(SOURCEDIR)/obj

OBJECTS := $(patsubst $(SOURCEDIR)/%.c,$(OBJDIR)/%.o, $(SOURCES))
DEPENDS := $(patsubst $(SOURCEDIR)/%.c,$(OBJDIR)/%.d, $(SOURCES))

# ADD MORE WARNINGS!
WARNING := -Wall -Wextra

# .PHONY means these rules get executed even if
# files of those names exist.
.PHONY: all clean

# The first rule is the default, ie. "make",
# "make all" and "make parking" mean the same
all: yourExecutableName

clean:
    $(RM) $(OBJECTS) $(DEPENDS) yourExecutableName

# Linking the executable from the object files
# $^   # "src.c src.h" (all prerequisites)
yourExecutableName: $(OBJECTS)
    $(CXX) $(WARNING) $^ -o $@
    #$(CXX) $(WARNING) $(CXXFLAGS) $(INC_PATH) $^ -o $@ $(LIBS)

-include $(DEPENDS)

$(OBJDIR):
    mkdir -p $(OBJDIR)

$(OBJDIR)/%.o: $(SOURCEDIR)/%.c Makefile | $(OBJDIR)
    $(CXX) $(WARNING) -MMD -MP -c 
lt; -o $@

CXX 变量很快定义了您的编译器(gcc、g++),with CXXFLAGS 您可以为您的编译定义标志(即-std=c++11)。 然后您可以包含并定义自定义(INC_PATHLIBS:示例中未设置)。 使用 SOURCEDIR 您可以指定源代码目录(其中 *.c 文件所在)。然后 SOURCES 基本上告诉您编译是所有扩展名为*.c的文件。

Makefile 包含一组规则,其结构如下:

output: inputs
    commandToExecute

生成可执行文件的规则

yourExecutableName: $(OBJECTS)
    $(CXX) $(WARNING) $^ -o $@

相当于 gcc -Wall -Wextra $(OBJECTS) -o yourExecutableName

$(OBJECTS) 是编译生成的目标文件。 当执行上述规则时,如果没有找到它们,make将继续扫描文件以找到生成它们的规则。 在这种情况下,生成这些文件的规则是:

$(OBJDIR)/%.o: $(SOURCEDIR)/%.c Makefile | $(OBJDIR)
    $(CXX) $(WARNING) -MMD -MP -c 
lt; -o $@

如果需要更多信息,请告诉我。

With the -o option.

gcc main.c -o myCoolExecutable.o

This is ok if your program consists of a single file. If you have more files I suggest using make: create a Makefile and then run the command make.

A Makefile is a file containing some rules for compilation.
An example can be the following (# means the line is a comment):

CXX = gcc

#CXXFLAGS = -std=c++11
#INC_PATH = ...
#LIBS = ...

SOURCEDIR := yourSourceFolder
SOURCES := $(wildcard $(SOURCEDIR)/*.c)
OBJDIR=$(SOURCEDIR)/obj

OBJECTS := $(patsubst $(SOURCEDIR)/%.c,$(OBJDIR)/%.o, $(SOURCES))
DEPENDS := $(patsubst $(SOURCEDIR)/%.c,$(OBJDIR)/%.d, $(SOURCES))

# ADD MORE WARNINGS!
WARNING := -Wall -Wextra

# .PHONY means these rules get executed even if
# files of those names exist.
.PHONY: all clean

# The first rule is the default, ie. "make",
# "make all" and "make parking" mean the same
all: yourExecutableName

clean:
    $(RM) $(OBJECTS) $(DEPENDS) yourExecutableName

# Linking the executable from the object files
# $^   # "src.c src.h" (all prerequisites)
yourExecutableName: $(OBJECTS)
    $(CXX) $(WARNING) $^ -o $@
    #$(CXX) $(WARNING) $(CXXFLAGS) $(INC_PATH) $^ -o $@ $(LIBS)

-include $(DEPENDS)

$(OBJDIR):
    mkdir -p $(OBJDIR)

$(OBJDIR)/%.o: $(SOURCEDIR)/%.c Makefile | $(OBJDIR)
    $(CXX) $(WARNING) -MMD -MP -c 
lt; -o $@

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 and LIBS: not set in the example). With SOURCEDIR you can specify your source code directory (where *.c files are).Then SOURCES 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:

output: inputs
    commandToExecute

The rule to generate your executable file is

yourExecutableName: $(OBJECTS)
    $(CXX) $(WARNING) $^ -o $@

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:

$(OBJDIR)/%.o: $(SOURCEDIR)/%.c Makefile | $(OBJDIR)
    $(CXX) $(WARNING) -MMD -MP -c 
lt; -o $@

If further information is needed let me know.

究竟谁懂我的在乎 2024-07-22 08:52:53

如果 foo 将是您的可执行文件,而 bar.c 是您的源文件,则命令为:

gcc -o foo bar.c

If foo will be your executable and bar.c is your source file then the command is:

gcc -o foo bar.c
愚人国度 2024-07-22 08:52:53
gcc filename.c -o outputfile

此命令将根据操作系统直接创建一个 outputfile.exeoutputfile.out。 我们可以输入路径来代替 filename.coutputfile,如下所示。

gcc ./home/user/filename.c -o ./home/outputfile
gcc filename.c -o outputfile

This command will directly create an outputfile.exe OR outputfile.out according to operating system. In place of filename.c OR outputfile we can enter path, as shown below.

gcc ./home/user/filename.c -o ./home/outputfile
流绪微梦 2024-07-22 08:52:53

编译使用:

cc -o <opfilename> <filename.c>

执行使用:

./<opfilename>

Compile using:

cc -o <opfilename> <filename.c>

Execute using:

./<opfilename>
柠檬色的秋千 2024-07-22 08:52:53

C 语言中根据用户选择给出 .exe 文件名称的格式

步骤 1 :- 运行 gcc(或编译器在终端 NB 上采用以下格式

    gcc -o put_your_name_you_want_to_give (space) your_file_name_you_want_to_execute

:- 如果您正在运行“Vs Code”,请使用“Tab”键自动完成。

格式写下程序的名称!

           .\the_name_you_have_given.exe

第 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

    gcc -o put_your_name_you_want_to_give (space) your_file_name_you_want_to_execute

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

           .\the_name_you_have_given.exe

you are done!

娇柔作态 2024-07-22 08:52:53

假设您在 ubuntu

第 1 步:使用以下命令运行 gcc 来编译 filename.c

gcc filename.c -o filename.out

  • <将创建 code>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

好听的两个字的网名 2024-07-22 08:52:52

大多数 C 编译器为此提供了一个选项,例如 gcc-o 选项以及其他一些选项:

gcc -o gentext gentext.c
cc  -o mainprog -Llib -lmymath firstbit.c secondbit.o
xlc -o coredump coredump.c

Most C compilers provide an option for this, such as the -o option for gcc and some others:

gcc -o gentext gentext.c
cc  -o mainprog -Llib -lmymath firstbit.c secondbit.o
xlc -o coredump coredump.c
回忆追雨的时光 2024-07-22 08:52:52

-ofilename 将生成 filename 而不是 a.out

-ofilename will make filename instead of a.out.

睡美人的小仙女 2024-07-22 08:52:52

根据手册:

-o <file>  Place the output into <file>

According to the manual:

-o <file>  Place the output into <file>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文