c++ - 在带有 Visual Studio 的 Windows 上使用 swig -go

标签 c++ dll go swig cgo

我想在 Windows 上使用带有 swig 的 golang call c++ dll。 (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,在Source Files中加入sample.cpp和sample_wrap.cxx,在工程中加入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在同一个目录)?谢谢!如果您需要我遗漏的额外信息,请直接询问。

亚历克斯

最佳答案

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

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

关于c++ - 在带有 Visual Studio 的 Windows 上使用 swig -go,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15135201/

相关文章:

c++ - Turbo C++ 中的彩色输出

c++ - C++ 和 OpenCV 中的 SSE 均值滤波器

c++ - 字符编码困惑!

c++ - C++中的名称修饰或名称修饰

c++ - 现有 DLL 函数的 Typedef 返回函数错误

c++ - 从 DLL 导出函数释放 CStringArray& 参数时出现堆冲突

go - 如何从 PKCS#12 容器中提取私钥并将其保存为 PKCS#8 格式?

c++ - Qt 中的消息未被翻译

go - 运行发出并发请求的 go 程序时,打开的文件太多/没有这样的主机错误

go - 如何使用嵌入从表中获取数据