gpt4 book ai didi

c++ - ldd 可执行文件输出中缺少动态加载的库

转载 作者:太空狗 更新时间:2023-10-29 11:49:05 26 4
gpt4 key购买 nike

为什么给ldd可执行文件名时没有出现动态加载的库?这是真的吗?因为我给的时候找不到。这可能是因为动态加载和动态链接。

请帮我解决这个问题,如果您需要任何进一步的详细信息,请告诉我。

输出如下。

ldd example6 

linux-vdso.so.1 => (0x00007ffe63369000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f56a2676000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f56a2372000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f56a1fa9000)
/lib64/ld-linux-x86-64.so.2 (0x00007f56a287a000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f56a1ca3000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f56a1a8d000)

我只能看到 libdl.so...但是我的可执行文件 example6 依赖于为某些测试而创建的 libtriangle.so。

如果您在没有 dlopen 命令的情况下访问共享库,我可以在可执行文件中找到 .so 的详细信息,因为我觉得它是静态加载和动态链接。

项目结构

此处可执行,main 调用共享库 libtriangle.so。共享库,triangle.so 调用另一个共享库,man.so.. man.so 调用 pthread api

Makefile 出现在最后

man.so是使用livingbeing.hpp,man.cpp获取的,man.cpp使用pthread apis.man.so依赖-lpthread

triangle.so 对 man.so 进行函数调用。 triangle.so 使用 dlopen、dlsym 命令访问共享库、man.so。

可执行,main依赖于triangle.so。主要访问 triangle.so 使用 dlopen、dlsym 函数。

ldconfig 也为/usr/local/lib/MYDIR 中存在的 .so 文件完成

请找到以下文件。

活人.hpp

 #ifndef LIVINGBEING_HPP

#define LIVINGBEING_HPP

#include <iostream>

using namespace std;

class livingbeing {
protected:
double side_length_;




public:
livingbeing()
: side_length_(0) {}

virtual ~livingbeing() {}
void set_length(double side_length) {
side_length_ = side_length;
}

virtual void eat() = 0;
};

typedef livingbeing* get_instance_t();
typedef void destroy_instance_t(livingbeing*);

#endif

人.cpp

 #include "livingbeing.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message_function( void *ptr );
class man : public livingbeing {
public:
man()
{
cout<<"man constructor\n";

}
~man()
{
cout<<"man destructor\n";
}

virtual void eat() {
cout<<"man eating\n";

pthread_t thread1, thread2;
int iret1;

/* Create independent threads each of which will execute function */

iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) NULL);

pthread_join( thread1, NULL);

}
};

void *print_message_function( void *ptr )
{
cout<<"hello thread1 created";
}
extern "C" livingbeing * get_instance()
{
return new man;
}
extern "C" void destroy_instance(livingbeing *lb)
{
delete lb;
}

三角形.cpp

#include "polygon.hpp"
#include "livingbeing.hpp"
#include <cmath>

#include <dlfcn.h>
//#include <iostream>

class triangle : public polygon {
public:
virtual double area() const {

// load the triangle library
void* man = dlopen("/usr/local/lib/ramu/libman.so", RTLD_LAZY);
if (!man) {
cerr << "Cannot load library: " << dlerror() << '\n';
return 1;
}

// reset errors
dlerror();

// load the symbols
get_instance_t* get_instance_man = (get_instance_t*) dlsym(man, "get_instance");
const char* dlsym_error = dlerror();
if (dlsym_error) {
cerr << "Cannot load symbol create: " << dlsym_error << '\n';
return 1;
}

destroy_instance_t* destroy_instance_man = (destroy_instance_t*) dlsym(man, "destroy_instance");
dlsym_error = dlerror();
if (dlsym_error) {
cerr << "Cannot load symbol destroy: " << dlsym_error << '\n';
return 1;
}

// create an instance of the class
livingbeing* living = get_instance_man();

// use the class
living->set_length(7);
cout << "The livingbeing is: ";

living->eat();
cout<<"\n";


// destroy the class
destroy_instance_man(living);

// unload the triangle library
dlclose(man);


return side_length_ * side_length_ * sqrt(3) / 2;
}
};

// the class factories
extern "C" polygon* create() {
return new triangle;
}

extern "C" void destroy(polygon* p) {
delete p;
}

main.cpp

#include "polygon.hpp"

#include <iostream>
#include <dlfcn.h>

int main() {
using std::cout;
using std::cerr;

// load the triangle library
// void* triangle = dlopen("./triangle.so", RTLD_LAZY);
void* triangle = dlopen("/usr/local/lib/rakesh/libtriangle.so", RTLD_LAZY);
if (!triangle) {
cerr << "Cannot load library: " << dlerror() << '\n';
return 1;
}

// reset errors
dlerror();

// load the symbols
create_t* create_triangle = (create_t*) dlsym(triangle, "create");
const char* dlsym_error = dlerror();
if (dlsym_error) {
cerr << "Cannot load symbol create: " << dlsym_error << '\n';
return 1;
}

destroy_t* destroy_triangle = (destroy_t*) dlsym(triangle, "destroy");
dlsym_error = dlerror();
if (dlsym_error) {
cerr << "Cannot load symbol destroy: " << dlsym_error << '\n';
return 1;
}

// create an instance of the class
polygon* poly = create_triangle();

// use the class
poly->set_side_length(7);
cout << "The area is: " << poly->area() << '\n';

// destroy the class
destroy_triangle(poly);

// unload the triangle library
dlclose(triangle);

生成文件

example6: main.cpp triangle
$(CXX) $(CXXFLAGS) main.cpp -o example6 -L/usr/local/lib/roh -ltriangle -ldl

triangle: man triangle.cpp polygon.hpp
$(CXX) $(CXXFLAGS) -shared -fPIC triangle.cpp -o libtriangle.so

man: man.cpp livingbeing.hpp
$(CXX) $(CXXFLAGS) -shared -fPIC -o man.so man.cpp -lpthread

clean:
rm -f example6 *.so *.o

.PHONY: clean

最佳答案

可执行的 main 不依赖于 libtriangle 在其导入表中具有 libtriangle 符号。 ldd 不知道在运行时使用 dlopen 加载的库,因为它不运行或以任何方式分析代码。它只查看符号表。请注意,即使您在构建可执行文件时显式链接了 libtriangle,它也没有任何效果。如果可执行文件有一些位于该库中的未解析符号,则链接器只会真正链接库,但如果是 main 可执行文件,则没有。

关于c++ - ldd 可执行文件输出中缺少动态加载的库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48899940/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com