1、程序簡介
該程序是基于OpenHarmony的C++公共基礎(chǔ)類庫的簡單案例:HelloWorld。
該應用案例已在OpenHarmony凌蒙派-RK3568開發(fā)板(即OpenHarmony-v3.2.1-release)運行正常,詳細說明及案例源代碼可參考:https://gitee.com/Lockzhiner-Electronics/lockzhiner-rk3568-openharmony/tree/master/samples/a21_utils_helloworld
2、程序解析
2.1、創(chuàng)建編譯引導
在//vendor/lockzhiner/rk3568/ohos.build添加編譯模塊系統(tǒng)名稱。
{ "parts": { "product_rk3568": { "module_list": [ "http://vendor/lockzhiner/rk3568/default_app_config:default_app_config", "http://vendor/lockzhiner/rk3568/image_conf:custom_image_conf", "http://vendor/lockzhiner/rk3568/preinstall-config:preinstall-config", "http://vendor/lockzhiner/rk3568/resourceschedule:resourceschedule", "http://vendor/lockzhiner/rk3568/etc:product_etc_conf", "http://vendor/lockzhiner/rk3568/samples:samples" ] } }, "subsystem": "product_lockzhiner"}
注意:"http://vendor/lockzhiner/rk3568/samples:samples"表示將vendor/lockzhiner/rk3568/samples目錄添加到編譯中。
在//vendor/lockzhiner/rk3568/samples/BUILD.gn文件添加一行編譯引導語句。
import("http://build/ohos.gni")
group("samples") { deps = [ "a21_utils_helloworld:utilshelloworld", ]}
"http://samples/a21_utils_helloworld:utilshelloworld",該行語句表示引入//a21_utils_helloworld 參與編譯。
2.2、創(chuàng)建編譯項目
創(chuàng)建//samples/a21_utils_helloworld 目錄,并添加如下文件:
a21_utils_helloworld├── utils_helloworld_sample.cpp # .cpp源代碼├── BUILD.gn # GN文件
2.3、創(chuàng)建BUILD.gn
編輯BUILD.gn文件。
import("http://build/ohos.gni")ohos_executable("utils_helloworld") { sources = [ "src/utils_helloworld_sample.cpp" ] # 參與編譯的源代碼文件 include_dirs = [ "http://commonlibrary/c_utils/base:utils", "http://third_party/googletest:gtest_main" ] part_name = "product_rk3568" # 模塊名稱 install_enable = true # 安裝到系統(tǒng)中}
注意:
(1)BUILD.gn中所有的TAB鍵必須轉(zhuǎn)化為空格,否則會報錯。如果自己不知道如何規(guī)范化,可以:
# 安裝gn工具sudo apt-get install ninja-buildsudo apt install generate-ninjas# 規(guī)范化BUILD.gngn format BUILD.gn
(2)可執(zhí)行程序的名稱
ohos_executable("utilshelloworld")中的utilshelloworld為可執(zhí)行程序的名稱,必須與//samples/BUILD.gn文件的內(nèi)容一致。
2.4、創(chuàng)建源代碼
utils_helloworld_sample.cpp具體代碼如下:
#include
using namespace std;
int main(int argc, char *argv[]){ cout << "Hello, World!" << endl; return 0;}
3、運行程序
系統(tǒng)啟動后,運行命令:
utils_helloworld
4、運行結(jié)果
運行結(jié)果:
# utilshelloworldHello, World!#
-
程序
+關(guān)注
關(guān)注
117文章
3847瀏覽量
85393 -
開發(fā)板
+關(guān)注
關(guān)注
26文章
6362瀏覽量
119901 -
OpenHarmony
+關(guān)注
關(guān)注
33文章
3965瀏覽量
21291
發(fā)布評論請先 登錄
keil實現(xiàn)c與c++混合編程
C語言與C++的區(qū)別及聯(lián)系
C與C++之間的聯(lián)系
C語言和C++之間的區(qū)別是什么
C++程序異常的處理機制
OpenHarmony TSC 2025年度技術(shù)課題發(fā)布(截至10月)
技能+1!如何在樹莓派上使用C++控制GPIO?
C++ 與 Python:樹莓派上哪種語言更優(yōu)?
OpenHarmony 2025年度技術(shù)課題發(fā)布
基于LockAI視覺識別模塊:C++目標檢測
貢獻 OpenHarmony 庫關(guān)鍵配置
主流的 MCU 開發(fā)語言為什么是 C 而不是 C++?
OpenHarmony C++公共基礎(chǔ)類庫應用案例:HelloWorld
評論