[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?

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

问题描述

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

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

假设我编写了一个使用线程的应用程序.在这个应用程序中,我没有直接使用 pthreads,而是使用了一个使用 pthreads 的包装器.因此,在这个包装器的 makefile 中,必须包含 -lpthread.还有一个问题 - 我是否需要在我的项目中包含 -lpthread ,还是没有必要?还是视情况而定?如果有,在什么方面?

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?

我问这个,因为我见过很多,我认为没有必要.-std=c++0x也是如此?

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?

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

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.

这是否回答了我的问题(使用是"),或者我可能做错了什么,除此之外(我是 makefile 的新手 (:)

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

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

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."

这让我想到(我公司项目的)makefile 中包含的所有库是否都是必要的,或者这是出于历史原因"

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:pthread 的包装是静态链接的,而 Informix 库是动态链接的,如果这很重要的话.
此外,操作系统是 RHEL(4 和 5),但我需要知道这是否取决于操作系统和链接方式(动态或静态)

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)

推荐答案

这是你做错的地方:

所以,在这个包装器的 makefile 中,-lpthread 必须包含

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

ar 工具创建的静态库只是一个特殊文件格式的目标文件的集合,链接器稍后会识别这些文件格式.此代码集合未链接到任何库.在这个阶段编译每个源文件时指定 -lpthread 是没有意义的,因为没有进行链接.

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.

只有在调用链接器以从所有库和目标文件中生成最终可执行文件时,您才需要使用 -l 选项传递特定库.注意这一步没有调用编译器,-l不是编译器选项,而是链接器选项.

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天全站免登陆