美文网首页
golang cgo

golang cgo

作者: 灵山路远 | 来源:发表于2018-01-14 12:59 被阅读0次

1 cplus.h

class A {

public:

virtual void test();

};

class B: public A {

public:

void test();

};

2 cplus.cpp

#include "cplus.h"

#include <iostream>

using namespace std;

void A::test(){

    cout<<"a"<<endl;

}

void B::test(){

    cout<<"b"<<endl;

}

3 ctest.h

#ifndef __cplusplus

extern "C" {

#endif

      void test();

#ifdef  __cplusplus

}

#endif

4 ctest.cpp

#include "ctest.h"

#include "cplus.h"

void test(){

    A *a = new B();

    a->text();

}

5 bulid.sh

g++ -o cplus.o -c cplus.cpp

g++ -o ctest.o -c ctest.cpp

ar  r libc_test.so c.o cplus.o

6 test.go

package main

//#cgo LDFLAGS: -L . -lc_test -lstdc++

//#cgo CFLAGS: -I ./

//#include "ctest.h"

import "C"

import "fmt"

func main(){

    fmt.Println("vim-go")

    C.test()

}

7 go build test.go

相关文章

网友评论

      本文标题:golang cgo

      本文链接:https://www.haomeiwen.com/subject/dgskoxtx.html