【问题标题】:use swig -go on windows with Visual Studio在带有 Visual Studio 的 Windows 上使用 swig -go
【发布时间】:2013-02-14 14:58:53
【问题描述】:

我想在 Windows 上使用 golang call c++ dll with swig。 (gc编译,在Linux上是成功的。)但是有一些问题。这是示例。

//sampel.h
int compute(int a, int b);

//sample.cpp
#include <iostream>
#include "sample.h"

int compute(int a, int b){
    int temp = (a+b)*(a-b);
    return temp;
}

//sample.i
%module sample

%inline %{
    #include "sample.h"
%}
int compute(int a,int b);

现在,我使用这个 cmd 来生成包装文件:

swig -c++ -go -soname sample.dll -intgosize 64 sample.i

然后在VS中创建一个空的dll项目,将sample.h也加入头文件,将sample.cpp和sample_wrap.cxx加入Source Files,将sample.i加入项目。

构建解决方案,生成sample.dll

如下使用cmd生成sample.a:

go tool 6g sample.go
go tool 6c -I C:\Go\pkg\windows_amd64 sample_gc.c
go tool pack grc sample.a sample.6 sample_gc.6

接下来,安装 sample.a(避免一些问题),然后运行 ​​test.go:

package main

import (
    "fmt"
    "sample"
)

func main() {
    fmt.Println(sample.Compute(3, 4))
}

问题就在这里,当我运行 test.go 时,得到了错误:

adddynlib: unsupported binary format

我该如何解决这个问题(dll 和 test.go 在同一个目录中)?谢谢!如果您需要我错过的额外信息,请询问。

亚历克斯

【问题讨论】:

    标签: c++ dll go swig cgo


    【解决方案1】:

    根据SWIG TutorialSWIG Comparability,它仅适用于 32 位版本的 Windows:

    SWIG 在包括 95/98/NT/2000/XP 在内的所有已知 32 位版本的 Windows 下也能完美运行。

    【讨论】:

    • 感谢您的回答,我将在 32 位版本的 Windows 上试用。
    • 我认为它在 2021 年也适用于 64 位 Windows SWIG works on 32-bit and 64-bit versions of Windows from swig.org/compat.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    相关资源
    最近更新 更多