在Windows上使用Visual Studio和swig -go。

huangapple go评论90阅读模式
英文:

use swig -go on windows with Visual Studio

问题

我想在Windows上使用golang调用带有swig的C++ dll。(在Linux上使用gc编译器是成功的。)但是遇到了一些问题。这里是示例代码。

//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);

现在,我使用以下命令生成包装文件:

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

然后在VS中创建一个空的dll项目,将sample.h添加到Header Files,将sample.cpp和sample_wrap.cxx添加到Source Files,将sample.i添加到项目中。

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

使用以下命令生成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在同一个目录中)?谢谢!如果你需要我遗漏的额外信息,请随时提问。

Alex

英文:

I want to use golang call c++ dll with swig on windows. (gc compiler, on Linux was successful.) But there have some problems. Here is the sample.

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

//sample.cpp
#include &lt;iostream&gt;
#include &quot;sample.h&quot;

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

//sample.i
%module sample

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

Now, I use this cmd to generate the wrap files:

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

Then create an empty dll project in VS, add sample.h too Header Files, add sample.cpp and sample_wrap.cxx to Source Files, add sample.i to the project.

Build Solution, generates sample.dll

Use cmd as below to generate 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

Next, install sample.a(to avoid some issue), then run the test.go:

package main

import (
    &quot;fmt&quot;
    &quot;sample&quot;
)

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

The problem is here, when I run test.go, got the error:

adddynlib: unsupported binary format

How do I fix the problem(dll and test.go is in the same directory)? Thanks! If you need extra information that I missed, just ask.

Alex

答案1

得分: 1

根据SWIG教程SWIG兼容性,它只适用于32位版本的Windows:

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

英文:

According to SWIG Tutorial and SWIG Comparability, it works just with 32 bit versions of Windows:

SWIG also works perfectly well under all known 32 bit versions of Windows including 95/98/NT/2000/XP.

huangapple
  • 本文由 发表于 2013年2月28日 20:08:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/15135201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定