如何在Windows上编译共享库,使其可以与raku中的NativeCall一起使用? [英] How to compile a shared library on Windows such that it can be used with NativeCall in raku?

查看:71
本文介绍了如何在Windows上编译共享库,使其可以与raku中的NativeCall一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Windows上编译可与Raku中的 NativeCall 一起使用的DLL库..这是最小的C代码( my_c_dll.c ):

I am trying to compile a DLL library on Windows that can be used with NativeCall in Raku. Here is a minimal C code (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上,并且已安装构建工具Visual Studio 2019 .为了编译该DLL,我打开了"VS 2019开发人员命令提示符".并运行:

I am on Windows 10 and have installed Build Tools for Visual Studio 2019. To compile the DLL I open a "Developer Command Prompt for VS 2019" and run:

> 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 )使用它:

This creates a DLL my_c_dll.dll, then I try to use this from Raku (test-dll.raku):

use v6.d;
use NativeCall;
sub foo() is native("./my_c_dll.dll"){ * }
foo();

但是当我运行它(我已经安装了Rakudo版本2020.05.1)时,我得到了:

but when I run this (I have installed Rakudo version 2020.05.1) I get:

> 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

这里可能是什么问题?

推荐答案

无法找到本地库'(null)':错误0xc1

Cannot locate native library '(null)': error 0xc1

错误代码 0xc1 通过使用我观察到它说的是 machine(x86) 32 bit word machine ,因此我怀疑DLL是32位DLL.但是,我在64位计算机上:

I observe that it says machine (x86) and 32 bit word machine, so I suspect the DLL is a 32-bit DLL. However, I am on a 64 bit machine:

> PowerShell -Command "systeminfo | perl -nE 'say if /System Type/'"
System Type:               x64-based PC

事实证明,除了"VS 2019的开发人员命令提示符"之外, Visual Studio 2019的构建工具还安装了更多开发人员提示,即:

It turns out that the Build Tools for Visual Studio 2019 installed some more developer prompts in addition to the "Developer Command Prompt for VS 2019", namely:

  • 用于VS 2019的x64本机工具命令提示符
  • 用于VS 2019的x64_x86交叉工具命令提示符
  • 用于VS 2019的x86本机工具命令提示符
  • 用于VS 2019的x86_x64交叉工具命令提示符

通过打开适用于VS 2019的x64本机工具命令提示符",并在这里重新编译DLL,我现在得到:

By opening a "x64 Native Tools Command Prompt for VS 2019" and recompiling the DLL here, I now get:

> 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可以处理大(> 2GB)地址,这似乎也可以解决此问题:

Notice that the output now says machine (x64) and Application can handle large (>2GB) addresses, and this also seems to fix the problem:

> raku test-dll.raku
Hello from C

这篇关于如何在Windows上编译共享库,使其可以与raku中的NativeCall一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆