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. 关于hexo的SEO的好文章

    1.hexo高阶教程:想让你的博客被更多的人在搜索引擎中搜到吗? 2.Hexo Seo优化让你的博客在google搜索排名第一 3.hexo 博客 seo 优化 4.HEXO SEO 高级优化 5.H ...

  3. CUDA多个流的使用

    CUDA中使用多个流并行执行数据复制和核函数运算可以进一步提高计算性能.以下程序使用2个流执行运算: #include "cuda_runtime.h" #include < ...

  4. eclipse中JUnit工具的使用

  5. win10系统应用打不开

    可能有一些用户升级Win10之后遇到了应用商店.应用打不开或闪退的问题,此时可尝试通过下面的一些方法来解决. 1.点击任务栏的搜索(Cortana小娜)图标,输入Powershell,在搜索结果中右键 ...

  6. c#调api串口通讯

    原文:c#调api串口通讯 在调试ICU通信设备的时候,由于串口通信老出现故障,所以就怀疑CF实现的SerialPort类是否有问题,所以最后决定用纯API函数实现串口读写. 先从网上搜索相关代码(关 ...

  7. Parse陨落,开发者服务今后路在何方?

    Parse为开发者提供移动应用的后台服务,包括数据存储.消息推送及用户管理等等.因此方便开发者可专心在客户端的制作,简化服务器端的设计. 关于 Parse 关停 2016年1月28日,Parse 官方 ...

  8. ng-zorro 子菜单

    效果 代码 // 初始化菜单 // res.menu是一个Menu数组 // 在后端创建子菜单 res.menu.push({ text: "", i18n: "子菜单& ...

  9. oracle_set运营商

     oracle_set运营商 ①UNION/UNION ALL UNION 运算符返回的结果集,并设置两个查询.对于这两组重复部分的.重复数据删除 例: select id,name from t ...

  10. C#控制台关闭之前做一些操作

    using System; using System.Runtime.InteropServices; class Program { static void Main(string[] args) ...