Home > Backend Development > C++ > How to Create and Use a C Dynamic Shared Library on Linux?

How to Create and Use a C Dynamic Shared Library on Linux?

Mary-Kate Olsen
Release: 2024-12-08 02:39:14
Original
222 people have browsed it

How to Create and Use a C   Dynamic Shared Library on Linux?

C Dynamic Shared Library on Linux

Dynamic shared libraries (DSLs), also known as shared libraries or shared objects, offer the capability to separate code into reusable modules in C programming. This enables code sharing between multiple programs, reduces code duplication, and allows for easier maintenance.

Creating a Shared Class Library

In C , creating a shared class library involves defining a header file (.h) and a source file (.cc) for the class implementation. The header file should declare the class interface, while the source file provides the implementation. To create a shared library containing these files:

#include "myclass.h"</p>
<h1>include <iostream></h1>
<p>using namespace std;</p>
<p>MyClass::MyClass()<br>{<br>  x = 20;<br>}</p>
<p>void MyClass::DoSomething()<br>{<br>  cout << x << endl;<br>}

External Linkage

Using symbols prefixed with extern "C", the external linkage instructs the compiler to make functions available to the outside world. This is necessary when calling functions from the shared library in other programs.

Using the Shared Library

To utilize the shared class library in a separate executable, follow these steps:

  1. Loading the Library: Use dlopen to load the shared library into memory.
  2. Retrieving Function Addresses: Utilize dlsym to get the addresses of create_object and destroy_object functions.
  3. Creating an Object: Invoke the create_object function to instantiate an object of the class.
  4. Using the Object: Call member functions, like DoSomething, on the created object.
  5. Destroying the Object: Finally, call the destroy_object function to deallocate the object.

Example Usage

The following code snippet illustrates how to use a shared class library:

#include <dlfcn.h></p>
<h1>include <iostream></h1>
<h1>include "myclass.h"</h1>
<p>using namespace std;</p>
<p>int main(int argc, char **argv) {<br>  MyClass<em> myClass = (MyClass</em>)create();<br>  myClass->DoSomething();<br>  destroy( myClass );<br>}

Compilation

For Mac OS X:

g++ -dynamiclib -flat_namespace myclass.cc -o myclass.so
g++ class_user.cc -o class_user
Copy after login

For Linux:

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

By using shared libraries, developers can enhance their code reusability, maintenance, and scalability in C programming. Dynamic linking enables the sharing of code between programs, optimizing memory usage and improving the overall performance of software systems.

The above is the detailed content of How to Create and Use a C Dynamic Shared Library on Linux?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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