GO程序调用C及C++库的实现方式

 

还有这个:

https://blog.csdn.net/zdy0_2004/article/details/79124269

https://blog.csdn.net/boshuzhang/article/details/83503147

 

GO程序调用C及C++库的实现方式

2017年05月09日 16:24:13  阅读数:10787

 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhuyunfei/article/details/71480290

一.windows下go调用c函数的实现方法

1)直接在go文件中使用

<span style="color:#000000"><code>package main
/*
int add(int a,int b){
return a+b;
}
*/
import "C"

import (
    "fmt"
)

func main() {
    fmt.Println("Hello World!")
    fmt.Println(C.add(2, 1))
}
</code></span>

 

执行结果:

<span style="color:#000000"><code>Hello World!
3
</code></span>

2)使用C函数

<span style="color:#000000"><code>// main.go
package main

/*
#include <stdlib.h>
*/
import "C"

import (
    "fmt"
)

func main() {
    fmt.Println("Hello World!")
    fmt.Println(C.rand())
}
</code></span>

3)使用动态库方法

a)go工程目录结构

<span style="color:#000000"><code>|-project
|  |-lib
|  |  |-Algorithm.dll
|  |-include
|  |  |-Algorithm.h
|  |-src
|  |  |-main.go
|  |-pkg
|  |-bin
</code></span>

b)go调用C&C++动态库

.h

<span style="color:#000000"><code>//Algorithm.h
#ifndef ALGORITHM_DLL_H
#define ALGORITHM_DLL_H

namespace Algorithm {
    extern "C" __declspec(dllexport) int add(int a, int b);
    extern "C" __declspec(dllexport) int sub(int a, int b);
}

#endif // !ALGORITHM_DLL_H
</code></span>

.cpp

<span style="color:#000000"><code>#include "Algorithm.h"

int Algorithm::add(int a, int b)
{
    return a + b;
}

int Algorithm::sub(int a, int b)
{
    return a - b;
}
</code></span>

c)main.go代码

<span style="color:#000000"><code>//方法一
// main.go
package main

import (
    "fmt"
)

import (
    "syscall"
)

func callDll() {
    dll32 := syscall.NewLazyDLL("lib/Algorithm.dll")
    fmt.Println("call dll:", dll32.Name)
    g := dll32.NewProc("add")
    ret, _, _ := g.Call(uintptr(4), uintptr(5))
    fmt.Println("result is :", ret)
}

func main() {
    fmt.Println("Hello World!")
    callDll()
    //fmt.Println(C.add(2, 1))
}
</code></span>

.

<span style="color:#000000"><code>//方法二
// main.go
package main

/*
#cgo CFLAGS: -Iinclude
#cgo LDFLAGS: -Llib -lAlgorithm
#include "Algorithm.h"
*/
import "C"

import (
    "fmt"
)

func main() {
    fmt.Println("Hello World!")

    ret := int32(C.add(2, 3))

    if ret != 0 {
        fmt.Println("1111")
    } else {
        fmt.Println("2222")
    }
}
</code></span>

PS:方法二需要将动态库和go程序放在同一目录下

4.环境搭建

需要安装如下程序

mingw-w64-install.exe

注意事项:安装此程序安装过程中要选择64位版本的,安装完后要将mingw的bin目录添加到path环境变量中

其实这里的32位版本或者64位版本的要求就是要求和go下使用的gcc版本(即32位或64位一致)

使用vs生成动态库,注意此动态库版本一定要和go编译环境下的gcc的环境版本要一样(win32&win64的区别)

总结:

1.import “C” 一定要紧跟注释后的语句

2.此语句不能和其他包一起导入

3.程序目录下包含库

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI视觉网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值