简体   繁体   English

为什么简单的C程序链接到libc.a,而不是libc.so?

[英]Why a simple c program links to libc.a, not libc.so?

My question is, is libc.a always needed, even if I wish to link with libc.so. 我的问题是,即使我希望与libc.so链接,也总是需要libc.a。 I searched internet and it gives 3 possible answers(conflicting) 我搜索了互联网,它给出了3个可能的答案(冲突)

1. c program will require to link to libc.a and crt1.o files
2. for "-l" option, ".so" is preferred against ".a"
3. There're both libc.a and libc.so

So what should be the correct answer to my question? 那么我的问题的正确答案应该是什么?

If you want to link against libc.so and not libc.a, you don't need to do anything special, this is the default. 如果要链接到libc.so而不是libc.a,则无需执行任何特殊操作,这是默认设置。

The first answer is outdated and not applicable to most normal Linux distros. 第一个答案已经过时,不适用于大多数普通的Linux发行版。 AC program by default will need libc.so but not libc.a. 默认情况下,AC程序需要libc.so,但不需要libc.a。

The second answer is correct by default. 默认情况下,第二个答案是正确的。 You need to supply a linker flag like -static to change this preference. 您需要提供-static这样的链接器标志才能更改此首选项。 Note this answer applies to all libraries, not just libc. 请注意,此答案适用于所有库,而不仅仅是libc。

The third answer is correct in most Linux distributions. 第三个答案在大多数Linux发行版中都是正确的。

In the case of GNU libc (supplied with a majority of Linux distros), statically linking your executable against libc.a is not recommended and may not work. 对于GNU libc(大多数Linux发行版提供),不建议将可执行文件与libc.a静态链接,这可能不起作用。 It is doable but you will need to know exactly what you are doing and jump through some hoops, otherwise your executable will still depend on libc.so. 它是可行的,但您需要确切地知道自己在做什么,并跳过一些麻烦,否则您的可执行文件仍将依赖libc.so。

Other versions of libc may be more static-linking-friendly. 其他版本的libc可能对静态链接更友好。

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

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