LDD可执行OUPUT中没有动态加载库

Absence of dynamic loaded libraries in ldd executable ouput

本文关键字:动态 加载 可执行 OUPUT LDD      更新时间:2023-10-16

为什么当您给出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 ...但是我的可执行文件示例6取决于libtriangle.so,它是为某些测试而创建的。

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

程序的结构

在此处可执行,Main致电共享库,libtriangle.so。共享库,三角形。

末端存在makefile

man.so使用livingbeing.hpp,man.cpp和man.cpp使用pthread apis.man.so so依赖于-lpthread

三角形。三角形。

可执行文件,main取决于三角形。主acccess三角形。因此使用dlopen,dlsym函数。

ldconfig也用于/usr/usr/local/lib/mydir

中的.so文件

请找到以下文件。

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

man.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 constructorn";
}
~man()
{
cout<<"man destructorn";
}
    virtual void eat() {
        cout<<"man eatingn";
        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;
}

triangle.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);

makefile

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

在其导入表中具有libtriangle符号的意义上,可执行的main不依赖于libtriangleldd不知道使用dlopen在运行时加载的库,因为它不运行或分析代码。它只查看符号表。请注意,即使您在构建可执行文件时明确链接libtriangle也没有效果。仅如果可执行文件具有一些未解决的符号,则链接器才会真正链接库,但是如果main可执行文件,则没有。