使用Swig从Go中使用C++类

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

Using C++ classes from Go using Swig

问题

我想在Windows 7上使用swig将C++类从Go中调用,当我构建项目"go build main.go"时,出现以下错误:

c:..\Temp\go-build591352403/swtest.a(swtest_wrap.o): malformed pe file: __cgo_topofstack: invalid symbol binding 105

我正在使用32位的go 1.3、32位的gcc 4.8.1和swig 3.0在Windows 7上。当我在Windows 7上使用64位的Go和GCC时,也会出现相同的错误。

但是,我能够在Ubuntu上使用64位的go和gcc成功构建和运行。

我在Windows上漏掉了什么吗?

以下是文件结构和内容:

main(文件夹)

  • main.go

      package main
    
      import (
        "swtest"
      )
    
      func main() {
        swtest.NewSwt().Print("Swig test!")
      }
    

swtest(文件夹)

  • swtest.cpp

      #include "swtest.h"
    
      void Swt::Print(const std::string& s)
      {
          std::cout << s;
          std::cout << std::endl;
      }
    
  • swtest.h

      #ifndef SWTEST_H
      #define SWTEST_H
    
      #include <string>
      #include <iostream>
    
      class Swt
      {
      public:
          void Print(const std::string& s);
      };
    
      #endif
    
  • swtest.go

      package swtest
    
  • swtest.swigcxx

      %module swtest
      %include "std_string.i"
    
      %{
      #include "swtest.h"
      %}
    
      %include "swtest.h"
    
英文:

I want to use C++ class from Go using swig in Windows7

When I build the project "go build main.go" I am getting following error:

> c:\..\Temp\go-build591352403/swtest.a(swtest_wrap.o): malformed pe file: __cgo_topofstack: invalid symbol binding 105

I am using go 1.3 32bit, gcc 4.8.1 32bit and swig 3.0 in Windows7.
I see same error when I use 64bit Go and GCC in Windows7.

I am able build and run successfully on Ubuntu with 64-bit go and gcc.

Am I missing something in windows?

Here is the file structure and content.

main (folder)

  • main.go

      package main
    
      import (
        &quot;swtest&quot;
      )
    
      func main() {
        swtest.NewSwt().Print(&quot;Swig test!&quot;)
      }
    

swtest (folder)

  • swtest.cpp

      #include &quot;swtest.h&quot;
    
      void Swt::Print(const std::string&amp; s)
      {
          std::cout &lt;&lt; s;
          std::cout &lt;&lt; std::endl;
      }
    
  • swtest.h

       #ifndef SWTEST_H
       #define SWTEST_H
    
       #include &lt;string&gt;
       #include &lt;iostream&gt;
    
       class Swt
       {
       public:
          void Print(const std::string&amp; s);
       };
    
       #endif  
    
  • swtest.go

       package swtest
    
  • swtest.swigcxx

      %module swtest
      %include &quot;std_string.i&quot;
    
      %{
      #include &quot;swtest.h&quot;
      %}
    
      %include &quot;swtest.h&quot;
    

答案1

得分: 0

如果你感觉胆大,Windows上的pe错误已在Go 1.5beta1中修复。试试看吧!下载1.5 beta2

英文:

If you are feeling bold, the pe error on windows is fixed in Go 1.5beta1. Give it a spin! Download 1.5 beta2

答案2

得分: 0

请升级到Go 1.5。这个问题在Go 1.5中已经解决了。
在之前的Go版本中,这是Windows操作系统中已知的问题。Go团队已经在1.5中修复了这个问题。
请查看以下帖子,其中会提供更多信息。

Go在Windows中使用SWIG出错

英文:

Please upgrade to Go 1.5. This problem is solved in Go 1.5.
It was known problem in Windows OS in prior Go versions. Go team has fixed in 1.5.
Please take a look at following thread which will give more information.

Go with SWIG error in Windows

huangapple
  • 本文由 发表于 2015年2月18日 16:55:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/28579454.html
匿名

发表评论

匿名网友

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

确定