GCC中的-o选项是什么? [英] What is the -o option in GCC?

查看:147
本文介绍了GCC中的-o选项是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gcc -c x.c

告诉编译器从x.c文件创建目标代码,而不是尝试链接它,对吧?

-o选项如何告诉编译器,如下所示?

gcc a.o y.o -o x.out

我还有另一个问题是,当我拥有像这样的源代码时:

#include <stdio.h>

int main(){
    puts("");
}

好吧,编译器知道在哪里寻找stdio.h,但是可执行文件必须链接到stdio.o,不是吗? stdio.o文件驻留在哪里?

解决方案

请参见联机帮助页. info/linux/gcc.1.html .在简介部分下,您将看到-o选项.

关于

好吧,编译器知道在哪里寻找stdio.h,但是可执行文件必须链接到stdio.o,不是吗? stdio.o文件驻留在哪里?

第一个问题的答案是否".由于第一个问题的答案为否",因此第二个问题不相关.

stdio.h中声明的函数和变量不必在stdio.o中.它们通常位于库(.a.so)中,该库位于链接程序查找库文件的目录之一中.

通常,没有规则,每个.h文件都具有一个对应的.o文件.可以在多个.c文件中实现在.h文件中声明的函数和变量,这将导致多个.o文件.也可以在多个.h文件中声明的函数和变量要在一个.c文件中实现.这些项目的组织方式因项目而异.

另一方面,每个.c文件都有一个对应的.o文件(我还没有看到可以编译多个.c文件来创建一个.o文件的平台).编译.c文件产生的所有.o文件都链接在一起以创建可执行文件.

gcc -c x.c

tells the compiler to create the object code from the x.c file, and not try to link it, right?

What does the -o option tell the compiler as in the following?

gcc a.o y.o -o x.out

And the other question I have is, when I have source code like:

#include <stdio.h>

int main(){
    puts("");
}

Well OK, the compiler knows where to look for stdio.h, but the executable must be linked to stdio.o, mustn't it? Where does the stdio.o file reside?

解决方案

See manpages.info/linux/gcc.1.html. Under the Synopsis section, you will see the -o option.

Regarding

Well OK, the compiler knows where to look for stdio.h, but the executable must be linked to stdio.o, mustn't it? Where does the stdio.o file reside?

The answer to the first question is "No". Since the answer to the first question is "No", the second question is not relevant.

The functions and variables declared in stdio.h need not be in stdio.o. They are usually in a library (.a or .so) which are found in one of the directories where the linker looks for library files.

In general, there is no rule that each .h file has a corresponding .o file. It is possible to have the functions and variables declared in a .h file to be implemented in multiple .c files that will result in multiple .o files. It is also possible to have functions and variables declared in multiple .h files to be implemented in one .c file. How these are organized varies from project to project.

Each .c file, on the other hand, has a corresponding .o file (I haven't seen any platforms where multiple .c files can be compiled to create one .o file). All the .o files resulting from compiling the .c files are linked together to create an executable.

这篇关于GCC中的-o选项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆