How to use std::vector or other container in cgo of golang?

不打扰是莪最后的温柔 提交于 2019-12-05 12:33:08
James Henstridge

While you can use C++ with CGo, you can't embed that code inside the .go file, since it ultimately gets built with a C compiler.

Instead, place your dosome function in a separate .cpp file in the same directory as the .go file, and declare your function to use C linkage. For example:

extern "C" {
    void dosome() {
        vector<int> ivec;
        ...
    }
}

If you include a prototype for the function in the CGo comment in the .go file so you can call it from Go.

Since you have multiple files now, you can't use the go run foo.go shorthand any more (since it only compiles a single file). Instead, you will need to use go run package or go build package, where your code is located at $GOPATH/src/package.

Uhh I think your conclusions are a bit too fast. GC cost is driven by two things: The more garbage your program produces, the more the GC will have to run. Second: The more pointers there are to scan, the longer a single GC will take.

That is to say: as long as you put your 100 million things into a go slice and keep them there: the GC won't have to run much, because there's no garbage. And second: if your things don't contain pointers, GC time, should it still occur, will be fine.

So, my question is: do your things have pointers?

if you just want to call someone's golang code this is a quick ram inefficient way:

package main

import "C"
import "fmt"
import "unsafe"

func intArrayFromC (src unsafe.Pointer, sz int) []uint64 {
    dest := make([]uint64, sz)
    copy(dest, (*(*[1000000000]uint64)(unsafe.Pointer(src)))[:sz:sz])// big number dose not affect ram.
    return dest
}

//export doPrint
func doPrint(src unsafe.Pointer, sz int){
    var numbers []uint64 = intArrayFromC(src, sz);

    for i := 0; i < len(numbers); i++ {
        fmt.Printf("%d  index: %d\n", numbers[i], i)
    }
}

func main() {}

and the c++ code:

#include "print.h"
#include <string.h>
#include <vector>

int main() {
    std::vector<GoUint64> numbers{99,44,11,00,2,33,44};

    while (1) {
        doPrint(numbers.data(), numbers.size());
    }
    return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!