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

相关推荐
利威尔xu1 小时前
2026-11-18-Go接口幂等设计实战
开发语言·后端·golang
利威尔xu1 天前
Go Mutex 饥饿模式:正常模式等待者饿死,1ms 阈值自动切换崩溃
开发语言·后端·面试·golang·并发编程
利威尔xu2 天前
Go channel 关闭 3 个 panic:向已关闭 channel 发送,服务当场崩溃
开发语言·后端·golang·并发编程·channel
Java展博2 天前
[Go 后端转场] 从 try-catch 到 if err != nil:写惯 Java 的人第一次上手最别扭的 4 件事
java·开发语言·golang
联犀3 天前
ai时代复杂项目的golang项目如何更好的与ai共舞:monorepo+gozero
开发语言·人工智能·golang
codeejun3 天前
每日一Go·MySQL-15、慢查询日志实战分析(从 0 到 1 实战排查慢 SQL)
sql·mysql·golang
程序员Query6 天前
day01-认识Go语言 + golang环境的安装
开发语言·后端·golang
喵了几个咪7 天前
GoWind Flow 拆解:一个 Go 工作流引擎与它的双模式设计器
开发语言·人工智能·后端·golang
codeejun7 天前
每日一Go·MySQL-14、慢 SQL 产生原因全解
sql·mysql·golang
利威尔xu10 天前
Go map 底层崩溃:3 个 goroutine 并发写同一张 map,fatal error 崩掉整个服务
golang·源码·map·并发·后端开发