从Visual C ++访问Golang模块 [英] Access Golang module from Visual C++

查看:126
本文介绍了从Visual C ++访问Golang模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有这样做的方法?


  1. 开发一个用Golang编写的库L1。 L1为C语言输出函数。
  2. 为Visual C ++构建L1并生成.lib文件。

  3. 通过调用C函数使用Visual C ++代码中的L1在L1中。


解决方案

我从来没有尝试过,而且我使用的是linux,但这里是我所知道的:

根据 golang文档,您可以将代码编译到共享库中(请参阅go help buildmode)。

为了能够从c代码中调用go功能, go功能必须导出

为了将您的代码编译到共享库中,您需要将标准库转换为共享库:

  go install -buildmode = shared std 

这会将所有标准代码编译到libstd中。所以(在Linux上,这个名字可能会在windows上更改)。

最后,您可以使用以下命令来获取共享库:

  go install -buildmode = shared -linkshared [packages] 



<标准共享库可以在以下位置找到:

  GOROOT / pkg / GOOS_GOARCH_dylink / 
  

GOPATH / pkg / GOOS_GOARCH_dylink /

这是一部分。

现在,如果您想从C ++项目中调用此代码,则需要创建包装go库的C库。你可以使用一些工具(我听说过 SWIG ,但从未尝试过)。



编辑:你可以做一些类似的静态去库,但因为你没有指定库类型,你会从C ++代码中使用它,我想你需要一个共享库。 / p>

Is there a way of doing this?

  1. Develop a library L1 written in Golang. L1 exports functions for C language.
  2. Build L1 and generate .lib file for Visual C++.
  3. Use L1 from Visual C++ code by calling C functions in L1.

解决方案

I've never tried, and I'm using linux, but here is what I know:

According to the golang documentation you can compile go code into shared library (see go help buildmode also).

To be able to call go function from c code, go function shall be exported.

In order to compile your go code into a shared library, you need to get the go standard library into a shared one too:

go install -buildmode=shared std

This will compile all the go standard code into libstd.so (on linux, the name might change on windows).

And finally, you can use the following command to get your shared library:

go install -buildmode=shared -linkshared [packages]

The standard shared library can be found in:

GOROOT/pkg/GOOS_GOARCH_dylink/ 

and your shared library under:

GOPATH/pkg/GOOS_GOARCH_dylink/

That is for the go part.

Now, if you want to call this code from a C++ project, you'll need to create the C library that wraps the go library. You can use some tool for that (I've heard about SWIG, but never tried).

EDIT: You can do something similar with static go library, but since you did not specify the library type and you will use it from C++ code, I suppose you need a shared library.

这篇关于从Visual C ++访问Golang模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆