英文:
Is there an example how to use SWIG to generate C++ buidings with go build?
问题
我想用"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))
}
###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: 错误: 未知的类型名: 'class'
./foo.h:5:15: 错误: 期望 '=', ',', ';', 'asm' 或 'attribute' 在 '{' 之前的标记
/tmp/go-build724218913/github.com/krux02/swig-test/_obj/foo_wrap.c: 在函数 '_wrap_MyClass_set':
/tmp/go-build724218913/github.com/krux02/swig-test/_obj/foo_wrap.c:225:3: 错误: 未知的类型名: 'class'
/tmp/go-build724218913/github.com/krux02/swig-test/_obj/foo_wrap.c:226:3: 错误: 未知的类型名: 'class'
/tmp/go-build724218913/github.com/krux02/swig-test/_obj/foo_wrap.c:229:5: 错误: 未知的类型名: 'class'
[...]
错误很明显,swig试图将所有内容构建为C库。
要测试它,只需复制:
go get github.com/krux02/swig-test
英文:
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
答案1
得分: 5
根据添加Swig支持到go build
的错误报告,看起来它根据文件扩展名来决定如何编译SWIG包装器:
.swig
文件被视为 C 语言.swigcxx
文件被视为 C++ 语言
如果你将 foo.swig
重命名为 foo.swigcxx
,它应该会使用正确的 SWIG 标志和编译器。
英文:
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
得分: 1
根据http://golang.org/cmd/go/上的文档,只有当您的构建文件以.swigcxx结尾时,-c++选项才会传递给SWIG。
英文:
As documented on http://golang.org/cmd/go/, the -c++ option will be passed to SWIG only if your build file ends with .swigcxx.
答案3
得分: 0
请参考Go源代码中的misc/swig/callback文件,其中有一个使用C++的SWIG代码示例。
英文:
See misc/swig/callback in the Go sources for an example of SWIG code that uses C++.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论