简体   繁体   English

链接共享库也链接 cmake 中的不同共享库

[英]Linking shared library that also links a different shared library in cmake

I am currently trying to create a C++ program in Ubuntu that embeds a shared library.我目前正在尝试在 Ubuntu 中创建一个嵌入共享库的 C++ 程序。 For this I have locally the.so file (in my case: libSimpleAmqpClient.so).为此,我在本地有.so 文件(在我的例子中:libSimpleAmqpClient.so)。

The library itself also calls preinstalled shared libraries (in my case: librabbitmq.so and librabbitmq.so.4).库本身也调用预安装的共享库(在我的例子中:librabbitmq.so 和 librabbitmq.so.4)。

I use the following CMAKE code:我使用以下 CMAKE 代码:

cmake_minimum_required(VERSION 3.19)
project(AMQPListener)

## Add SimpleAMQP library headers ##
include_directories(/usr/local/SimpleAmqplient)

## Add RabbitMQ library headers ##
include_directories(/usr/local/rabbitmq-c)

# Create program executable
add_executable(AMQPListener main.cpp)

target_link_libraries(AMQPListener /usr/local/lib/librabbitmq.so.4)
target_link_libraries(AMQPListener /usr/local/lib/librabbitmq.so)
target_link_libraries(AMQPListener /usr/local/lib/libSimpleAmqpClient.so)

The main.cpp file looks like this: main.cpp 文件如下所示:

#include "SimpleAmqpClient/SimpleAmqpClient.h"

int main() {
    AmqpClient::Channel::OpenOpts opts;
    opts.port = 5672;
    opts.host = "192.0.2.255";
    opts.auth = AmqpClient::Channel::OpenOpts::BasicAuth("guest", "guest");

    AmqpClient::Channel::ptr_t channel = AmqpClient::Channel::Open(opts);

    return 0;
}

It compiles fine.它编译得很好。 But during the runtime, I get the following error message:但是在运行时,我收到以下错误消息:

error while loading shared libraries: librabbitmq.so.4: cannot open shared object file: No such file or directory加载共享库时出错:librabbitmq.so.4:无法打开共享 object 文件:没有这样的文件或目录

But it works fine, if I directly call some functions from the shared library that is called by the actual library I would like to include:但它工作正常,如果我直接从共享库中调用一些函数,这些函数由我想包含的实际库调用:

#include "amqp.h"
#include "SimpleAmqpClient/SimpleAmqpClient.h"

int main() {

    amqp_connection_state_t conn;
    conn = amqp_new_connection();

    AmqpClient::Channel::OpenOpts opts;
    opts.port = 5672;
    opts.host = "192.0.2.255";
    opts.auth = AmqpClient::Channel::OpenOpts::BasicAuth("guest", "guest");

    AmqpClient::Channel::ptr_t channel = AmqpClient::Channel::Open(opts);

    return 0;

But of course I do not want to do that.但我当然不想那样做。 So, is there a way I can just use the one shared library and the other needed shared libraries are automatic included?那么,有没有一种方法可以让我只使用一个共享库而自动包含其他需要的共享库?

The shared library files can identify their dependencies on their own during execution, Given that you have linked them during the compilation process共享库文件可以在执行过程中自行识别它们的依赖关系,假设您在编译过程中已链接它们

Here's a Link to Read about that.这是一个阅读链接。 Linking with dynamic library with dependencies 与具有依赖关系的动态库链接

What cmake code below makes sure that compiler ( linker ) finds all the required function definition in the below.so files下面的 cmake 代码确保编译器 ( linker ) 在下面的.so 文件中找到所有必需的 function 定义

target_link_libraries(AMQPListener /usr/local/lib/librabbitmq.so.4)
target_link_libraries(AMQPListener /usr/local/lib/librabbitmq.so)
target_link_libraries(AMQPListener /usr/local/lib/libSimpleAmqpClient.so)

Since, these are not static library files (***.a) , the library function defintions will not be present inside the executable itself, rather the executable will have an instruction stating that these instructions could be found in.so files.由于这些不是 static 库文件(***.a) ,因此库 function 定义不会出现在可执行文件本身中,而是可执行文件将有一条指令说明这些指令可以在 .so 文件中找到。

However during runtime, the OS will look for the required.so files only in the paths set by the LD_LIBRARY_PATH ( unless specified otherwise )但是在运行时,操作系统只会在 LD_LIBRARY_PATH 设置的路径中查找 required.so 文件(除非另有说明)

So the given error message error while loading shared libraries: librabbitmq.so.4: cannot open shared object file: No such file or directory means that during runtime the executable could not find the librabbitmq.so.4 file in the paths which is mentioned by LD_LIBRARY_PATH所以error while loading shared libraries: librabbitmq.so.4: cannot open shared object file: No such file or directory意味着在运行时可执行文件在提到的路径中找不到librabbitmq.so.4文件通过LD_LIBRARY_PATH

Given, your OS is debian based, You can try executing this in the terminal, before running your program鉴于,您的操作系统是基于 debian 的,您可以在运行程序之前尝试在终端中执行此操作

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/

Or follow the methods given here How to set the environmental variable LD_LIBRARY_PATH in linux或者按照这里给出的方法How to set the environment variable LD_LIBRARY_PATH in linux

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

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