作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 Windows 上编译一个可以与 NativeCall 一起使用的 DLL 库在乐。这是一个最小的 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\sources\947BDAB9F96E0E5FCCB383124F923A6BF6F8D76B (NativeCall) line 298
in block foo at C:\rakudo\share\perl6\core\sources\947BDAB9F96E0E5FCCB383124F923A6BF6F8D76B (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 的头文件
> 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 的开发人员命令提示符”之外,还安装了更多的开发人员提示,即:
> 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 - 如何在 Windows 上编译共享库,以便它可以与 raku 中的 NativeCall 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66510786/
我是一名优秀的程序员,十分优秀!