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

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

use swig -go on windows with Visual Studio

问题

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

  1. //sampel.h
  2. int compute(int a, int b);
  3. //sample.cpp
  4. #include <iostream>
  5. #include "sample.h"
  6. int compute(int a, int b){
  7. int temp = (a+b)*(a-b);
  8. return temp;
  9. }
  10. //sample.i
  11. %module sample
  12. %inline %{
  13. #include "sample.h"
  14. %}
  15. int compute(int a,int b);

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

  1. 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:

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

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

  1. package main
  2. import (
  3. "fmt"
  4. "sample"
  5. )
  6. func main() {
  7. fmt.Println(sample.Compute(3, 4))
  8. }

问题出在这里,当我运行test.go时,出现了错误:

  1. 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.

  1. //sampel.h
  2. int compute(int a, int b);
  3. //sample.cpp
  4. #include &lt;iostream&gt;
  5. #include &quot;sample.h&quot;
  6. int compute(int a, int b){
  7. int temp = (a+b)*(a-b);
  8. return temp;
  9. }
  10. //sample.i
  11. %module sample
  12. %inline %{
  13. #include &quot;sample.h&quot;
  14. %}
  15. int compute(int a,int b);

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

  1. 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:

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

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

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;sample&quot;
  5. )
  6. func main() {
  7. fmt.Println(sample.Compute(3, 4))
  8. }

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

  1. 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:

确定