简体   繁体   English

GHC:如何使用64位GHC构建32位DLL? (从c ++调用函数)

[英]GHC: how to build a 32-bit DLL using 64-bit GHC? (To call functions from c++)

I'd like to build the 32-bit DLL with 64-bit GHC. 我想用64位GHC构建32位DLL。 And here is the minimal example. 这是最小的例子。

Test.hs Test.hs

{-# LANGUAGE ForeignFunctionInterface #-}
module Test where

import Foreign.C.Types

foreign export ccall c_hello :: IO()
foreign export ccall boo :: CInt

c_hello :: IO()
c_hello = do
    print "Hello!" 

init_exit.cpp init_exit.cpp

#include "Test_stub.h"
#include <C:\Program Files\Haskell Platform\8.0.1\lib\include\Rts.h>

#define DLLExport extern "C" __declspec(dllexport)

DLLExport void hello()
{
    c_hello();    
}

DLLExport int HsStart()
{
    int argc = 1;
    char* argv[] = {"ghcDLL", NULL};

    char** args = argv;
    hs_init(&argc, &args);

    printf("Haskell library has been initialized!\n");
    return 0;
}

DLLExport int HsEnd()
{
    hs_exit();

    printf("Haskell library has been finalized!\n");
    return 0;
}

And then I build the library, using the following commands: 然后我使用以下命令构建库:

ghc -c -O Test.hs 
ghc -c init_exit.cpp 
ghc -shared -o Test.dll Test.o init_exit.o

What flags should I pass to ghc or maybe to gcc to build the 32-bit DLL instead of 64-bit? 我应该将哪些标志传递给ghc或gcc以构建32位DLL而不是64位? Or maybe there is another way to do this. 或许还有另一种方法可以做到这一点。

A normal Windows 64-bit GHC build (such as the one you can download from the GHC website) is only capable of building 64-bit object files. 正常的Windows 64位GHC版本(例如可以从GHC网站下载的版本)只能构建64位目标文件。 For example, it doesn't include 32-bit versions of any of the libraries that come with GHC. 例如,它不包括GHC附带的任何库的32位版本。

Your options are to build a Windows 64-bit to Windows 32-bit cross-compiler, or just run the normal Windows 32-bit build of GHC (probably much easier). 您可以选择构建Windows 64位到Windows 32位交叉编译器,或者只运行正常的Windows 32位GHC版本(可能更容易)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM