gcc如何决定隐含地包含哪些库? [英] How does gcc decide which libraries to implicitly include?

查看:164
本文介绍了gcc如何决定隐含地包含哪些库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参照这个问题

a>:

在一个小型微型嵌入式项目中,我发现我的编译代码大小比预期的要大得多。原来,这是因为我包含了使用assert()的代码。在包含的代码中使用assert是合适的,但是导致我的编译代码大小几乎翻倍。



问题不在于if / when应该使用断言,而是编译器/链接器决定包含断言的所有必要开销。



我从另一篇文章的原始问题:

如果有人能够向我解释当调用assert时gcc如何决定包含库函数会有帮助?我看到assert.h声明了一个外部函数__assert_func。链接器如何知道从库中引用它,而不是只说未定义的__asert_func引用?

解决方案

工具链,作者决定默认应链接哪些库。通常这包括运行时启动/初始化代码和一个名为 libc 的库,其中包含一个C标准的实现,以及作者认为相关的任何其他代码(例如,libc也可能实现Posix,任何定制板特定功能等),并且对于嵌入式目标,连接到为目标实现实时操作系统的库也不是不寻常的。 b
$ b

您可以使用 -nodefaultlibs 标志让gcc在链接阶段省略这些默认库。



在assert()的情况下,它是一个标准的C宏/函数,通常在libc中实现。如果失败,assert()可能会打印到 stdout ,所以使用assert()可以引入实现FILE *处理/缓冲,printf等的整个stdio工具。在libc中实现。



如果运行 gcc -v ,默认情况下可以看到gcc链接的库。为链接阶段。


With reference to this question:

On an embedded project for a small micro I found my compiled code size was much larger than expected. It turned out it was because I had included code that used assert(). The use of assert was appropriate in the included code but caused my compiled code size to almost double.

The question is not around if/when assert should be used but how the compiler/linker decides to include all the necessary overhead for assert.

My original question from the other post:

It would be helpful if someone could explain to me how gcc decides to include library functions when assert is called? I see that assert.h declares an external function __assert_func. How does the linker know to reference it from a library rather than just say "undefined reference to __asert_func"?

解决方案

When configuring a toolchain, the authors decide which libraries that should be linked to by default.

Often this is includes runtime startup/initializing code and a library named libc which includes an implementation of the C standard, and any other code the authors deem relevant (e.g. libc might also implement Posix, any custom board specific functions etc.) and for embedded targets it's not unusual to also link to a library implementing an RTOS for the target.

You can use the -nodefaultlibs flag to gcc to omit these default libraries at the linking stage.

In the case of assert(), it is a standard C macro/function , which is normally implemented in libc. assert() might print to stdout if it fails, so using assert() could pull in the entire stdio facility that implements FILE* handling/buffering, printf etc., all which is implemented in libc.

You can see the libraries that gcc links to by default if you run gcc -v for the linking stage.

这篇关于gcc如何决定隐含地包含哪些库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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