我想在 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 Tutorial和 SWIG Comparability ,它仅适用于 32 位版本的 Windows:
SWIG 在所有已知的 32 位版本的 Windows 下也能完美运行,包括 95/98/NT/2000/XP。
关于c++ - 在带有 Visual Studio 的 Windows 上使用 swig -go,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15135201/