Bonjour :-) 您的论点中似乎缺少对win32ada库的引用。gnatdll您可能想试试这个(注意最后三个附加参数):
gnatdll -k -d hello_dll.dll obj\hello_dll.ali -I"C:\GNAT\2017\lib\win32ada.relocatable" -largs -lwin32ada
但是,话虽如此,我实际上认为使用 GNAT 项目文件而不是使用gnatdll. 根据您的示例(我对其进行了一些修改),我从下面的文件和命令行中一次性创建了一个 DLL。如果构建成功,该lib目录将包含一个 DLL:
> gprbuild -p -XWIN32ADA_BUILD=relocatable -P .\hello.gpr
Setup
[mkdir] object directory for project Hello
[mkdir] library directory for project Hello
Compile
[Ada] hello.adb
Build Libraries
[gprlib] hello.lexch
[bind SAL] hello
[Ada] b__hello.adb
[link library] libhello.dll
和
src/hello.ads
package Hello is
procedure Initialize
with Export, Convention => C;
procedure Finalize
with Export, Convention => C;
procedure Say_Hello
with Export, Convention => C;
end Hello;
src/hello.adb
with System; use System;
with Interfaces.C.Strings;
with Win32;
with Win32.Windef;
with Win32.Winuser;
package body Hello is
----------------
-- Initialize --
----------------
procedure Initialize is
procedure helloinit with Import; -- Generated by binder.
begin
helloinit;
end Initialize;
--------------
-- Finalize --
--------------
procedure Finalize is
procedure hellofinal with Import; -- Generated by binder.
begin
hellofinal;
end Finalize;
---------------
-- Say_Hello --
---------------
procedure Say_Hello is
package C_Str renames Interfaces.C.Strings;
C_Text : C_Str.chars_ptr := C_Str.New_String ("Hello World!");
C_Caption : C_Str.chars_ptr := C_Str.New_String ("A Hello World Dialog.");
Result : Win32.Int := 0;
begin
Result := Win32.Winuser.Messagebox
(hWnd => Null_Address,
lpText => Win32.To_Pcstr (C_Text),
lpCaption => Win32.To_Pcstr (C_Caption),
uType => 0);
-- Don't forget to free the allocated strings.
C_Str.Free (C_Text);
C_Str.Free (C_Caption);
end Say_Hello;
end Hello;
你好.gpr
with "win32ada";
library project Hello is
for Library_Kind use "dynamic";
for Library_Name use "hello";
for Library_Interface use ("hello");
-- The "Library_Auto_Init" option is set to False. This will make the binder
-- generate initialization and finalization functions (in this case
-- "helloinit" and "hellofinal"). It's a good habit to wrap these
-- and call these wrappers from the client application.
for Library_Auto_Init use "False";
for Library_Dir use "lib";
for Object_Dir use "obj";
for Source_Dirs use ("src");
end Hello;
lib/libhello.def(导出的符号,区分大小写,这里必须小写)
LIBRARY LIBHELLO
EXPORTS
initialize
finalize
say_hello
额外:使用 Visual Studio 2019 测试 DLL
为了测试 DLL,我决定使用 Visual Studio CE 2019 创建一个小型应用程序。我首先使用库管理器(lib工具)创建了一个导入库。从“x64 Native Tools Command Prompt for VS 2019”我调用:
> lib /machine:X64 /def:./lib/libhello.def /out:./lib/libhello.lib
Microsoft (R) Library Manager Version 14.26.28806.0
Copyright (C) Microsoft Corporation. All rights reserved.
Creating library libhello.lib and object libhello.exp
然后,我创建了一个新文件夹,并用下面的源文件和之前创建的文件填充了它libhello.dll,libhello.a并且libhello.exp. 我终于使用了(再次,来自“x64 Native Tools Command Prompt for VS 2019”)
cl HelloApp.cpp libhello.lib
Microsoft (R) C/C++ Optimizing Compiler Version 19.26.28806 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
HelloApp.cpp
Microsoft (R) Incremental Linker Version 14.26.28806.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:HelloApp.exe
HelloApp.obj
libhello.lib
这个新HelloApp.exe显示(如预期的那样)一个对话框。请注意,libhello.dll依赖于 GNAT 运行时库(检查依赖 walker),因此请确保(在您的情况下)C:\GNAT\2017\bin在运行应用程序时位于搜索路径上(或将所需的 DLL 复制到与 相同的文件夹中HelloApp.exe)。
你好.h
#pragma once
extern "C"
{
extern void say_hello();
extern void initialize();
extern void finalize();
}
HelloApp.cpp
#include <windows.h>
#include "hello.h"
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
UNREFERENCED_PARAMETER(hInstance);
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(pCmdLine);
UNREFERENCED_PARAMETER(nCmdShow);
initialize();
say_hello();
finalize();
return 0;
}