简体   繁体   English

相互链接两个库

[英]Link two libraries to each other

I have a function in libA.so that is used in libB.so , And a function in libB.so that is used in libA.so ! 我有一个功能libA.so是在使用libB.so ,并且在功能libB.so是在使用libA.so So defenetly i can not compile none of these libraries. 因此,毫无根据,我无法编译所有这些库。 How can I compile these two libraries? 如何编译这两个库? Should i used third library and move the dependebcies to this library? 我应该使用第三个图书馆并将依赖关系移至该图书馆吗? I used qt and c++ 我用qt和c ++

Updated: in compile libA.so get error cannot find libB.so and in libB.so get error can not find libA.so 更新:在编译libA.so中获取错误找不到libB.so,在libB.so中获取错误找不到libA.so

对于将来的重用来说,这似乎有点问题,您可能想要将这些库之间的功能LibA ,或者创建一个包含所有“工具”功能的第三个库,以使LibAlibB函数相互不存在。

BIG FAT DISCLAIMER Only do this if absolutely necessary. 大脂肪免责声明仅在绝对必要时才这样做。 The preferred way is to refactor your project structure such that it doesn't contain dependency cycles. 首选方法是重构项目结构,使其不包含依赖关系周期。

When producing a shared library, the linker in general does not need to know about other shared libraries. 生成共享库时,链接程序通常不需要了解其他共享库。 One can use them on the command line but this is optional. 可以在命令行上使用它们,但这是可选的。 Example: 例:

// libA.cpp
extern void funcB();
void funcA() {
    funcB();
}

Compile and link: 编译链接:

g++ -fPIC -c libA.cpp
g++ -shared -o libA.so libA.o

funcB is supposed to live in libB.so but we are not telling the linker where to find it. funcB应该存在于libB.so但我们没有告诉链接器在哪里可以找到它。 The symbol is simply left undefined in libA.so , and will be (hopefully) resolved at load time . 该符号只是在libA.so未定义,并且将(希望)在装入时解析。

// libB.cpp
extern void funcA();
void funcB() {
   funcA();
}

Compile and link, now using libA.so explicitly (ignore the infinite recursion, it's just an example): 现在,显式使用libA.so进行编译和链接(忽略无限递归,这只是一个示例):

g++ -fPIC -c libB.cpp 
g++ -shared -o libB.so libB.o -L/where/libA/is -lA

Now it is up to the executable to load libB.so before loading libA.so , otherwise libA.so cannot be loaded. 现在是到可执行文件加载libB.so加载之前libA.so ,否则libA.so无法加载。 It's easy to do so (just link the executable with only libB.so and not libA.so ), but can be inconvenient at times. 这样做很容易(仅将可执行文件仅与libB.so链接,而不与libA.so ),但有时会带来不便。 So one can re-link libA.so after building libB.so : 因此,可以构建libB.so 之后重新链接libA.so

g++ -shared -o libA.so libA.o -L /where/libB/is -lB

Now one can link an executable to libA or libB and the other one will be picked up automatically. 现在,一个程序可以将可执行文件链接到libA libB,而另一个程序将被自动提取。

I have a function in libA.so that is used in libB.so, And a function in libB.so that is used in libA.so! 我在libA.so中有一个在libB.so中使用的函数,在libB.so中有一个在libA.so中使用的函数!

This is wrong design . 这是错误的设计 A library cannot, even indirectly, depend upon itself. 图书馆甚至不能间接依靠自己。 Such a circularity is the symptom of something very wrong, and you are misunderstanding what a software library is (it is more than a random collection of functions, or of object files; it has to be somehow a "software module" and it is related to modular programming and often defines and implements completely a collection of related abstract data types ). 这样的循环性是某些非常错误的症状,并且您误解了软件库是什么(它不仅仅是功能或目标文件的随机集合;它一定是某种“软件模块”并且与之相关) 模块化编程 ,通常会定义并完全实现一组相关的 抽象数据类型 )。

So throw both libA.so and libB.so away . 因此,请丢掉libA.solibB.so And make a single libAB.so containing all the code that you have put in both libA.so and libB.so shared objects (and not genuine libraries). 作出单一 libAB.so包含所有你已经把两个密码libA.solibB.so共享对象(而不是真正的库)。

The answer from nm gives a technical way to solve your problem, but at heart your design is wrong and you are abusing libraries (and you cannot call your libA or your libB a library, even if you built them as some shared object in ELF ). nm答案提供了解决问题的技术方法,但从libA ,您的设计是错误的,并且您正在滥用库(并且即使将它们构建为ELF中的某些共享库,也无法将libAlibB称为库) 。

You could also design your code by adding some indirection with callbacks , or closures or function pointers held in some variable or data (and provide some way to set these callbacks, or initialize the closures or the function pointers at runtime ). 您还可以通过在回调 函数中添加一些间接性,或者在某些变量或数据中添加闭包函数指针来设计代码(并提供某种方式设置这些回调,或者在运行时初始化闭包或函数指针)。 Since you use Qt , consider also defining appropriately your new Qt signals and slots (they are based on some callback machinery). 由于您使用Qt ,因此请考虑适当定义新的Qt信号和时隙 (它们基于某些回调机制)。

Read Program Library HowTo and Drepper's How to Write Shared libraries paper for more. 阅读程序库HowTo和Drepper的如何编写共享库的文章以获取更多信息。

Finally I solve it. 最后我解决了。
As @nm said we dont need to link libA.so and libB.so in compile time, so I remove -lA and -lB when build them and i didnt get any error. 正如@nm说,我们不需要在编译时链接libA.so和libB.so,所以我删除-lA-lB当建立他们和我没有得到任何错误。 And In app that want to use libA.so or libB.so I linked them with -lA or -lB . 而在想要使用libA.so或libB.so应用我联系他们-lA-lB So this work correctly. 这样可以正常工作。

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

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