这是Dynamic Shared Library compilation with g++的后续行动.

I'm trying to create a shared class library in C++ on Linux. I'm able to get the library to compile, and I can call some of the (non-class) functions using the tutorials that I found here and here. My problems start when I try to use the classes that are defined in the library. The second tutorial that I linked to shows how to load the symbols for creating objects of the classes defined in the library, but stops short of using those objects to get any work done.

有没有人知道一个更完整的教程,用于创建共享C++类库,它还演示了如何在单独的可执行文件中use个类?这是一个非常简单的教程,展示了对象的创建、使用(简单的getter和setter就可以了),以及删除.链接或引用一些开源代码来说明共享类库的使用也同样好.


Although the answers from codelogic and nimrodm do work, I just wanted to add that I picked up a copy of Beginning Linux Programming since asking this question, and its first chapter has example C code and good explanations for creating and using both static and shared libraries. These examples are available through Google Book Search in an older edition of that book.

推荐答案

myclass.h

#ifndef __MYCLASS_H__
#define __MYCLASS_H__

class MyClass
{
public:
  MyClass();

  /* use virtual otherwise linker will try to perform static linkage */
  virtual void DoSomething();

private:
  int x;
};

#endif

myclass.cc

#include "myclass.h"
#include <iostream>

using namespace std;

extern "C" MyClass* create_object()
{
  return new MyClass;
}

extern "C" void destroy_object( MyClass* object )
{
  delete object;
}

MyClass::MyClass()
{
  x = 20;
}

void MyClass::DoSomething()
{
  cout<<x<<endl;
}

class_user.cc

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

using namespace std;

int main(int argc, char **argv)
{
  /* on Linux, use "./myclass.so" */
  void* handle = dlopen("myclass.so", RTLD_LAZY);

  MyClass* (*create)();
  void (*destroy)(MyClass*);

  create = (MyClass* (*)())dlsym(handle, "create_object");
  destroy = (void (*)(MyClass*))dlsym(handle, "destroy_object");

  MyClass* myClass = (MyClass*)create();
  myClass->DoSomething();
  destroy( myClass );
}

在Mac OS X上,使用以下工具编译:

g++ -dynamiclib -flat_namespace myclass.cc -o myclass.so
g++ class_user.cc -o class_user

在Linux上,使用以下工具编译:

g++ -fPIC -shared myclass.cc -o myclass.so
g++ class_user.cc -ldl -o class_user

如果这是一个插件系统,那么您可以使用MyClass作为基类,并定义所有必需的虚拟函数.然后,插件作者将从MyClass派生,重写virtuals并实现create_objectdestroy_object.您的主应用程序不需要以任何方式进行更改.

Linux相关问答推荐

变量在vim中打印良好,但在bashshell中打印不好,这怎么可能发生?

如何在 shell 脚本中自定义 SFTP 详细输出

计算与文件第一列对应的第二列中的字符串出现次数

x64 NASM 汇编程序在程序开始时显示分段错误

如何在我的 PC 上配置 GitLab 和 GitHub ssh 密钥

在 bash 脚本中保持两个进程处于活动状态(并在死亡时重生它们)

Git在某些文件中添加回车符,尽管autocrlf已关闭

在 Linux 上的 std::threads 中创建子进程

SessionNotCreatedException:无法启动新会话.响应代码 500 在远程服务器上的 Apache Tomcat/10.0.23 上使用 ChromeDriver

仅在 Linux 上出现 AWS RDS `flush tables` 错误的 mysqldump

从 curl 命令输出中获取值

有必要注意非错误提示吗?好像没有找到包裹‘***’?

查找在特定日期从特定机器登录的所有用户

使用 awk 或 sed 删除特定字符

如何在 Linux 中设置目录大小限制?

如何从任意 pthread_t 获取线程 ID?

使用 C++ 和 Linux 的高分辨率计时器?

os.walk 没有隐藏文件夹

linux中netstat和ss的区别?

如何安排 tcpdump 在特定时间段内运行?