简体   繁体   English

当我使用第三方库时,如何找出我需要链接的 lib 文件?

[英]How to find out which lib file I need to link while I use a third party library?

For example if I use a function in opencv, and I can use two methods to specify the libs and link them.例如,如果我在 opencv 中使用 function,我可以使用两种方法来指定库并链接它们。 First one, I can use find_package(OpenCV 3.3 REQUIRED) and them target_link_libraries( main ${OpenCV_LIBS}) because the macro include all the libraries we need.第一个,我可以使用find_package(OpenCV 3.3 REQUIRED)target_link_libraries( main ${OpenCV_LIBS})因为宏包含我们需要的所有库。 Second one, I want to link my opencv without ".cmake" and I need to give CMakeLists.txt a link_directiories(/xxxx) and use target_link_libraries(main opencv_core opencv_imgcodecs) .第二个,我想在没有“.cmake”的情况下链接我的 opencv,我需要给 CMakeLists.txt 一个link_directiories(/xxxx)并使用target_link_libraries(main opencv_core opencv_imgcodecs)

So my question is how can I quickly find out which lib I should link while I use the second method.所以我的问题是如何在使用第二种方法时快速找出应该链接哪个库。 Like I use some function in the opencv_core.so but actually I know it by try several times among much libs in opencv.就像我在 opencv_core.so 中使用了一些 function 但实际上我在 opencv 中的许多库中尝试了几次就知道了。

There is no quick way to do it, apart maybe from reading your library documentation.除了阅读您的库文档之外,没有快速的方法可以做到这一点。 In OpenCV it is quite straightforward, documentation specifies the list of the modules, then your rule of thumb should be: whenever you #include <opencv2/MODULE_NAME.hpp> , you link to opencv_MODULE_NAME .在 OpenCV 中它非常简单,文档指定了模块列表,那么您的经验法则应该是:每当您#include <opencv2/MODULE_NAME.hpp>时,您链接到opencv_MODULE_NAME Eg #include <opencv2/imgproc.hpp> , then you should link with opencv_imgproc .例如#include <opencv2/imgproc.hpp> ,那么你应该链接到opencv_imgproc

If you don't have this kind of information beforehand, on the Linux machine you can investigate the symbols in the available dynamic libraries:如果您事先没有此类信息,则可以在 Linux 机器上查看可用动态库中的符号:

for lib in `find LIB_PATH -name "*.so"`; do echo ${lib}; nm -D -C ${lib}; done

where LIB_PATH is where your libraries are installed, eg /usr/lib . LIB_PATH是安装库的位置,例如/usr/lib

You can then save the output and search for the undefined symbol that was reported by your linker while building your project.然后,您可以保存 output 并搜索 linker 在构建项目时报告的未定义符号。 Instead of "*.so" you can have more restrictive pattern like libopencv*.so to limit the output.而不是"*.so" ,您可以使用更严格的模式,例如libopencv*.so来限制 output。

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

相关问题 如何在CMake中链接第三方库(LibUSB) - How to Link a third Party Library (LibUSB) in CMake 如何使第三方库对Boost线程使用是线程安全的? - How can I make a third-party library thread-safe for use with Boost threads? 如何将第三方库添加到QtCreator? - How do I go about adding a third party library to QtCreator? 如何在 cmake 中构建参数化的第三方库? - How do I build a parameterized third-party library in cmake? 如何通过Node N-API使用第三方dll,标头和lib文件 - How to use a third party dll, header, and lib file with Node N-API 我有第三方lib,我有错误LNK2019:外部未解决…如何进行调查以解决问题 - I have a third party lib, I have error LNK2019: unresolved external … How to investigate to fix it 如何在 Visual Studio C++ 中使用第三方 DLL 文件? - How do I use a third-party DLL file in Visual Studio C++? 如何从 constexpr function 中未标记为 constexpr 的第三方库调用 function? - How can I call a function from a third party lib that is not marked constexpr from a constexpr function? 为什么我需要将lib文件链接到我的项目? - why do I need to link a lib file to my project? 如何访问第三方源文件中的功能? - How do I access functions in a third party source file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM