gpt4 book ai didi

c++ - 是否有一个示例如何使用 SWIG 通过 go build 生成 C++ 建筑?

转载 作者:IT王子 更新时间:2023-10-29 01:34:02 29 4
gpt4 key购买 nike

我想用“go build”构建我的项目。我还没有找到任何关于如何将 swig 与 go 构建过程集成的文档。而且也很重要,它应该是一个 C++ 示例 C 很容易。

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

输出

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«
[...]

错误很明显,swig 尝试将所有内容构建为 C 库。

要测试它,只需复制:

go get github.com/krux02/swig-test

最佳答案

查看 bug report that added Swig support to go build ,看起来它决定如何根据文件扩展名编译 SWIG 包装器:

  • .swig 文件被视为 C
  • .swigcxx 文件被视为 C++

如果将 foo.swig 重命名为 foo.swigcxx,它应该使用正确的 SWIG 标志和编译器。

关于c++ - 是否有一个示例如何使用 SWIG 通过 go build 生成 C++ 建筑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21220034/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com