我可以在cgo中使用c ++吗?

Is it possible to mix in some C++ code in cgo?

I tried this:

package main
/* 
    #include <iostream>

    extern "C" void test(const char* str)
    {
        std::cout << str;
    }
*/
// #cgo CFLAGS: -x c++
// #cgo LDFLAGS: -lstdc++
import "C"

func main() {
    C.test(C.CString("Testing!!!"))
}

But I get these errors:

error: 'char* CString(_GoString_)' cannot appear in a constant-exp
error: 'void test(const char*)' cannot appear in a constant-expres
error: invalid conversion from 'char* (*)(_GoString_)' to 'long long int' [-fpermissive]
error: invalid conversion from 'void (*)(const char*)' to 'long long int' [-fpermissive]

I'm using go1.0.2 and MinGW-w64 4.7.1

dpjmljx90897323
dpjmljx90897323 code.google.com/p/go/issues/detail?id=1476
接近 8 年之前 回复

1个回答



@ephemient在Go Bug跟踪器中提供了对此功能请求的链接。 进而提供了指向如何在Go中使用C ++的链接。</ a >有关堆栈溢出的信息。 那里有一个很好的讨论,但是对我来说,收获是:</ p>


  1. Go常见问题解答的链接( Go程序是否与C / C ++程序链接?):</ p> \ n


    ... cgo程序提供了“外部函数接口”的机制,以允许从Go代码安全地调用C库。 SWIG将此功能扩展到了C ++库。</ p>
    </ blockquote> </ li>

  2. Go的SWIG文档。</ p> </ li>
    </ ol>
    </ div>

展开原文

原文

@ephemient provided a link to the feature request for this in the Go bug tracker. That in turn provided a link back to How to use C++ in Go? here on Stack Overflow. There's a good discussion there, but the takeaways for me were:

  1. The link to the Go FAQ (Do Go programs link with C/C++ programs?):

    ... The cgo program provides the mechanism for a “foreign function interface” to allow safe calling of C libraries from Go code. SWIG extends this capability to C++ libraries.

  2. The link to The SWIG documentation for Go.

drjtua5953
drjtua5953 -伟大的更新链接,谢谢。
一年多之前 回复
douduan9129
douduan9129 Swig 3.0已发布:swig.org/Doc3.0/Go.html#Go
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐