Golang 调用 C/C++ 库

参考 参考 参考

目录结构:

|-importC
|  |-lib
|  |  |-libvideo.dll
|  |  |-libvideo.so
|  |-include
|  |  |-video.h
|  |  |-video.c
|  |-main.go

C的代码如下:

头文件 video.h

//video.h
#ifndef VIDEO_H
#define VIDEO_H
void exeFFmpegCmd(); // 声明
int sum(int a,int b);
#endif

源文件 video.c

#include <stdio.h>
#include "video.h"

void exeFFmpegCmd(){ // 实现
    printf("finish");
}

int sum(int a,int b){
    return (a+b);
}

编译:使用 gcc 或 g++ 生成 .so库,或 win 下生成 dll, 将.so库放到lib目录下(可以任意放,但在go的代码中要能找到)

命令:gcc video.c -fPIC -shared -o libvideo.so

go的代码:

package main

/*
#cgo CFLAGS: -Iincludes
#cgo LDFLAGS: -Llib -lvideo -lstdc++
#include "video.h"
*/
import "C"
import "fmt"

func main() {
	C.exeFFmpegCmd()
	GoSum(5,6)
}

func GoSum(a,b int) {
	s := C.sum(C.int(a),C.int(b))
	fmt.Println(s)
}

CFLAGS中的-I(大写的i) 参数表示.h头文件所在的路径
LDFLAGS中的-L(大写) 表示.so文件所在的路径 -l(小写的L) 表示指定该路径下的库名称,比如要使用libhi.so,则只需用-lhi (省略了libhi.so中的lib和.so字符)表示。

CFLAGS对应C语言特有的编译选项、
CXXFLAGS对应是C++特有的编译选项、
CPPFLAGS则对应C和C++共有的编译选项

LDFLAGS 对应库.so链接选项

 C 数值类型与 Go 中的访问类型对应关系 

C 类型名称

Go 类型名称
charC.char
signed charC.schar
unsigned charC.uchar
shortC.short
unsighed shortC.ushort
intC.int
unsigned intC.uint
longC.long
unsigned longC.ulong
long longC.longlong
unsigned long longC.ulonglong
floatC.float
doubleC.double
complex floatC.complexfloat
complex doubleC.complexdouble
void*unsafe.Pointer
__int128_t   __uint128_t[16]byte

常见问题:

问题1:

./main: error while loading shared libraries: libvideo.so: cannot open shared object file: No such file or directory

直接度娘 error while loading shared libraries,就会知道其实就是没有找到这个动态库,我们编辑:

vim /etc/ld.so.conf 文件,将我们的路径写在该文件下,如图,编辑好后执行:ldconfig

问题2:

./include/check-reg.h:4:18: fatal error: glib.h: No such file or directory
 #include <glib.h>
                  ^
compilation terminated.

由于在头文件中 #include <glib.h> 导入了glib.h,go build时报错,找不到

解决: 首先确保系统安装了glib-2.0, sudo apt-get install libglib2.0-dev

然后在命令行执行 pkg-config --cflags glib-2.0,找到glib的获取编译器标志

 在go代码CFLAGS中告诉cgo在使用gcc编译时,glib的依赖在哪里

package main


/*
#cgo CFLAGS: -Iinclude -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
#cgo LDFLAGS: -L${SRCDIR}/lib -lreg-check

#include "check-reg.h"

*/
import "C"
import (
	"fmt"
	"net"
	"encoding/json"

)

使用CGO的项目交叉编译问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值