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

相关推荐
程序员小八7774 小时前
Go 语言特性
开发语言·后端·golang
X1A0RAN8 小时前
pytest 动态报告:数据可视化篇 —— 用 Golang + Echo 打造自动化测试任务可视化平台
信息可视化·golang·pytest
圣殿骑士-Khtangc11 小时前
Go-sync包并发原语详解从Mutex到Once的深度应用
golang
灯澜忆梦11 小时前
【基于GO的Web开发15】gin路由和路由组
前端·后端·golang·gin
xcLeigh13 小时前
Go入门:rune与byte的区别和使用场景
android·javascript·golang
深念Y13 小时前
数据库层设计的取舍:ORM 便利性与手写 SQL 的安全性权衡
java·数据库·sql·golang·框架·语言·ome
深念Y13 小时前
Makefile vs go run:Go 项目构建方式对比
java·开发语言·golang·k8s·编译·流水线·cicd
西瓜很甜哟13 小时前
golang每日十题
golang
灯澜忆梦14 小时前
【基于GO的Web开发14】gin请求重定向
前端·后端·golang·gin
路多辛14 小时前
为什么用 Go 写 AI Agent,covo-agent 的选型思考
开发语言·golang·agent·ai编程