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环境下竟然没发现

相关推荐
geovindu1 天前
go: Recursion Algorithm
开发语言·后端·算法·golang·递归算法
Java面试题总结1 天前
Go 语言大白话入门 6 - 判断与循环
开发语言·后端·golang
techdashen2 天前
Go 1.26 新增 `bytes.Buffer.Peek`:只看数据,不移动读取位置
开发语言·后端·golang
ttwuai2 天前
Go 后台接口 401/403 排查:JWT 过期、刷新请求和权限码怎么定位
开发语言·golang·状态模式
techdashen2 天前
Go 1.25 新增 `reflect.TypeAssert`:更直接、更高效地从 `reflect.Value` 取出类型值
开发语言·后端·golang
geovindu2 天前
go: Enumeration Algorithm
开发语言·后端·算法·golang·枚举算法
Wang's Blog2 天前
Go-Zero基础入门5: 探究go-zero如何基于gRPC扩展客户端
开发语言·后端·golang·go-zero
江畔柳前堤3 天前
GO01-Go 语言与主流编程语言深度对比
开发语言·人工智能·后端·微服务·云原生·golang·go
Hazenix3 天前
Go 指南:一篇文章速通 Golang
开发语言·后端·golang
灯澜忆梦3 天前
GO_复合类型---指针
开发语言·后端·golang