hijacker 发表于 2025-4-9 08:50:13

OpenHarmony编译环境搭建 (兼容至4.0-Master和主干分支)

预备工作
1. 安装虚拟机工具VMware或者VirtualBox。
2. 新建虚拟机,内存16GB及以上,硬盘100GB及以上。
3. 安装Ubuntu,推荐使用20.04版本。用户名不能包含中文。
4. 启动并进入Ubuntu虚拟机,以下步骤将在Ubuntu虚拟机中进行操作。

一、将Shell环境修改为bash
sudo dpkg-reconfigure dash
选择“No”。


二、替换Ubuntu软件源
在“https://mirrors.ustc.edu.cn/repogen/”下载对应版本最新的源。


在下载好的文件(sources.list)所在的位置开启一个终端窗口,执行下列命令。

备份原始文件:
sudo cp /etc/apt/sources.list /etc/apt/source.list.bak
替换源:
sudo mv -f sources.list /etc/apt/
更新软件包索引:
sudo apt update
三、安装必要的库和工具
需要分两步进行安装。注意,这里比较重要,官网文档用一步进行安装,会出现问题,导致安装不完整,从而导致后期编译出现“3000”错误。
sudo apt-get install binutils binutils-dev git git-lfs gnupg flex bison gperf build-essential zip curl zlib1g-dev   libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip m4 bc gnutls-bin python3.8 python3-pip ruby genext2fs device-tree-compiler make libffi-dev e2fsprogs pkg-config perl openssl libssl-dev libelf-dev libdwarf-dev u-boot-tools mtd-utils cpio doxygen liblz4-tool openjdk-8-jre gcc g++ texinfo dosfstools mtools default-jre default-jdk libncurses5 apt-utils wget scons python3.8-distutils tar rsync git-core libxml2-dev lib32z-dev grsync xxd libglib2.0-dev libpixman-1-dev kmod jfsutils reiserfsprogs xfsprogs squashfs-tools pcmciautils quota ppp libtinfo-dev libtinfo5 libncurses5-dev libncursesw5 libstdc++6 gcc-arm-none-eabi vim ssh locales libxinerama-dev libxcursor-dev libxrandr-dev libxi-devsudo apt-get install gcc-arm-linux-gnueabi gcc-9-arm-linux-gnueabi
四、配置Python
设置默认Python解释器,python和python3软链接为python3.8:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1python --version
安装python包管理工具(pip3):
sudo apt install python3-pip
配置pip软件包更新源:
mkdir ~/.pippip3 config set global.index-url https://mirrors.huaweicloud.com/repository/pypi/simplepip3 config set global.trusted-host mirrors.huaweicloud.compip3 config set global.timeout 120
五、安装Samba服务
用于在开发环境中使用VS Code编写代码。执行下列命令安装samba并打开配置文件:
sudo apt install sambasudo gedit /etc/samba/smb.conf
在配置文件末尾添加以下内容:
comment = User Homespath = /homeguest ok = nowritable = yesbrowsable = yescreate mask = 0755directory mask = 0755
建立用户并设置密码(请将下面的用户名换成您自己的):
sudo smbpasswd -a dragon
重启samba:
sudo service smbd restart
六、获取源代码的必要工具和配置
安装git-lfs,会自动安装git:
sudo apt install git-lfs
安装repo和requests:
wget https://gitee.com/oschina/repo/raw/fork_flow/repo-py3sudo mv repo-py3 /usr/local/bin/reposudo chmod a+x /usr/local/bin/repopip3 install -i https://repo.huaweicloud.com/repository/pypi/simple requests
配置git(请将下面的邮箱和用户名换成您自己的):
git config --global user.email "dragon@hbu.cn"git config --global user.name "dragon"git config --global credential.helper storegit config --global --add safe.directory "*"
七、拉取源码
以4.0为例(可以拉取branch或tag,这里使用branch):
mkdir -p ~/openharmony/4.0cd ~/openharmony/4.0repo init -u https://gitee.com/openharmony/manifest -b OpenHarmony-4.0-Release --no-repo-verifyrepo sync -crepo forall -c 'git lfs pull'
在源码根目录下执行prebuilts脚本,安装编译器及二进制工具:
bash build/prebuilts_download.sh
八、安装编译工具
在源码根目录运行如下命令安装hb并更新至最新版本:
python3 -m pip install --user build/hb
设置环境变量:
echo 'export PATH=~/.local/bin:$PATH' >> ~/.bashrcsource ~/.bashrc
九、编译
两种方式,使用hb或者build.sh。
1. 使用hb。在源码根目录运行如下命令(选择要编译的产品并进行编译):
hb sethb build
2. 使用build.sh。命令格式为:
./build.sh --product-name {product_name} --ccache
例如,在源码根目录运行如下命令:
./build.sh --product-name rk3568 --ccache
耐心等待,对于标准系统来说,完整编译过程会持续2-3小时。

Shoubuliao 发表于 2025-4-9 08:51:03

<div class="vditor-reset">按照上述文章步骤下拉和编译,4.0 release和master编译也会报错,不知该如何处理,部分log如下: ld.lld: error: undefined symbol: __aarch64_cas4_acq_rel... ld.lld: error: .btf.vmlinux.bin.o: unknown file type... exceptions.ohos_exception.OHOSException: ninja phase failed Code:      4000

dddddno1 发表于 2025-4-9 08:51:39

回复 martinhu: 会不会是源码有点问题呢,之前3.2的出现报错重新拉取源码搭好环境编译就没问题了,但是4.0的我尝试好多次了一样的错误,还是说还有什么地方还差着什么没有配置吗。。。
榜单了解详情')">有用 榜单了解详情')">无用 回复举报



马迪
发表于 2023-11-17 09:54 IP属地:江苏省扬州市


建议是从[这里]((https://gitee.com/openharmony/do ... ony-v4.0-release.md))拿代码

分支上的都随时有人提交,不一定稳定





榜单了解详情')">有用 榜单了解详情')">无用 回复举报





dragon
发表于 2023-11-18 09:16 IP属地:河北省唐山市


回复 lucky1: 虚拟机内存分配了多少?



榜单了解详情')">有用 榜单了解详情')">无用 回复举报





martinhu
发表于 2023-11-20 08:31 IP属地: - 中国湖北省襄阳市 中国电信IDC


回复 lucky1: 看到好多人编译4.0 代码遇到4000报错,源码可能有跟编译环境相关的bug



榜单了解详情')">有用 榜单了解详情')">无用 回复举报





lihonggang
发表于 2024-5-16 17:51 IP属地:山东省济南市


回复 马迪: 你这个页面已经打不开了



榜单了解详情')">有用 榜单了解详情')">无用 回复举报



【5 条回复】 查看全部点评

dragon
虚拟机内存分配了多少? 发表于 2023-11-18 09:15



                                          回复举报
                  

                                                <div class="ssfv" key="2" >                  <div id="post_4641" style="position: relative" class="a0a viewbox otherfloor cl" >            
<div class="viewinfo">
               alexsuo
板凳                                    发表于 2024-1-21 09:23:30


<div class="pct"><div class="pcb"><div class="t_fsz">用同样的办法编译dayu210 却会失败,博主有没有经验可以分享?

伊阳进 发表于 2025-4-9 08:51:46

搭建环境有困难的同学,可以使用我的在线平台试试,目前已经正式发布
使用方法参考我之前发的文章或查看在线帮助视频

https://lengqinjie.xyz/lab/index

whjfp 发表于 2025-4-9 08:51:53

回复 冷钦街: 登陆在线平台后提示我更换浏览器是什么问题?我用edge和chrome都试了,还是同样的问题
榜单了解详情')">有用 榜单了解详情')">无用 回复举报



冷钦街
发表于 2024-2-15 20:54 IP属地: - 中国四川省 中国电信IDC


回复 faceoh: 可能是浏览器不支持,更大的可能是网络不流畅,更换一下网络环境,可能就好了。



榜单了解详情')">有用 榜单了解详情')">无用 回复举报





2964213324
发表于 2024-6-12 21:36 IP属地:福建省泉州市


回复 冷钦街: 哥不行耶



榜单了解详情')">有用 榜单了解详情')">无用 回复举报



【3 条回复】

                                          回复举报
                  

                                                <div class="ssfv" key="4" >                  <div id="post_5073" style="position: relative" class="a0a viewbox otherfloor cl" >            
<div class="viewinfo">
               xyshi
5#                                    发表于 2024-2-19 14:29:01


<div class="pct"><div class="pcb"><div class="t_fsz">https://www.vmware.com/products/ ... pro-evaluation.html

wybt 发表于 2025-4-9 08:52:38

博主写的很全

flzt120 发表于 2025-4-9 08:52:48

请问为什么我是用hb build只读取配置而不编译呢
The run time for _prebuild_and_preload is 0.01 s

Checking all build args...

subsytem config scan completed

attribt 发表于 2025-4-9 08:53:41

回复 pengbiao: 相同问题,请问您解决了吗?或者有什么思路吗?

榜单了解详情')">有用 榜单了解详情')">无用 回复举报

【1 条回复】

                                          回复举报
                  

                                                <div class="ssfv" key="7" >                  <div id="post_8010" style="position: relative" class="a0a viewbox otherfloor cl" >      https://forums-obs.openharmony.cn/avatar/noavatar.svg   
<div class="viewinfo">
               haha0000
8#                                    发表于 2024-7-29 10:47:03


<div class="pct"><div class="pcb"><div class="t_fsz">安装好环境之后,就可以在任意目录下编译程序是吗,C可以吗

hijacker 发表于 2025-4-9 08:54:02

ACTION //third_party/typescript:build_typescript_pack(//build/toolchain/ohos:ohos_clang_arm)
FAILED: obj/third_party/typescript/ohos-typescript-4.9.5-r4.tgz
/usr/bin/env ../../third_party/typescript/compile_typescript.py /home/oo/hm5.0/third_party/typescript/ /home/oo/hm5.0/out/rk3568/obj/third_party/typescript
npm error code ERR_INVALID_URL
npm error Invalid URL
npm error A complete log of this run can be found in: C:\Users\oo\AppData\Local\npm-cache\_logs\2025-04-08T01_00_47_911Z-debug-0.log

Traceback (most recent call last):
File "../../third_party/typescript/compile_typescript.py", line 56, in <module>
    main(sys.argv)
File "../../third_party/typescript/compile_typescript.py", line 52, in main
    run_pack(source_path, dest_out_path)
File "../../third_party/typescript/compile_typescript.py", line 46, in run_pack
    run_cmd(["npm", "pack"], dest_out_path)
File "../../third_party/typescript/compile_typescript.py", line 30, in run_cmd
    raise Exception(stderr.decode())
Exception: npm error code ERR_INVALID_URL
npm error Invalid URL
npm error A complete log of this run can be found in: C:\Users\oo\AppData\Local\npm-cache\_logs\2025-04-08T01_00_47_911Z-debug-0.log
请问这个错误怎么办
页: [1]
查看完整版本: OpenHarmony编译环境搭建 (兼容至4.0-Master和主干分支)