如何在 Windows 上编译共享库,以便它可以与 raku 中的 NativeCall 一起使用?
How to compile a shared library on Windows such that it can be used with NativeCall in raku?
我正在尝试在 Windows 上编译一个 DLL 库,它可以在 Raku 中与 NativeCall 一起使用。这是一个最小的 C 代码 (my_c_dll.c
):
#include <stdio.h>
#define EXPORTED __declspec(dllexport)
extern __declspec(dllexport) void foo();
void foo()
{
printf("Hello from C\n");
}
我在 Windows 10 并安装了 Build Tools for Visual Studio 2019。为了编译 DLL,我打开了“VS 2019 的开发人员命令提示符”和 运行:
> cl.exe /c my_c_dll.c
> link /DLL /OUT:my_c_dll.dll my_c_dll.obj
这会创建一个 DLL my_c_dll.dll
,然后我尝试在 Raku (test-dll.raku
) 中使用它:
use v6.d;
use NativeCall;
sub foo() is native("./my_c_dll.dll"){ * }
foo();
但是当我 运行 这个(我已经安装了 Rakudo 版本 2020.05.1)时,我得到:
> raku test-dll.raku
Cannot locate native library '(null)': error 0xc1
in method setup at C:\rakudo\share\perl6\core\sources7BDAB9F96E0E5FCCB383124F923A6BF6F8D76B (NativeCall) line 298
in block foo at C:\rakudo\share\perl6\core\sources7BDAB9F96E0E5FCCB383124F923A6BF6F8D76B (NativeCall) line 594
in block <unit> at test-dll.raku line 9
这里可能是什么问题?
Cannot locate native library '(null)': error 0xc1
错误代码 0xc1
是 Windows system error code :
ERROR_BAD_EXE_FORMAT
193 (0xC1)
%1 is not a valid Win32 application.
通过使用 dumpbin
检查 DLL 的 header
> dumpbin /headers my_c_dll.dll
Microsoft (R) COFF/PE Dumper Version 14.26.28806.0
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file my_c_dll.dll
PE signature found
File Type: DLL
FILE HEADER VALUES
14C machine (x86)
4 number of sections
6043A8CB time date stamp Sat Mar 6 17:07:39 2021
0 file pointer to symbol table
0 number of symbols
E0 size of optional header
2102 characteristics
Executable
32 bit word machine
DLL
我观察到它说 machine (x86)
和 32 bit word machine
,所以我怀疑 DLL 是 32 位 DLL。但是,我在 64 位机器上:
> PowerShell -Command "systeminfo | perl -nE 'say if /System Type/'"
System Type: x64-based PC
原来 Build Tools for Visual Studio 2019
除了“VS 2019 的开发人员命令提示”之外,还安装了一些开发人员提示,即:
- 适用于 VS 2019 的 x64 本机工具命令提示符
- x64_x86 适用于 VS 2019 的交叉工具命令提示符
- 适用于 VS 2019 的 x86 本机工具命令提示符
- x86_x64 适用于 VS 2019 的交叉工具命令提示符
通过打开“VS 2019 的 x64 本机工具命令提示符”并在此处重新编译 DLL,我现在得到:
> dumpbin /headers my_c_dll.dll
Microsoft (R) COFF/PE Dumper Version 14.26.28806.0
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file my_c_dll.dll
PE signature found
File Type: DLL
FILE HEADER VALUES
8664 machine (x64)
6 number of sections
6043AAA2 time date stamp Sat Mar 6 17:15:30 2021
0 file pointer to symbol table
0 number of symbols
F0 size of optional header
2022 characteristics
Executable
Application can handle large (>2GB) addresses
DLL
请注意,输出现在显示 machine (x64)
和 Application can handle large (>2GB) addresses
,这似乎也解决了问题:
> raku test-dll.raku
Hello from C
我正在尝试在 Windows 上编译一个 DLL 库,它可以在 Raku 中与 NativeCall 一起使用。这是一个最小的 C 代码 (my_c_dll.c
):
#include <stdio.h>
#define EXPORTED __declspec(dllexport)
extern __declspec(dllexport) void foo();
void foo()
{
printf("Hello from C\n");
}
我在 Windows 10 并安装了 Build Tools for Visual Studio 2019。为了编译 DLL,我打开了“VS 2019 的开发人员命令提示符”和 运行:
> cl.exe /c my_c_dll.c
> link /DLL /OUT:my_c_dll.dll my_c_dll.obj
这会创建一个 DLL my_c_dll.dll
,然后我尝试在 Raku (test-dll.raku
) 中使用它:
use v6.d;
use NativeCall;
sub foo() is native("./my_c_dll.dll"){ * }
foo();
但是当我 运行 这个(我已经安装了 Rakudo 版本 2020.05.1)时,我得到:
> raku test-dll.raku
Cannot locate native library '(null)': error 0xc1
in method setup at C:\rakudo\share\perl6\core\sources7BDAB9F96E0E5FCCB383124F923A6BF6F8D76B (NativeCall) line 298
in block foo at C:\rakudo\share\perl6\core\sources7BDAB9F96E0E5FCCB383124F923A6BF6F8D76B (NativeCall) line 594
in block <unit> at test-dll.raku line 9
这里可能是什么问题?
Cannot locate native library '(null)': error 0xc1
错误代码 0xc1
是 Windows system error code :
ERROR_BAD_EXE_FORMAT
193 (0xC1)
%1 is not a valid Win32 application.
通过使用 dumpbin
检查 DLL 的 header> dumpbin /headers my_c_dll.dll
Microsoft (R) COFF/PE Dumper Version 14.26.28806.0
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file my_c_dll.dll
PE signature found
File Type: DLL
FILE HEADER VALUES
14C machine (x86)
4 number of sections
6043A8CB time date stamp Sat Mar 6 17:07:39 2021
0 file pointer to symbol table
0 number of symbols
E0 size of optional header
2102 characteristics
Executable
32 bit word machine
DLL
我观察到它说 machine (x86)
和 32 bit word machine
,所以我怀疑 DLL 是 32 位 DLL。但是,我在 64 位机器上:
> PowerShell -Command "systeminfo | perl -nE 'say if /System Type/'"
System Type: x64-based PC
原来 Build Tools for Visual Studio 2019
除了“VS 2019 的开发人员命令提示”之外,还安装了一些开发人员提示,即:
- 适用于 VS 2019 的 x64 本机工具命令提示符
- x64_x86 适用于 VS 2019 的交叉工具命令提示符
- 适用于 VS 2019 的 x86 本机工具命令提示符
- x86_x64 适用于 VS 2019 的交叉工具命令提示符
通过打开“VS 2019 的 x64 本机工具命令提示符”并在此处重新编译 DLL,我现在得到:
> dumpbin /headers my_c_dll.dll
Microsoft (R) COFF/PE Dumper Version 14.26.28806.0
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file my_c_dll.dll
PE signature found
File Type: DLL
FILE HEADER VALUES
8664 machine (x64)
6 number of sections
6043AAA2 time date stamp Sat Mar 6 17:15:30 2021
0 file pointer to symbol table
0 number of symbols
F0 size of optional header
2022 characteristics
Executable
Application can handle large (>2GB) addresses
DLL
请注意,输出现在显示 machine (x64)
和 Application can handle large (>2GB) addresses
,这似乎也解决了问题:
> raku test-dll.raku
Hello from C