Linux 上的 C++ 动态共享库 [英] C++ Dynamic Shared Library on Linux

查看:39
本文介绍了Linux 上的 C++ 动态共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是使用 g++ 编译动态共享库的后续.

我正在尝试在 Linux 上用 C++ 创建一个共享类库.我能够编译库,并且可以使用我找到的教程调用一些(非类)函数 此处此处.当我尝试使用库中定义的类时,我的问题就开始了.我链接到的第二个教程展示了如何加载符号以创建库中定义的类的对象,但没有使用这些对象来完成任何工作.

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++ 类库的教程,该教程还展示了如何在单独的可执行文件中使用这些类?一个非常简单的教程,展示了对象的创建、使用(简单的 getter 和 setter 就可以了)和删除会很棒.一个链接或对一些开源代码的引用来说明共享类库的使用也同样好.

Does anyone know of a more complete tutorial for creating shared C++ class libraries that also shows how to use those classes in a separate executable? A very simple tutorial that shows object creation, use (simple getters and setters would be fine), and deletion would be fantastic. A link or a reference to some open source code that illustrates the use of a shared class library would be equally good.

虽然来自 codelogicnimrodm 做工作,我只是想补充一点,我拿起了Beginning Linux Programming 自从问了这个问题,它的第一章有示例 C 代码和很好的解释来创建和使用两者静态库和共享库.这些示例可通过 Google 图书搜索在 该书的旧版.

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 上,编译:

On Mac OS X, compile with:

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

在 Linux 上,编译:

On Linux, compile with:

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

如果这是用于插件系统,您将使用 MyClass 作为基类并定义所有必需的虚拟函数.然后插件作者将从 MyClass 派生,覆盖虚拟并实现 create_objectdestroy_object.您的主应用程序无需以任何方式进行更改.

If this were for a plugin system, you would use MyClass as a base class and define all the required functions virtual. The plugin author would then derive from MyClass, override the virtuals and implement create_object and destroy_object. Your main application would not need to be changed in any way.

这篇关于Linux 上的 C++ 动态共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆