简体   繁体   English

如何在MinGW或GCC中自动链接库?

[英]How to auto link a library in MinGW or GCC?

When a custom library is used in the code, it requires -l linker parameter to use: 在代码中使用定制库时,它需要-l链接器参数才能使用:

gcc myprogram.c -lmylibrary

Is there a way to convince MinGW linker to check header files and automatically find and link a library in /lib folder? 有没有办法说服MinGW链接器检查头文件并自动在/lib文件夹中查找和链接库? Or is there a reason why it would be a bad idea? 还是有一个为什么会是个坏主意的原因?

No. 没有。

The problem of looking at C source code and figuring out which libraries it uses is very hard. 查看C源代码并弄清楚它使用哪个库的问题非常困难。 It feels kind of "AI complete" to me, which is why it's typically solved manually by the programmer pointing out the exact right libraries to satisfy the dependencies with. 在我看来,这是“ AI完整的” ,这就是为什么通常由程序员手动指出要满足其依赖性的正确库来手动解决的原因。

Imagine for mylibrary , it's easy to imagine a system with both mylibrary 1.x and 2.x versions installed, and some calls are named exactly the same. 想象一下mylibrary ,可以轻松想象同时安装了mylibrary 1.x和2.x版本的系统,并且某些调用的命名完全相同。 Now try to imagine a computer program capable of deducing what you meant, which library to link with. 现在,尝试想象一个计算机程序,该程序能够推断出您的含义,要链接的库。 It's not possible, since only the programmer knows. 这是不可能的,因为只有程序员才知道。

The pkg-config tool helps with the mechanics of what each library requires in order to be used, but it's still up to you to tell it (via the module name argument(s)) which exact libraries to use. pkg-config工具可以帮助您了解每个库需要使用的机制,但是仍然需要您(通过模块名称参数)告诉您要使用哪个库。

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

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