duangu6588 2014-01-19 17:22
浏览 71
已采纳

有示例如何使用SWIG来通过go build生成C ++建筑物吗?

I want to build my project with "go build". I've not found any documentation how to integrate swig with the go build process. And also very important, it should be a C++ example C is easy.

foo.swig

/* foo.i */
%module foo
%{
#include "foo.h"
%}
%include "foo.h"

foo.h

#pragma once
int foo(int a, int b);
class MyClass {
    int a,b,c;
public:
    MyClass(int a, int b, int c): a(a),b(b),c(c) {}
    int foo() {
        return (a+b)*c;
    }
    int bar() {
        return a*(b+c);
    }
};

foo.cpp

#include "foo.h"
int foo(int a, int b){
    return a*b;
}

main.go

package foo
import "fmt"
func main(args []string) {
    fmt.Println("Hello World!")
    fmt.Println(foo.foo(12, 17))
}

output

arne@ad-X201t ~/g/s/g/k/swig-test> go build
# github.com/krux02/swig-test
In file included from $WORK/github.com/krux02/swig-test/_obj/foo_wrap.c:194:0:
./foo.h:5:1: Fehler: unbekannter Typname: »class«
./foo.h:5:15: Fehler: expected »=«, »,«, »;«, »asm« or »__attribute__« before »{« token
/tmp/go-build724218913/github.com/krux02/swig-test/_obj/foo_wrap.c: In Funktion »_wrap_MyClass_set«:
/tmp/go-build724218913/github.com/krux02/swig-test/_obj/foo_wrap.c:225:3: Fehler: unbekannter Typname: »class«
/tmp/go-build724218913/github.com/krux02/swig-test/_obj/foo_wrap.c:226:3: Fehler: unbekannter Typname: »class«
/tmp/go-build724218913/github.com/krux02/swig-test/_obj/foo_wrap.c:229:5: Fehler: unbekannter Typname: »class«
[...]

the error is obvious, swig tries to build everything as a C library.

to test it, just copy:

go get github.com/krux02/swig-test
  • 写回答

3条回答 默认 最新

  • duanao4729 2014-01-20 02:09
    关注

    Looking at the bug report that added Swig support to go build, it looks like it decides how to compile the SWIG wrapper based on the file extension:

    • .swig files are treated as C
    • .swigcxx files are treated as C++

    If you rename foo.swig to foo.swigcxx, it should use the correct SWIG flags and compiler.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图