• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

HowtoUsetheDynamicLinkLibraryinC++Linux(C++调用Delphi写的.so文件)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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/


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
delphidebugrelease区别是什么?发布时间:2022-07-18
下一篇:
DelphiListView基本用法总结发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap