惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
Google DeepMind News
Google DeepMind News
T
Threatpost
AWS News Blog
AWS News Blog
NISL@THU
NISL@THU
Security Latest
Security Latest
C
Cisco Blogs
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
L
LINUX DO - 热门话题
Know Your Adversary
Know Your Adversary
L
LINUX DO - 最新话题
P
Privacy & Cybersecurity Law Blog
SecWiki News
SecWiki News
O
OpenAI News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
H
Hacker News: Front Page
Scott Helme
Scott Helme
C
Check Point Blog
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
爱范儿
爱范儿
博客园 - 【当耐特】
H
Heimdal Security Blog
The Hacker News
The Hacker News
罗磊的独立博客
C
CERT Recently Published Vulnerability Notes
美团技术团队
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
SegmentFault 最新的问题
Last Week in AI
Last Week in AI
Project Zero
Project Zero
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
宝玉的分享
宝玉的分享
The Register - Security
The Register - Security
A
Arctic Wolf
酷 壳 – CoolShell
酷 壳 – CoolShell
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
Latest news
Latest news
I
InfoQ
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Y
Y Combinator Blog
J
Java Code Geeks
S
Security @ Cisco Blogs

博客园 - 毕海

[原]zeromq框架测试报告 [转]linux下查看进程内存使用情况 程序员必读系列 python 不同版本下载资源 提示“load System.Core failed” phpwind主要表结构的研究随笔[1] [转]Python快速教程 [转]80个Python经典资料(教程+源码+工具)汇总——下载目录 [转]一位大牛整理的python资源 [转]PyInstaller2的信息文件Version的生成 [转]使用PyInstaller2将Python脚本转化为可执行文件(下-进阶使用) [转]使用PyInstaller2将Python脚本转化为可执行文件(上-安装部分) [转]使用PyInstaller2将Python脚本转化为可执行文件(中-使用部分) [转]Nginx安装 [转]Centos 5.5 卸载与安装java1.7环境 在win8 x64上安装Scrapy mongodb协议透传 mongodb的监控与性能优化 记一次MongoDB性能问题
golang的cgo支持调用C++的方法
毕海 · 2015-03-11 · via 博客园 - 毕海

1)swift,貌似官网的推荐

2)extern "C"

我使用后者,用起来比较爽,上代码

c.h

1 #pragma once
2 
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 void test();
7 #ifdef __cplusplus    
8 }
9 #endif

c.c

1 #include "cplus.hpp"
2 #include "c.h"
3 
4 void test() {
5     A *a = new B();
6     a->test();
7 }

cplus.hpp

 1 #pragma once
 2 
 3 class A {
 4     public:
 5         virtual void test();
 6 };
 7 class B: public A {
 8     public:
 9         void test();
10 };

cplus.cpp

#include <iostream>
#include "cplus.hpp"

using namespace std;

void A::test() {
    cout << "a" << endl;
}

void B::test() {
    cout << "b" << endl;
}

build.sh

1 g++ -o cplus.o -c cplus.cpp
2 g++ -o c.o -c c.c
3 ar r libc_test.so c.o cplus.o

test.go

 1 package main
 2 // #cgo LDFLAGS: -L . -lc_test -lstdc++
 3 // #cgo CFLAGS: -I ./
 4 // #include "c.h"
 5 import "C"
 6 
 7 func main(){
 8     
 9     C.test()
10     
11 }

执行顺序

1 ./build.sh
2 go build test.go

posted on 2015-03-11 10:09  毕海  阅读(7542)  评论()    收藏  举报