我创建了一个名为 car.so 的 .so 文件。我想在 test.cc 代码中测试和使用这个库。在命令行中我想编译:g++ test.cc -o test。我不想也链接(包括)库 car.so。
怎么做?
我的 test.cc 代码如下所示:
void* handle = dlopen("/home/v3/car.so", RTLD_LAZY);
Car* (*create)();
void (*destroy)(Car*);
create = (Car* (*)())dlsym(handle, "create_object");
destroy = (void (*)(Car*))dlsym(handle, "destroy_object");
Car* carr = (Car*)create();
carr->brake();
destroy( carr );
我还想问一下是否可以在一个.so文件中包含3个.so文件。
编辑:
我在 Ubuntu/Linux 上工作
最佳答案
一般来说,您应该在 (application)/test.cc 中包含 .so 库的 .h 文件,然后通过链接该 .so 文件并使用生成的二进制文件来编译该 test.cc 文件。
我认为这个链接真的能帮到你.. http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
关于c++ - 如何在不在命令行 g++ 中运行 .so 文件的情况下使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7305249/