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条)

报告相同问题?

悬赏问题

  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并