Home > Backend Development > C++ > How Can I Create and Use Dynamic Shared Libraries (DSLs) in C on Linux?

How Can I Create and Use Dynamic Shared Libraries (DSLs) in C on Linux?

Mary-Kate Olsen
Release: 2024-12-05 20:24:12
Original
475 people have browsed it

How Can I Create and Use Dynamic Shared Libraries (DSLs) in C   on Linux?

Creating and Using Dynamic Shared Libraries in C on Linux

Dynamic shared libraries (DSLs) allow multiple programs to share code, reducing memory usage and improving efficiency. In C , DSLs enable the creation of reusable class libraries accessible to multiple executables.

Creating the DSL

To create a DSL, define the class interface and implementation in header and source files (e.g., myclass.h and myclass.cc). Ensure the class includes a virtual destructor and public methods marked extern "C" to facilitate symbol loading.

Using the DSL

To utilize a DSL in a separate executable, perform the following steps:

  1. Include necessary libraries () and headers.
  2. Use dlopen() to load the shared library.
  3. Access the library's functions and symbols using dlsym().
  4. Call the create_object() function to instantiate an object from the library class.
  5. Call the object's methods.
  6. Destroy the object using destroy_object().

Example Code

myclass.h

#include <iostream>

class MyClass {
public:
  MyClass();
  virtual void DoSomething();
private:
  int x;
};
Copy after login

myclass.cc

#include "myclass.h"

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

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

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

void MyClass::DoSomething() {
  std::cout << x << std::endl;
}
Copy after login

class_user.cc

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

int main() {
  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 = create();
  myClass->DoSomething();
  destroy(myClass);
}
Copy after login

Compilation and Execution

On Mac OS X:

  • Compile the DSL: g -dynamiclib -flat_namespace myclass.cc -o myclass.so
  • Compile the user executable: g class_user.cc -o class_user

On Linux:

  • Compile the DSL: g -fPIC -shared myclass.cc -o myclass.so
  • Compile the user executable: g class_user.cc -ldl -o class_user

Execute class_user to use the MyClass shared library. It will instantiate and utilize the MyClass object successfully.

The above is the detailed content of How Can I Create and Use Dynamic Shared Libraries (DSLs) in C on Linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template