Ada制作DLL

我们可以使用gnatdll工具来制作动态链接库。

先贴一个最为简单的代码:

package  myLib is

    -- C方式导出
    function add(a,b:integer) return integer is (a+b);
    pragma export(c,add,"add");

    -- stdcall方式导出
    function sub(a,b:integer) return integer is (a-b);
    pragma export(stdcall,sub,"sub");


end myLib;

输出定义文件需要手工完成。

mylib.def文件内容示例如下:

LIBRARY  "myLib"

EXPORTS

add

sub@8

接着我们用如下命令进行制作

gcc -c myLib.ads

gnatdll -e mylib.def  -d myLib.dll    myLib.ali

gnatdll命令中,要用ali文件,用o文件容易找不到某些函数定位。

接着进行调用测试:

with Ada.Integer_Text_IO;
procedure test is

    pragma linker_options("mylib.dll");

    function "+"(a,b:integer) return integer with Import,external_name=>"add";

    function "-"(a,b:integer) return integer with Import,Convention=>stdcall,external_name=>"sub";

begin

   ada.integer_Text_IO.Put(1+2);
   
   ada.integer_Text_IO.Put(10-6);


end test;

输出:

>test
          3          4
>Exit code: 0

你可能感兴趣的:(Ada)