前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c语言调用go封装的动态库步骤及减小体积包的方法

c语言调用go封装的动态库步骤及减小体积包的方法

作者头像
杨永贞
发布2020-08-04 11:01:13
2.9K1
发布2020-08-04 11:01:13
举报

嫌go编译后的动态库或静态库供c代码或嵌入式终端使用,体积太大?upx工具解决这一问题。

upx真是一个神器,再也不用担心和抱怨go在嵌入式linux应用上体积包大了。

1.go build添加 -ldflags="-w -s" 会去除 DWARF调试信息、符号信息

``` go build -ldflags="-w -s" ota_main.go ```

``` go build -buildmode=c-shared -o test.so ``` -buildmode=c-shared requires exactly one main package

注意:生成C可调用的so时,Go源代码需要以下几个注意。

必须导入 “C” 包

必须在可外部调用的函数前加上 【//export 函数名】的注释

必须是main包,切含有main函数,main函数可以什么都不干

2.优化方案 第二步:压缩优化

执行命令: ``` upx.exe -9 *.exe ``` upx-3.96-amd64_linux.tar.xz

打开文件夹

upx工具 解压后放到 /usr/bin目录下就可以直接使用了

编译为c动态库用的什么指令?

增加一个属性:-buildmode=c-archive

-buildmode=c-shared 这个是动态库

举例:一个go文件编译初始值:7M

去调试信息编译:5.2M

upx处理后:1.9M

go调用c代码一例:

代码语言:javascript
复制
package main
/*
#include <stdio.h>
#include <stdlib.h>
void c_print(char *str) {
 printf("%s\n", str);
}
*/
import "C"  //import “C” 必须单起一行,并且紧跟在注释行之后
import "unsafe"

func main() {
 s := "Hello Cgo"
 cs := C.CString(s)//字符串映射
 C.c_print(cs)//调用C函数
 defer C.free(unsafe.Pointer(cs))//释放内存
}

1、go代码中的C代码,需要用注释包裹,块注释和行注释均可。

其次import “C”是必须的,并且和上面的C代码之间不能用空行分割,必须紧密相连

如果执行go run **时出现

# command-line-arguments could not determine kind of name for xxx

那么就需要考虑 是不是improt “C”和上面的C代码没有紧挨着导致了

2、import “C” 并没有导入一个名为C的包,这里的import “C”类似于告诉Cgo将之前注释块中的C代码生成一段具有包装性质的Go代码

3、访问C语言中的函数需要在前面加上C.前缀,如C.Cstring C.go_print C.free

4、对于C语中的原生类型,Cgo都有对应的Go语言中的类型 如go代码中C.int,C.char对应于c语言中的int,signed char,而C语言中void*指针在Go语言中用特殊的unsafe.Pointer(cs)来对应。

而Go语言中的string类型,在C语言中用字符数组来表示,二者的转换需要通过go提供的一系列函数来完成:

C.Cstring : 转换go的字符串为C字符串,C中的字符串是使用malloc分配的,所以需要调用C.free来释放内存。

C.Gostring : 转换C字符串为go字符串。

C.GoStringN : 转换一定长度的C字符串为go字符串。

typedef signed char GoInt8;//对应go代码中的int8类型

typedef struct { const char *p; GoInt n; } GoString;//对应go中的字符串

需要注意的是每次转换都会导致一次内存复制,所以字符串的内容是不可以修改的。

5、利用defer C.free 和unsafe.Pointer显示释放调用C.Cstring所生成的内存块。

go代码中的main函数是必须的,有main函数才能让cgo编译器去把包编译成c的库。

import “C”是必须的,如果没有import “C” 将只会build出一个.a文件,而缺少.h文件。

//exoort 函数名 这里的函数名要和下面的的go函数名一致,并且下面一行即为要导出的go函数。

举例:

代码语言:javascript
复制
package main

import "fmt"
import "C"

//export test
func test() {
	fmt.Printf("hello go test0\n")
}

//export test1
func test1(a, b int) int {
	fmt.Printf("hello go test1\n")
	return 0
}

//export test2
func test2(a, b string) (int, error) {
	fmt.Printf("hello go test2,a=%s,b=%s\n", a, b)
	return 0, nil
}

//export test3
func test3(a, b []byte) {
	fmt.Printf("hello go test3\n")
	fmt.Printf("%#v,%#v\n", a, b)
}

func main() {
	fmt.Printf("hello go,this is main\n")
}

go build -buildmode=c-shared -o test.so

c语言中调用:

代码语言:javascript
复制
#include <stdio.h>
#include <string.h>
#include "test.h"
int main()
{
   printf("c test,use go so lib\n");
   test();
   test1(1,2);
   struct test2_return r;
   char cvalue[] = "Hello This is a C Application";
   int length = strlen(cvalue);
   GoString value = {cvalue, length};//go中的字符串类型在c中为GoString
   
   r = test2(value,value);
   GoSlice p1,p2;
   char a1[5],a2[5];
   p1.data = a1;
   p1.len =5;
   p1.cap = 5;
   
   p2.data = a2;
   p2.len =5;
   p2.cap = 5;
   
   test3(p1,p2);
   
   return 0;
}

test.h头文件如下:

代码语言:javascript
复制
/* Code generated by cmd/cgo; DO NOT EDIT. */

/* package test */


#line 1 "cgo-builtin-prolog"

#include <stddef.h> /* for ptrdiff_t below */

#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H

typedef struct { const char *p; ptrdiff_t n; } _GoString_;

#endif

/* Start of preamble from import "C" comments.  */




/* End of preamble from import "C" comments.  */


/* Start of boilerplate cgo prologue.  */
#line 1 "cgo-gcc-export-header-prolog"

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;

/*
  static assertion to make sure the file is being used on architecture
  at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];

typedef _GoString_ GoString;
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue.  */

#ifdef __cplusplus
extern "C" {
#endif


extern void test();

extern GoInt test1(GoInt p0, GoInt p1);

/* Return type for test2 */
struct test2_return {
	GoInt r0;
	GoInterface r1;
};

extern struct test2_return test2(GoString p0, GoString p1);

extern void test3(GoSlice p0, GoSlice p1);

#ifdef __cplusplus
}
#endif

gcc test.c test.so 生成了a.out

./a.out运行。

Go导出dll如何返回struct等复杂结构?

Go type not supported in export: struct

因为export里不支持Go语言的struct。

正确的写法如:

代码语言:javascript
复制
package main

/*
struct Info  {
	int x;
};
*/
import "C"

import (
	"log"
)

//export Show
func Show() C.struct_Info{
	log.Printf("Return: %d", 110)
    return C.struct_Info{x:0}
}

func main() {
	Show();
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-06-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档