golang CGO在跨平台交叉编译x86到arm64

1.编译环境配置

2.交叉编译工具链安装

具体可以查一下,g++

bash 复制代码
gcc
更新软件包列表:

bash
sudo apt update
安装 GCC 交叉编译器 (C语言):

bash
sudo apt install gcc-aarch64-linux-gnu

安装 G++ 交叉编译器 (C++语言):

bash
sudo apt install g++-aarch64-linux-gnu
bash 复制代码
cq@cq-desktop:~$ aarch64-linux-gnu-gcc --version
aarch64-linux-gnu-gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

cq@cq-desktop:~$ aarch64-linux-gnu-g++ --version
aarch64-linux-gnu-g++ (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

3.头文件修改

进行系统判定,移除 Windows 特有的关键字;

go 复制代码
// Linux 兼容性:移除 Windows 特有的关键字
#ifdef __linux__
    #define __declspec(x)
    #define _stdcall
#endif

#ifndef DLLAPI_EX
#define DLLAPI  __declspec(dllexport)     // export DLL information
#else
#define DLLAPI  __declspec(dllimport)     // import DLL information
#endif

#ifdef __cplusplus
extern "C"
{
#endif

4,so文件修改正确

在so文件名前必须要加lib,必须保证为arm64架构
5.总结,交叉编译成功

我发现linux环境下会更加的严谨,一些细微的错误都能发现,这就是为什么Linux系统稳定的原因吧,比如程序中的大小写错误,win10环境下竟然没发现

相关推荐
徐小黑ACG8 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
北冥you鱼11 小时前
Go 语言空接口(interface{})使用场景与最佳实践
开发语言·windows·golang
徐小黑ACG14 小时前
Golang 基础06 接口interface
开发语言·后端·golang
小小龙学IT14 小时前
Go 语言 cgo 与 C/C++ 互操作深度解析:从 fwlib 直连到工业数采网关实战系列
c语言·c++·golang
牛奔15 小时前
mise 管理多版本 Go 项目
开发语言·前端·chrome·后端·golang
ttwuai17 小时前
Go后台管理系统开源项目有哪些?如何按交付形态筛掉不合适的仓库
开发语言·golang·开源
报错小能手18 小时前
GO面试题 Go基础面试题
golang
皓月盈江19 小时前
Go读写TXT文件
开发语言·后端·golang·go·读写txt文件·bufio·go读写txt
JavaPub-rodert20 小时前
开源一套 Go + React 后台管理系统,支持 RBAC 权限管理、Docker 一键部署!
react.js·golang·开源
FfHUCisI20 小时前
pprof 火焰图(Flame Graph)阅读与热点代码重构实战
架构·golang