The Dynamic Link Library (DLL) is stored separately from the target application and shared among different applications, compared to Static Library. The DLL is the file extension on Windows while on Linux, it is *.so (Shared Object).

The *.so/*.dll can be loaded right before the application starts or during the application’s runtime. On Windows, the Win32 API LoadLibrary is used while on Linux gcc compiler, the dlopen function is used.

CREATING *.SO ON WINDOWS

There are many ways and many languages to create a *.so on Linux e.g. You can write a C or C++ source code and compile it with the following command:

1
gcc -shared -o libhello.so -fPIC hello.c

Example hello.c that exports the hello function:

1
2
3
int hello() {
    return 8; // my lucky number
}

Let’s do this slightly differently. In this post, we learn that you can now use Delphi to compile the source code into Linux 64-bit Server native code directly.. So, let’s do this by creating a tiny Delphi DLL/*.so library. This library exports a single function that takes a double and returns its double value.

build-dll-using-delphi-for-linux-64-bit

And, as you hit F9 to build, you will have the libProject6.so file, the Linux *.so library (if you change target platform to Windows, the code will be compiled into Project6.dll automatically, that is the beauty of the Delphi IDE).

HOW TO USE THE DYNAMIC LINK LIBRARY IN C++ LINUX (GCC COMPILER)?

Copy the file to Ubuntu and create the following C/C++ source code in the same directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// https://helloacm.com/how-to-use-the-dynamic-link-library-in-c-linux-gcc-compiler/
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
 
// function export from the *.so
typedef double (*double_ptr)(double);
 
int main(int argc, char **argv) {
    char *error;
    double_ptr GetDouble;
    void *handle;
    // open the *.so
    handle = dlopen ("./libProject6.so", RTLD_LAZY);
    if (!handle) {
        fputs (dlerror(), stderr);
        exit(1);
    }
    // get the function address and make it ready for use
    GetDouble = (double_ptr)dlsym(handle, "GetDouble");
    if ((error = dlerror()) != NULL)  {
        fputs(error, stderr);
        exit(1);
    }
    // call the function in *.so
    printf ("%f\n", (*GetDouble)(2.0));
    // remember to free the resource
    dlclose(handle);
}

The RTLD_LAZY resolves undefined symbols when code from the dynamic library is executed, which is faster to open. Alternatively, RTLD_NOW resolves all undefined symbols before dlopen returns and fails if this cannot be done.

Compile the source code using (-ldl, the Interfaces for Dynamic Loader):

1
gcc -o test test.cpp -ldl

Run the test which should print a nice:

1
2
root@helloacm.com:/home/delphi# ./test
4.000000

–EOF (The Ultimate Computing & Technology Blog) —

https://helloacm.com/how-to-use-the-dynamic-link-library-in-c-linux-gcc-compiler/
https://helloacm.com/delphi-compiles-code-to-linux-64-bit-server/
https://helloacm.com/quick-review-delphi-10-2-tokyo-preview/

How to Use the Dynamic Link Library in C++ Linux (C++调用Delphi写的.so文件)的更多相关文章

  1. Walkthrough: Creating and Using a Dynamic Link Library (C++)

    Original Link: http://msdn.microsoft.com/zh-cn/library/ms235636.aspx Following content is only used ...

  2. Custom Action : dynamic link library

    工具:VS2010, Installshield 2008 实现功能: 创建一个C++ win32 DLL的工程,MSI 工程需要调用这个DLL,并将Basic MSI工程中的两个参数,传递给DLL, ...

  3. DYNAMIC LINK LIBRARY - DLL

    https://www.tenouk.com/ModuleBB.html MODULE BB DYNAMIC LINK LIBRARY - DLL Part 1: STORY What do we h ...

  4. Walkthrough: Create and use your own Dynamic Link Library (C++)

    参考网站:https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-librar ...

  5. 动态链接库(Dynamic Link Library)学习笔记(附PE文件分析)

    转载:http://www.cnblogs.com/yxin1322/archive/2008/03/08/donamiclinklibrary.html 作者:EricYou 转载请注明出处   注 ...

  6. [DLL] Dynamic link library (dll) 的编写和使用教程

    前一阵子,项目里需要导出一个DLL,但是导出之后输出一直不怎么对,改了半天才算改对...读了一些DLL教程,感觉之后要把现在的代码导出,应该还要花不少功夫...下面教程参照我读的3个教程写成,所以内容 ...

  7. 动态链接库(Dynamic Link Library)

    DLL INTRODUCTION A DLL is a library that contains code and data that can be used by more than one pr ...

  8. How to Create DLL(Dynamic link library)

    该文章属于在YouTube视频上看到的,链接如下: https://www.youtube.com/watch?v=EmDJsl7C9-k&t=3s 1.创建一个工程并建立一个控制台程序 2. ...

  9. Linux Dynamic Shared Library && LD Linker

    目录 . 动态链接的意义 . 地址无关代码: PIC . 延迟版定(PLT Procedure Linkage Table) . 动态链接相关结构 . 动态链接的步骤和实现 . Linux动态链接器实 ...

随机推荐

  1. 学习鸟哥的Linux私房菜笔记(7)——文件查找与文件管理1

    一.可执行文件的搜索 which 显示一个可执行文件的完整路径 按照alias->$PATH的顺序查找 查看系统的环境变量 whereis 搜索一个可执行工具及其相关配置.帮助 slocate ...

  2. 【codeforces 782D】 Innokenty and a Football League

    [题目链接]:http://codeforces.com/contest/782 [题意] 每个队名有两种选择, 然后第一个选择队名相同的那些队只能选第二种; 让你安排队名 [题解] 首先全都选成第一 ...

  3. mysql5.6+主从集的版本号(mysql5.5主机和从机载带后,5.5在设置有一定的差距)

    怎么安装mysql数据库.这里不说了,仅仅说它的主从复制,过程例如以下 在进行主从设置之前 首先确保mysql主从server之间的数据库port防火墙互相打开, 尽量确保主从数据库账户一致性(主从切 ...

  4. Python 第三方库 cp27、cp35 等文件名的含义

    What does version name 'cp27' or 'cp35' mean in Python? 如对于 gensim-0.12.4-cp27-none-win_amd64.whl文件名 ...

  5. 利用WPF建立自己的3d gis软件(非axhost方式)(二)基础状态切换

    原文:利用WPF建立自己的3d gis软件(非axhost方式)(二)基础状态切换   先下载SDK:https://pan.baidu.com/s/1M9kBS6ouUwLfrt0zV0bPew 密 ...

  6. WPF 使用不安全代码快速从数组转 WriteableBitmap

    原文:WPF 使用不安全代码快速从数组转 WriteableBitmap 本文告诉大家一个快速的方法,直接把数组转 WriteableBitmap 先来说下以前的方法,以前使用的是 BitmapSou ...

  7. Java:JSON解析工具-org.json

    一.简介 org.json是Java常用的Json解析工具,主要提供JSONObject和JSONArray类,现在就各个类的使用解释如下. 二.准备 1.在使用org.json之前,我们应该先从该网 ...

  8. jQuery在线选座订座(高铁版)

    除了电影院在线选座,我们还会接触到飞机机舱选座,当然也有汽车票火车票选座的.假如有一天买火车票也提供在线选座,那么今天我来给大家介绍下如何使用jQuery选座插件完成高铁列车座位布置.选座.不同等级座 ...

  9. C# 异步和多线程

    C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿! 说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开 ...

  10. 使用Toast进行用户提醒(转)

    Toast是Android提供的一个轻量级的用户提醒控件,使用也很简单,就相当一个极简的dialog!!!下面将向您介绍一些Toast的详细用法: 1.普遍使用的方法: Context context ...