使用静态C++使用swig从go代码内部的库

发布于 2025-01-31 05:40:24 字数 2217 浏览 3 评论 0原文

我如何使用c ++ - 使用静态C ++的代码 - go go 使用 swig (简化包装器和接口生成器)?

(注意:以下文章已经描述了答案和工作解决方案)

我正在使用SWIG在我的C ++ - 代码和我的file文件之间实现桥梁。 C ++ - 代码位于.cpp文件中,进而调用我创建的静态.A C ++库中的方法。

问题是,如何适当地设置所有零件以从我的GO代码中调用静态库中的方法。

这是我的琐碎设置:

simple.cpp

#include "Simple.h"

int DoSimple()
{
    return 1701;
}

simple.h

int DoSimple();

foo.cpp

#include "include/Foo.h"

int foo()
{
    return DoSimple();
}

foo.h

#include "d:\\home\\workspaces\\simple\\include\\Simple.h"

int foo();

swig .i

%module simple

%{
#include "include/Foo.h"
%}

%include "include/Foo.h"

main.go

package main

import "fmt"
import "awesomeProject/foo"

func main() {
    fmt.Println("Hello")

    a := simple.Foo()

    fmt.Println(a)

    fmt.Println("Bye")
}

我正在创建这两个文件中的静态和共享的库:

$ g++ -c -o Simple.o Simple.cpp -Id:/home/workspaces/simple/include
$ g++ -shared -o libSimple.so Simple.o
$ ar rcs libSimple.a Simple.o

然后我最终将拥有:

|-- simple/
       |-- include/
       |    |-- Simple.h
       |-- libSimple.a
       |-- libSimple.so
       |-- Simple.cpp
       |-- Simple.o


|-- foo/
      |-- main/
      |    |-- main.go
      |-- swig/
      |    |-- include/
      |           |-- Foo.h
      |    |-- Foo.cpp
      |    |-- simple.go
      |    |-- swig.i
      |    |-- swig_wrap.cxx

两个相关的GO环境变量设置为:

$ go env -w CGO_LDFLAGS="-Ld:/home/workspaces/simple -lSimple"
$ go env -w CGO_CXXFLAGS=-Id:/home/workspaces/simple/include

最后,我正在发表:

$ swig -go -c++ -intgosize 64 ../foo/swig.i
$ go build -x .\main.go 

惊喜,令人惊讶,输出是

$ go run main.go
Hello
1701
Bye

如此,一切都很好。

How can I use C++-code that uses static C++-libraries from within a go file using SWIG (Simplified Wrapper and Interface Generator)?

(Note: The following post already describes the answer and a working solution)

I am using SWIG to implement the bridge in-between my C++-code and my go-file. The C++-code resides in a .cpp-file, which in turn calls a method from a static .a C++-library that I created.

The question is, how to set up all parts appropriately in order to call a method from the static library from my go code.

This is my trivial setup:

Simple.cpp

#include "Simple.h"

int DoSimple()
{
    return 1701;
}

Simple.h

int DoSimple();

Foo.cpp

#include "include/Foo.h"

int foo()
{
    return DoSimple();
}

Foo.h

#include "d:\\home\\workspaces\\simple\\include\\Simple.h"

int foo();

swig.i

%module simple

%{
#include "include/Foo.h"
%}

%include "include/Foo.h"

main.go

package main

import "fmt"
import "awesomeProject/foo"

func main() {
    fmt.Println("Hello")

    a := simple.Foo()

    fmt.Println(a)

    fmt.Println("Bye")
}

I am creating the static and shared libraries out of these two files as so:

$ g++ -c -o Simple.o Simple.cpp -Id:/home/workspaces/simple/include
$ g++ -shared -o libSimple.so Simple.o
$ ar rcs libSimple.a Simple.o

I am then ending up having:

|-- simple/
       |-- include/
       |    |-- Simple.h
       |-- libSimple.a
       |-- libSimple.so
       |-- Simple.cpp
       |-- Simple.o


|-- foo/
      |-- main/
      |    |-- main.go
      |-- swig/
      |    |-- include/
      |           |-- Foo.h
      |    |-- Foo.cpp
      |    |-- simple.go
      |    |-- swig.i
      |    |-- swig_wrap.cxx

The two relevant go environment variables are set to:

$ go env -w CGO_LDFLAGS="-Ld:/home/workspaces/simple -lSimple"
$ go env -w CGO_CXXFLAGS=-Id:/home/workspaces/simple/include

Finally, I am issuing:

$ swig -go -c++ -intgosize 64 ../foo/swig.i
$ go build -x .\main.go 

And surprise, surprise, the output is

$ go run main.go
Hello
1701
Bye

So, all good.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文