[UNIX]:我是否需要在项目的makefile中添加库中使用的所有库,用于我的项目中? [英] [UNIX] : Do I need to add all libraries in my project's makefile, that are used from a library, used in my project?

查看:102
本文介绍了[UNIX]:我是否需要在项目的makefile中添加库中使用的所有库,用于我的项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哈,这听起来比实际上更复杂。这就是我的意思:

假设我编写了一个使用线程的应用程序。在这个应用程序中,我不直接使用 pthreads ,但是我使用了一个包装器,它使用 pthreads 。所以,在这个包装器的makefile中,必须包含 -lpthread 。问题是 - 我需要在我的项目中包含 -lpthread ,还是没有必要?或者它取决于?如果是这样,在什么?



我问这个,因为我看过很多,我不认为这是必要的。 c $ c> -std = c ++ 0x ?






另外,我有一个Informix C ++接口的问题,因为它依赖于名为 DMI 的lib,它建立在 ESQL / C 。当我删除这些库并只使用真正的库时,我遇到链接器问题(找到库的问题)。当我添加 dmi esql / c 时,一切都很好。



这是否回答我的问题(带YES),或者我可能做错了,除此之外(我是makefiles(:)的新手






来源:Makefiles是一个神秘的话题 - 有一个笑话认为世界上只有一个makefile,所有其他的makefile只是它的扩展,但我向你保证,这不是真的;我不时编写自己的makefile。



这让我想如果所有包含在我公司项目的makefile中的库都是必要的,或者这是出于历史原因






编辑 pthread 链接为static,并且 Informix lib是动态链接的,如果这很重要。

另外,操作系统是RHEL(4和5),但我需要知道这是否取决于操作系统和连接方式(d ynamic或静态的)

解决方案

这是您犯错的地方:

< blockquote>

因此,在这个包装器的makefile中,必须包含
-lpthread

静态由 ar 工具创建的库仅仅是特殊文件格式的对象文件集合,后者由链接器识别。这个代码集合没有链接到任何库。在此阶段编译每个源文件时指定 -lpthread 是没有意义的,因为没有链接完成。



只当调用链接器从所有库和对象文件中生成最终的可执行文件时,需要使用 -l <​​/ code>选项传递特定的库。请注意,在此步骤中不会调用编译器,并且 -l <​​/ code>不是编译器选项,而是链接器选项。



例如,这只会调用链接器,因为没有提供源文件:

  gcc -o myprog main.o  - lmylib -lpthread 

相比之下,将源文件编译为目标文件并指定库是毫无意义的,因为没有链接将被执行:

  gcc -c wrapper_source.c -lpthread 


Ha, this sounds more complicated, than it actually is. Here's what I mean:

Suppose I write an application, that uses threads. In this application, I don't use the pthreads directly, but I use a wrapper, that uses pthreads. So, in this wrapper's makefile, -lpthread must be included. And the question - do I need to include -lpthread in my project, or it's not necessary? Or it depends? If so, on what?

I'm asking this, because I've seen this a lot and I don't think it's necessary.. The same for -std=c++0x?


Also, I had a problem with the Informix C++ interface, as it relies on lib, called DMI, which is built on the top of ESQL/C. When I removed these libraries and use only the real one, I got linker problems(problems with finding the libs). When I added dmi and esql/c, everything was fine.

Does this answers my question (with "YES"), or I could've done something wrong, other than this (I'm newbie with makefiles (: )


source: "Makefiles are something of an arcane topic--one joke goes that there is only one makefile in the world and that all other makefiles are merely extensions of it. I assure you, however, that this is not true; I have written my own makefiles from time to time."

This made me think if all included libraries in the makefiles (of my company's projects) are necessary, or this is for "historical reasons"


EDIT: The wrapper around pthread is linked static, and the Informix lib was dynamically linked, if this matters.
Also, the OS is RHEL (4 and 5), but I need to know if this depends on the OS and on the way of linking (dynamic or static)

解决方案

This is where you do a mistake:

So, in this wrapper's makefile, -lpthread must be included

A static library created by the ar tool is just a collection of object files in a special file format that is recognised later by the linker. This collection of code isn't linked to any library. Specifying -lpthread when compiling each of the source files at this stage is pointless because no linking is done.

Only when calling the linker to produce the final executable out of all libraries and object files, you need to pass the particular libraries with the -l option. Note that the compiler is not invoked at this step, and -l is not a compiler option, but a linker option.

For example, this only invokes the linker because no source files are supplied:

gcc -o myprog main.o -lmylib -lpthread

By contrast, compiling a source file to an object file and specifying a library is pointless because no linking is going to be performed:

gcc -c wrapper_source.c -lpthread

这篇关于[UNIX]:我是否需要在项目的makefile中添加库中使用的所有库,用于我的项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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