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

查看:148
本文介绍了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 ++类库,也显示如何使用这些类在单独的可执行文件?一个非常简单的教程,显示对象创建,使用(简单的getters和setters会很好),删除将是太棒了。链接或对一些开源代码的引用说明了共享类库的使用,同样是好的。

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.

虽然来自 codelogic nimrodm 做工作,我只是想补充说,我拿起一个开始Linux编程自从提出这个问题,它的第一章有示例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 / p>

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
$ b

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


$ b b

如果这是一个插件系统,你将使用MyClass作为基类并定义所有必需的函数virtual。然后,插件作者将从MyClass派生,覆盖虚拟并实现 create_object destroy_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天全站免登陆