Files
luban-lite/doc/topics/sdk/app/dm_app_run_compile.html

412 lines
46 KiB
HTML
Raw Normal View History

2025-04-23 17:54:31 +08:00
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn" lang="zh-cn" data-whc_version="26.1">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="description" content="DM-APP 可以被编译成两种类型:可执行文件 ( .mo ) 和库文件 ( .so )。本质上两者都是 ET_DYN 类型的 ELF 文件,唯一的不同是可执行文件指定了 main 函数作为执行入口,而库文件指定了 0 作为执行入口。 编译和运行可执行文件 .mo 执行本节步骤前,确认打开正确的命令行运行环境: 在 Windows 系统上:双击 win_env.bat 打开命令行运行环境。 在 ..."/><meta name="DC.rights.owner" content="(C) 版权 2025"/><meta name="copyright" content="(C) 版权 2025"/><meta name="generator" content="DITA-OT"/><meta name="DC.type" content="concept"/><meta name="DC.relation" content="../../../topics/sdk/app/dm.html"/><meta name="DC.relation" content="../../../topics/sdk/app/dm_app_dev.html"/><meta name="DC.relation" content="../../../topics/sdk/app/dm_app_gdb_debug.html"/><meta name="DC.contributor" content="yan.wang"/><meta name="DC.contributor" content="yan.wang"/><meta name="DC.creator" content="yan.wang"/><meta name="DC.date.modified" content="2025-04-09"/><meta name="DC.format" content="HTML5"/><meta name="DC.identifier" content="dm_app_run_compile"/><title>编译和运行</title><meta name="wh-path2root" content="../../../"/><meta name="wh-toc-id" content="dm_app_run_compile-d3703e1411"/><meta name="wh-source-relpath" content="topics/sdk/app/dm_app_run_compile.dita"/><meta name="wh-out-relpath" content="topics/sdk/app/dm_app_run_compile.html"/>
2025-01-23 16:37:00 +08:00
2025-04-23 17:54:31 +08:00
<link rel="stylesheet" type="text/css" href="../../../webhelp/app/commons.css?buildId=2024041900"/>
<link rel="stylesheet" type="text/css" href="../../../webhelp/app/topic.css?buildId=2024041900"/>
2025-01-23 16:37:00 +08:00
2025-04-23 17:54:31 +08:00
<script src="../../../webhelp/app/options/properties.js?buildId=20250409170918"></script>
<script src="../../../webhelp/app/localization/strings.js?buildId=2024041900"></script>
<script src="../../../webhelp/app/search/index/keywords.js?buildId=20250409170918"></script>
<script defer="defer" src="../../../webhelp/app/commons.js?buildId=2024041900"></script>
<script defer="defer" src="../../../webhelp/app/topic.js?buildId=2024041900"></script>
<link rel="stylesheet" type="text/css" href="../../../webhelp/template/css/aic-styles-web-internal.css?buildId=2024041900"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/css/notes.css?buildId=2024041900"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/css/aic-common.css?buildId=2024041900"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/css/aic-images.css?buildId=2024041900"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/css/footnote.css?buildId=2024041900"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/css/aic-highlight-changes.css?buildId=2024041900"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/css/search-in-header.css?buildId=2024041900"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/css/topic-body-list.css?buildId=2024041900"/></head>
2025-01-23 16:37:00 +08:00
<body id="dm_app_run_compile" class="wh_topic_page frmBody">
<a href="#wh_topic_body" class="sr-only sr-only-focusable">
跳转到主要内容
</a>
<header class="navbar navbar-default wh_header">
<div class="container-fluid">
2025-04-23 17:54:31 +08:00
<div xmlns:whc="http://www.oxygenxml.com/webhelp/components" class="wh_header_flex_container navbar-nav navbar-expand-md navbar-dark">
2025-01-23 16:37:00 +08:00
<div class="wh_logo_and_publication_title_container">
<div class="wh_logo_and_publication_title">
2025-04-23 17:54:31 +08:00
<a href="https://www.artinchip.com" class=" wh_logo d-none d-sm-block "><img src="../../../company-logo.png" alt=" RTOS SDK 使用指南 SDK 指南文件 "/></a>
<div class=" wh_publication_title "><a href="../../../index.html"><span class="booktitle"> <span class="ph mainbooktitle">RTOS SDK 使用指南</span> <span class="ph booktitlealt">SDK 指南文件</span> </span></a></div>
2025-01-23 16:37:00 +08:00
</div>
</div>
<div class="wh_top_menu_and_indexterms_link collapse navbar-collapse" id="wh_top_menu_and_indexterms_link">
</div>
<div class=" wh_search_input navbar-form wh_topic_page_search search " role="form">
<form id="searchForm" method="get" role="search" action="../../../search.html"><div><input type="search" placeholder="搜索 " class="wh_search_textfield" id="textToSearch" name="searchQuery" aria-label="搜索查询" required="required"/><button type="submit" class="wh_search_button" aria-label="搜索"><span class="search_input_text">搜索</span></button></div></form>
2025-04-23 17:54:31 +08:00
</div></div>
</div>
</header>
2025-01-23 16:37:00 +08:00
<div class="container-fluid" id="wh_topic_container">
<div class="row">
<nav class="wh_tools d-print-none navbar-expand-md" aria-label="Tools">
<div data-tooltip-position="bottom" class=" wh_breadcrumb "><ol class="d-print-none"><li><span class="home"><a href="../../../index.html"><span>主页</span></a></span></li><li><div class="topicref" data-id="concept_rcx_czh_pzb"><div class="title"><a href="../../../topics/sdk/chapter-app.html">应用场景</a><div class="wh-tooltip"><p class="shortdesc">描述了 SDK 在不同应用场景中的配置和使用包括系统更新、OTA、安全方案等。</p></div></div></div></li><li><div class="topicref" data-id="id"><div class="title"><a href="../../../topics/sdk/app/dm.html">动态加载 (DM-APP)</a></div></div></li><li class="active"><div class="topicref" data-id="dm_app_run_compile"><div class="title"><a href="../../../topics/sdk/app/dm_app_run_compile.html">编译和运行</a></div></div></li></ol></div>
<div class="wh_right_tools">
<button class="wh_hide_highlight" aria-label="切换搜索突出显示" title="切换搜索突出显示"></button>
<button class="webhelp_expand_collapse_sections" data-next-state="collapsed" aria-label="折叠截面" title="折叠截面"></button>
<div class=" wh_navigation_links "><span id="topic_navigation_links" class="navheader">
<span class="navprev"><a class="- topic/link link" href="../../../topics/sdk/app/dm_app_dev.html" title="DM-APP 开发" aria-label="上一主题: DM-APP 开发" rel="prev"></a></span>
<span class="navnext"><a class="- topic/link link" href="../../../topics/sdk/app/dm_app_gdb_debug.html" title="GDB 调试" aria-label="下一主题: GDB 调试" rel="next"></a></span> </span></div>
<div class=" wh_print_link print d-none d-md-inline-block "><button onClick="window.print()" title="打印此页" aria-label="打印此页"></button></div>
<button type="button" id="wh_toc_button" class="custom-toggler navbar-toggler collapsed wh_toggle_button navbar-light" aria-expanded="false" aria-label="Toggle publishing table of content" aria-controls="wh_publication_toc">
<span class="navbar-toggler-icon"></span>
</button>
</div>
</nav>
</div>
<div class="wh_content_area">
<div class="row">
<nav id="wh_publication_toc" class="col-lg-3 col-md-3 col-sm-12 d-md-block d-none d-print-none" aria-label="Table of Contents Container">
<div id="wh_publication_toc_content">
2025-04-23 17:54:31 +08:00
<div class=" wh_publication_toc " data-tooltip-position="right"><span class="expand-button-action-labels"><span id="button-expand-action" role="button" aria-label="Expand"></span><span id="button-collapse-action" role="button" aria-label="Collapse"></span><span id="button-pending-action" role="button" aria-label="Pending"></span></span><ul role="tree" aria-label="Table of Contents"><li role="treeitem"><div data-tocid="revinfo_linux-d3703e457" class="topicref" data-id="revinfo_linux" data-state="leaf"><span role="button" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/revinfo/revinfo_rtos.html" id="revinfo_linux-d3703e457-link">修订记录</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="id-d3703e479" class="topicref" data-id="id" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action id-d3703e479-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/env/sdk-compile.html" id="id-d3703e479-link">SDK 编译</a><div class="wh-tooltip"><p class="shortdesc">介绍不同编译环境下 SDK 的详细编译流程。</p></div></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="id-d3703e601" class="topicref" data-id="id" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action id-d3703e601-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/advanced/sdk-usage.html" id="id-d3703e601-link">使用指南</a><div class="wh-tooltip"><p class="shortdesc">命令详解,编译选项,镜像和分区配置,添加应用和驱动等的详细使用说明。</p></div></div></div></li><li role="treeitem" aria-expanded="true"><div data-tocid="concept_rcx_czh_pzb-d3703e1001" class="topicref" data-id="concept_rcx_czh_pzb" data-state="expanded"><span role="button" tabindex="0" aria-labelledby="button-collapse-action concept_rcx_czh_pzb-d3703e1001-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/chapter-app.html" id="concept_rcx_czh_pzb-d3703e1001-link">应用场景</a><div class="wh-tooltip"><p class="shortdesc">描述了 SDK 在不同应用场景中的配置和使用包括系统更新、OTA、安全方案等。</p></div></div></div><ul role="group" class="navbar-nav nav-list"><li role="treeitem"><div data-tocid="id-d3703e1018" class="topicref" data-id="id" data-state="leaf"><span role="button" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/usb/udisk.html" id="id-d3703e1018-link">挂载 U 盘 </a></div></div></li><li role="treeitem"><div data-tocid="id-d3703e1032" class="topicref" data-id="id" data-state="leaf"><span role="button" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/sdmc/sdcard.html" id="id-d3703e1032-link">挂载 SD 卡</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="id-d3703e1046" class="topicref" data-id="id" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action id-d3703e1046-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/burnsys/burnsys_user_guide.html" id="id-d3703e1046-link">系统更新</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="id-d3703e1160" class="topicref" data-id="id" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action id-d3703e1160-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/ota/ota_guide.html" id="id-d3703e1160-link">OTA 方案</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="id-d3703e1280" class="topicref" data-id="id" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action id-d3703e1280-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/xip/xip_user_guide.html" id="id-d3703e1280-link">XIP 方案 </a></div></div></li><li role="treeitem" aria-expanded="true"><div data
2025-01-23 16:37:00 +08:00
</div>
</nav>
<div class="col-lg-7 col-md-9 col-sm-12" id="wh_topic_body">
<button id="wh_close_publication_toc_button" class="close-toc-button d-none" aria-label="Toggle publishing table of content" aria-controls="wh_publication_toc" aria-expanded="true">
<span class="close-toc-icon-container">
<span class="close-toc-icon"></span>
</span>
</button>
<button id="wh_close_topic_toc_button" class="close-toc-button d-none" aria-label="Toggle topic table of content" aria-controls="wh_topic_toc" aria-expanded="true">
<span class="close-toc-icon-container">
<span class="close-toc-icon"></span>
</span>
</button>
2025-04-23 17:54:31 +08:00
<div class=" wh_topic_content body "><main role="main"><article class="- topic/topic concept/concept topic concept" role="article" aria-labelledby="ariaid-title1">
<span class="edit-link" style="font-size:12px; opacity:0.6; text-align:right; vertical-align:middle"><a target="_blank" href="http://aicdocco/taskstopics/sdk/app/dm_app_run_compile.dita">Edit online</a></span><h1 class="- topic/title title topictitle1" id="ariaid-title1">编译和运行</h1>
<div class="date inPage">9 Apr 2025</div><div style="color: gray;">
2025-01-23 16:37:00 +08:00
Read time: 4 minute(s)
2025-04-23 17:54:31 +08:00
</div>
<div class="- topic/body concept/conbody body conbody">
<p class="- topic/p p" data-ofbid="d159493e36__20250409171049">DM-APP 可以被编译成两种类型:可执行文件 (<span class="+ topic/ph sw-d/filepath ph filepath">.mo</span>) 和库文件
2025-01-23 16:37:00 +08:00
(<span class="+ topic/ph sw-d/filepath ph filepath">.so</span>)。本质上两者都是 <span class="+ topic/ph sw-d/filepath ph filepath">ET_DYN</span> 类型的 ELF 文件,唯一的不同是可执行文件指定了
2025-04-23 17:54:31 +08:00
<span class="+ topic/keyword pr-d/parmname keyword parmname">main</span> 函数作为执行入口,而库文件指定了 <span class="+ topic/keyword pr-d/parmname keyword parmname">0</span> 作为执行入口。</p>
<section class="- topic/section section" id="dm_app_run_compile__section_xj1_mvm_tdc" data-ofbid="dm_app_run_compile__section_xj1_mvm_tdc"><h2 class="- topic/title title sectiontitle">编译和运行可执行文件<span class="+ topic/ph sw-d/filepath ph filepath">.mo</span></h2>
2025-01-23 16:37:00 +08:00
2025-04-23 17:54:31 +08:00
<div class="- topic/p p" data-ofbid="d159493e61__20250409171049">执行本节步骤前,确认打开正确的命令行运行环境:<ul class="- topic/ul ul" id="dm_app_run_compile__ul_zt5_4vm_tdc" data-ofbid="dm_app_run_compile__ul_zt5_4vm_tdc">
<li class="- topic/li li" data-ofbid="d159493e65__20250409171049">
<p class="- topic/p p" data-ofbid="d159493e67__20250409171049">在 Windows 系统上:双击 <span class="+ topic/ph sw-d/filepath ph filepath">win_env.bat</span> 打开命令行运行环境。</p>
</li>
<li class="- topic/li li" data-ofbid="d159493e74__20250409171049">
<p class="- topic/p p" data-ofbid="d159493e76__20250409171049">在 Linux 系统上:直接使用 Shell 命令行即可。</p>
</li>
</ul><ol class="- topic/ol ol" id="dm_app_run_compile__ul_nf4_2m5_fdc" data-ofbid="dm_app_run_compile__ul_nf4_2m5_fdc">
<li class="- topic/li li" data-ofbid="d159493e82__20250409171049">进入 dm-app
根目录:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_l5b_zl5_fdc" data-ofbid="dm_app_run_compile__codeblock_l5b_zl5_fdc">cd luban-lite/packages/artinchip/aic-dm-apps</pre></li>
<li class="- topic/li li" data-ofbid="d159493e87__20250409171049">执行下列命令编译 dm-app
为可执行文件:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_yhy_b1n_tdc" data-ofbid="dm_app_run_compile__codeblock_yhy_b1n_tdc">scons --app=hello</pre></li>
<li class="- topic/li li" data-ofbid="d159493e92__20250409171049">查看目标文件:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_dn2_c1n_tdc" data-ofbid="dm_app_run_compile__codeblock_dn2_c1n_tdc">ls hello/hello.mo</pre></li>
<li class="- topic/li li" data-ofbid="d159493e97__20250409171049">如有需要,执行下列命令可清理目标文件,否则可跳过此步:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_sgm_c1n_tdc" data-ofbid="dm_app_run_compile__codeblock_sgm_c1n_tdc">scons --app=hello -c</pre></li>
<li class="- topic/li li" data-ofbid="d159493e102__20250409171049">将生成的 <span class="+ topic/ph sw-d/filepath ph filepath">hello.mo</span> 文件拷贝到单板存储介质的文件系统中:</li>
<li class="- topic/li li" data-ofbid="d159493e109__20250409171049">在 Shell
下直接运行:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_m5b_zl5_fdc" data-ofbid="dm_app_run_compile__codeblock_m5b_zl5_fdc">/sdcard/hello.mo</pre><div class="- topic/p p" data-ofbid="d159493e113__20250409171049">系统输出示例如下:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_ivy_z1n_tdc" data-ofbid="dm_app_run_compile__codeblock_ivy_z1n_tdc">[AIC-DM-APP] init! <em class="hl-comment">// DM 初始化函数 module_init()</em>
2025-01-23 16:37:00 +08:00
[AIC-DM-APP] Hello, world! <em class="hl-comment">// DM 主函数 main()</em>
index =&gt; <span class="hl-number">0</span> <em class="hl-comment">// my_thread_init() 调用 rt-thread API 创建的线程</em>
index =&gt; <span class="hl-number">1</span>
index =&gt; <span class="hl-number">2</span>
2025-04-23 17:54:31 +08:00
index =&gt; <span class="hl-number">3</span></pre></div></li>
</ol></div>
</section>
<section class="- topic/section section" id="dm_app_run_compile__section_lbt_kvm_tdc" data-ofbid="dm_app_run_compile__section_lbt_kvm_tdc"><h2 class="- topic/title title sectiontitle">编译和运行库文件 <span class="+ topic/ph sw-d/filepath ph filepath">.so</span></h2>
2025-01-23 16:37:00 +08:00
2025-04-23 17:54:31 +08:00
<div class="- topic/p p" data-ofbid="d159493e127__20250409171049">
<ol class="- topic/ol ol" id="dm_app_run_compile__ol_b3c_hbn_tdc" data-ofbid="dm_app_run_compile__ol_b3c_hbn_tdc">
<li class="- topic/li li" data-ofbid="d159493e131__20250409171049">进入 dm-app
根目录:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_c4x_gbn_tdc" data-ofbid="dm_app_run_compile__codeblock_c4x_gbn_tdc">cd luban-lite/packages/artinchip/aic-dm-apps</pre></li>
<li class="- topic/li li" data-ofbid="d159493e136__20250409171049">执行下列命令编译 dm-app
为可执行文件:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_d4x_gbn_tdc" data-ofbid="dm_app_run_compile__codeblock_d4x_gbn_tdc">scons --app=hello</pre></li>
<li class="- topic/li li" data-ofbid="d159493e141__20250409171049">查看目标文件:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_e4x_gbn_tdc" data-ofbid="dm_app_run_compile__codeblock_e4x_gbn_tdc">ls hello/hello.so</pre></li>
<li class="- topic/li li" data-ofbid="d159493e146__20250409171049">如有需要,执行下列命令可清理目标文件,否则可跳过此步:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_f4x_gbn_tdc" data-ofbid="dm_app_run_compile__codeblock_f4x_gbn_tdc">scons --app=hello -c</pre></li>
<li class="- topic/li li" data-ofbid="d159493e151__20250409171049">在 Kernel 中使能 <span class="+ topic/ph ui-d/uicontrol ph uicontrol">test_dm_lib</span>
2025-01-23 16:37:00 +08:00
测试命令:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_xmn_d45_fdc" data-ofbid="dm_app_run_compile__codeblock_xmn_d45_fdc">Drivers options ---&gt;
Drivers examples ---&gt;
2025-04-23 17:54:31 +08:00
[*] Enable DM Lib test command</pre></li>
<li class="- topic/li li" data-ofbid="d159493e160__20250409171049">
<p class="- topic/p p" data-ofbid="d159493e162__20250409171049">将生成的 <span class="+ topic/ph sw-d/filepath ph filepath">hello.so</span> 拷贝到单板存储介质的文件系统中。</p>
</li>
<li class="- topic/li li" data-ofbid="d159493e169__20250409171049">
<p class="- topic/p p" data-ofbid="d159493e171__20250409171049">使用 <span class="+ topic/keyword sw-d/cmdname keyword cmdname">test_dm_lib</span> 命令来动态加载:</p>
2025-01-23 16:37:00 +08:00
<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_o5b_zl5_fdc" data-ofbid="dm_app_run_compile__codeblock_o5b_zl5_fdc">test_dm_lib <em class="hl-comment">// dlopen() 动态加载 /sdcard/hello.so</em></pre>
<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_wtr_2pm_tdc" data-ofbid="dm_app_run_compile__codeblock_wtr_2pm_tdc">[AIC-DM-APP] init! <em class="hl-comment">// DM 初始化函数 module_init()</em>
index =&gt; <span class="hl-number">0</span> <em class="hl-comment">// my_thread_init() 调用 rt-thread API 创建的线程</em>
index =&gt; <span class="hl-number">1</span>
index =&gt; <span class="hl-number">2</span></pre>
2025-04-23 17:54:31 +08:00
</li>
</ol>
2025-01-23 16:37:00 +08:00
</div>
2025-04-23 17:54:31 +08:00
</section>
<section class="- topic/section section" id="dm_app_run_compile__section_s2t_m5m_tdc" data-ofbid="dm_app_run_compile__section_s2t_m5m_tdc"><h2 class="- topic/title title sectiontitle">实例代码注意事项</h2>
2025-01-23 16:37:00 +08:00
2025-04-23 17:54:31 +08:00
<p class="- topic/p p" data-ofbid="d159493e192__20250409171049">本节根据实例代码分析,详细书面了使用可执行文件 <span class="+ topic/ph sw-d/filepath ph filepath">hello.mo</span> 和库文件
2025-01-23 16:37:00 +08:00
<span class="+ topic/ph sw-d/filepath ph filepath">hello.so</span> 的源码时需要注意的特殊事项:</p>
2025-04-23 17:54:31 +08:00
<ul class="- topic/ul ul" id="dm_app_run_compile__ul_bzg_3fb_pdc" data-ofbid="dm_app_run_compile__ul_bzg_3fb_pdc">
<li class="- topic/li li" data-ofbid="d159493e203__20250409171049">
2025-01-23 16:37:00 +08:00
<div class="- topic/div div section" id="dm_app_run_compile__id11"><strong class="+ topic/ph hi-d/b ph b">可执行文件 <span class="+ topic/ph sw-d/filepath ph filepath">.mo</span> 注意事项</strong>
2025-04-23 17:54:31 +08:00
<ol class="- topic/ol ol" id="dm_app_run_compile__ol_img_bn5_fdc" data-ofbid="dm_app_run_compile__ol_img_bn5_fdc">
<li class="- topic/li li" data-ofbid="d159493e214__20250409171049">
2025-01-23 16:37:00 +08:00
<div class="- topic/div div section" id="dm_app_run_compile__rt-thread-api"><strong class="+ topic/ph hi-d/b ph b">RT-Thread API
的调用</strong>
2025-04-23 17:54:31 +08:00
<div class="- topic/p p" data-ofbid="d159493e220__20250409171049"><span class="+ topic/ph sw-d/filepath ph filepath">hello.mo</span> 中,可以调用如
2025-01-23 16:37:00 +08:00
<span class="+ topic/keyword pr-d/apiname keyword apiname">rt_thread_create</span>
<span class="+ topic/keyword pr-d/apiname keyword apiname">rt_thread_startup</span> 等 RT-Thread API。这些
API 默认已经使用 <span class="+ topic/keyword pr-d/apiname keyword apiname">RTM_EXPORT()</span> 声明,可以直接调用
<span class="+ topic/ph sw-d/filepath ph filepath">luban-lite/packages/artinchip/aic-dm-apps/hello/rtt_api_test.c</span>。例如:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_x1w_245_fdc" data-ofbid="dm_app_run_compile__codeblock_x1w_245_fdc">#include &lt;rtthread.h&gt;
<strong class="hl-keyword">void</strong> my_thread_entry(<strong class="hl-keyword">void</strong>* parameter)
{
<strong class="hl-keyword">int</strong> index = <span class="hl-number">0</span>;
<strong class="hl-keyword">while</strong> (<span class="hl-number">1</span>)
{
rt_kprintf(<span class="hl-string">"index =&gt; %d\n"</span>, index ++);
rt_thread_delay(RT_TICK_PER_SECOND);
}
}
<strong class="hl-keyword">int</strong> my_thread_init(<strong class="hl-keyword">void</strong>)
{
rt_thread_t tid;
tid = rt_thread_create(<span class="hl-string">"tMyTask"</span>, my_thread_entry, RT_NULL,
<span class="hl-number">2048</span>, <span class="hl-number">20</span>, <span class="hl-number">20</span>);
<strong class="hl-keyword">if</strong> (tid != RT_NULL)
rt_thread_startup(tid);
<strong class="hl-keyword">return</strong> <span class="hl-number">0</span>;
}</pre></div></div>
2025-04-23 17:54:31 +08:00
</li>
<li class="- topic/li li" data-ofbid="d159493e242__20250409171049">
2025-01-23 16:37:00 +08:00
<div class="- topic/div div section" id="dm_app_run_compile__id12"><strong class="+ topic/ph hi-d/b ph b">模块初始化和退出函数</strong>
2025-04-23 17:54:31 +08:00
<div class="- topic/p p" data-ofbid="d159493e248__20250409171049">如果 dm-app 定义了 <span class="+ topic/keyword pr-d/apiname keyword apiname">module_init()</span>
2025-01-23 16:37:00 +08:00
<span class="+ topic/keyword pr-d/apiname keyword apiname">module_cleanup()</span>
函数,它们会在模块初始化和退出时被自动调用
<span class="+ topic/ph sw-d/filepath ph filepath">luban-lite/packages/artinchip/aic-dm-apps/hello/main.c</span>。例如:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_zjc_g45_fdc" data-ofbid="dm_app_run_compile__codeblock_zjc_g45_fdc"><strong class="hl-keyword">void</strong> module_init(<strong class="hl-keyword">struct</strong> rt_dlmodule *module)
{
printf(<span class="hl-string">"[AIC-DM-APP] init!\n"</span>);
}
<strong class="hl-keyword">void</strong> module_cleanup(<strong class="hl-keyword">struct</strong> rt_dlmodule *module)
{
printf(<span class="hl-string">"[AIC-DM-APP] exit!\n"</span>);
}</pre>
2025-04-23 17:54:31 +08:00
</div><p class="- topic/p p" data-ofbid="d159493e262__20250409171049">用户可以利用该机制来做一些初始化和清理的工作。如果不需要,则无需实现这两个函数。</p></div>
</li>
<li class="- topic/li li" data-ofbid="d159493e266__20250409171049">
2025-01-23 16:37:00 +08:00
<div class="- topic/div div section" id="dm_app_run_compile__hello-mo">
<strong class="+ topic/ph hi-d/b ph b">查看 <span class="+ topic/ph sw-d/filepath ph filepath">hello.mo</span> 创建的子线程</strong>
2025-04-23 17:54:31 +08:00
<p class="- topic/p p" data-ofbid="d159493e276__20250409171049">从代码可知,运行 <span class="+ topic/ph sw-d/filepath ph filepath">hello.mo</span> 后会创建
2025-01-23 16:37:00 +08:00
<code class="+ topic/ph pr-d/codeph ph codeph">tMyTask</code> 线程,但使用 <span class="+ topic/keyword sw-d/cmdname keyword cmdname">ps</span> 或者
<span class="+ topic/keyword sw-d/cmdname keyword cmdname">list_thread</span>
命令无法看到该线程。这是因为该方式下启动的线程会被链接到模块本身的进程链表
<code class="+ topic/ph pr-d/codeph ph codeph">module-&gt;object_list</code> ,而上述命令只能查看全局链表
<code class="+ topic/ph pr-d/codeph ph codeph">information-&gt;object_list</code>
2025-04-23 17:54:31 +08:00
中的线程。</p><p class="- topic/p p" data-ofbid="d159493e297__20250409171049">目前模块本身的进程链表
2025-01-23 16:37:00 +08:00
<code class="+ topic/ph pr-d/codeph ph codeph">module-&gt;object_list</code>
不支持命令查看,在模块退出时会停止掉 <code class="+ topic/ph pr-d/codeph ph codeph">module-&gt;object_list</code>
中模块启动的所有子进程。</p>
</div>
2025-04-23 17:54:31 +08:00
</li>
<li class="- topic/li li" data-ofbid="d159493e308__20250409171049">
2025-01-23 16:37:00 +08:00
<div class="- topic/div div section" id="dm_app_run_compile__id13">
<strong class="+ topic/ph hi-d/b ph b">后台进程保活</strong>
2025-04-23 17:54:31 +08:00
<div class="- topic/p p" data-ofbid="d159493e315__20250409171049"><span class="+ topic/ph sw-d/filepath ph filepath">hello.mo</span><span class="+ topic/keyword pr-d/apiname keyword apiname">main()</span>
2025-01-23 16:37:00 +08:00
函数返回后,系统马上会执行模块退出动作,<span class="+ topic/keyword pr-d/apiname keyword apiname">main()</span>
函数创建的所有子进程也会被全部清理。为了让模块的子进程作为后台进程继续运行,可以为
<span class="+ topic/ph sw-d/filepath ph filepath">hello.mo</span>
<span class="+ topic/keyword pr-d/apiname keyword apiname">main()</span> 函数定义一个特殊返回值
<span class="+ topic/keyword pr-d/parmname keyword parmname">RT_DLMODULE_DEAMON</span>。如果返回该值,则
<span class="+ topic/keyword pr-d/apiname keyword apiname">main()</span> 函数返回后系统不会执行模块退出动作。例如
<span class="+ topic/ph sw-d/filepath ph filepath">luban-lite/packages/artinchip/aic-dm-apps/hello/main.c</span><pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_d23_h45_fdc" data-ofbid="dm_app_run_compile__codeblock_d23_h45_fdc">#<span class="hl-directive">define</span> RT_API_TEST
<strong class="hl-keyword">int</strong> main(<strong class="hl-keyword">int</strong> argc, <strong class="hl-keyword">char</strong> *argv[])
{
printf(<span class="hl-string">"[AIC-DM-APP] Hello, world!\n"</span>);
#ifdef RT_API_TEST
my_thread_init();
<strong class="hl-keyword">return</strong> RT_DLMODULE_DEAMON;
#endif
<strong class="hl-keyword">return</strong> <span class="hl-number">0</span>;
}</pre></div></div>
2025-04-23 17:54:31 +08:00
</li>
</ol></div>
</li>
<li class="- topic/li li" data-ofbid="d159493e348__20250409171049">
2025-01-23 16:37:00 +08:00
<div class="- topic/div div section" id="dm_app_run_compile__id14">
<strong class="+ topic/ph hi-d/b ph b">库文件 <span class="+ topic/ph sw-d/filepath ph filepath">.so</span></strong>
2025-04-23 17:54:31 +08:00
<ol class="- topic/ol ol" id="dm_app_run_compile__ol_mjz_bn5_fdc" data-ofbid="dm_app_run_compile__ol_mjz_bn5_fdc">
<li class="- topic/li li" data-ofbid="d159493e359__20250409171049">
2025-01-23 16:37:00 +08:00
<div class="- topic/div div section" id="dm_app_run_compile__dlopen-dlsym">
<strong class="+ topic/ph hi-d/b ph b"><span class="+ topic/keyword pr-d/apiname keyword apiname">dlopen()</span><span class="+ topic/keyword pr-d/apiname keyword apiname">dlsym()</span> 实例</strong>
2025-04-23 17:54:31 +08:00
<div class="- topic/p p" data-ofbid="d159493e371__20250409171049"><span class="+ topic/keyword sw-d/cmdname keyword cmdname">test_dm_lib</span> 命令的基本原理是使用
2025-01-23 16:37:00 +08:00
<span class="+ topic/keyword pr-d/apiname keyword apiname">dlopen()</span> 函数动态加载
<span class="+ topic/ph sw-d/filepath ph filepath">hello.so</span> 到系统内存,再使用
<span class="+ topic/keyword pr-d/apiname keyword apiname">dlsym()</span> 函数查找到
<span class="+ topic/ph sw-d/filepath ph filepath">hello.so</span> 中的
<span class="+ topic/keyword pr-d/apiname keyword apiname">my_thread_init()</span> 函数并调用
<span class="+ topic/ph sw-d/filepath ph filepath">luban-lite/bsp/examples/test-dm-lib/test_dm_lib.c</span>。例如:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="dm_app_run_compile__codeblock_dtn_j45_fdc" data-ofbid="dm_app_run_compile__codeblock_dtn_j45_fdc">#<span class="hl-directive">define</span> DM_LIB_PATH <span class="hl-string">"/sdcard/hello.so"</span>
#<span class="hl-directive">define</span> DM_LIB_FUNC <span class="hl-string">"my_thread_init"</span>
#<span class="hl-directive">define</span> DEAMON_THREAD
<strong class="hl-keyword">static</strong> <strong class="hl-keyword">void</strong> cmd_test_dm_lib(<strong class="hl-keyword">int</strong> argc, <strong class="hl-keyword">char</strong> **argv)
{
<strong class="hl-keyword">struct</strong> rt_dlmodule *module = NULL;
<strong class="hl-keyword">int</strong> (*func)(<strong class="hl-keyword">void</strong>) = NULL;
module = dlopen(DM_LIB_PATH, <span class="hl-number">0</span>);
<strong class="hl-keyword">if</strong> (!module) {
printf(<span class="hl-string">"dlopen %s fail!\n"</span>, DM_LIB_PATH);
<strong class="hl-keyword">return</strong>;
}
func = dlsym(module, DM_LIB_FUNC);
<strong class="hl-keyword">if</strong> (!func) {
printf(<span class="hl-string">"dlsym %s fail!\n"</span>, DM_LIB_FUNC);
<strong class="hl-keyword">return</strong>;
}
func();
#ifndef DEAMON_THREAD
dlclose(module);
#endif
}</pre></div></div>
2025-04-23 17:54:31 +08:00
</li>
<li class="- topic/li li" data-ofbid="d159493e398__20250409171049">
2025-01-23 16:37:00 +08:00
<div class="- topic/div div section" id="dm_app_run_compile__hello-so">
<strong class="+ topic/ph hi-d/b ph b">查看 <span class="+ topic/ph sw-d/filepath ph filepath">hello.so</span> 创建的子线程</strong>
2025-04-23 17:54:31 +08:00
<p class="- topic/p p" data-ofbid="d159493e408__20250409171049">通过 <span class="+ topic/keyword sw-d/cmdname keyword cmdname">test_dm_lib</span> 命令动态加载
2025-01-23 16:37:00 +08:00
<span class="+ topic/ph sw-d/filepath ph filepath">hello.so</span> 并调用
<span class="+ topic/keyword pr-d/apiname keyword apiname">my_thread_init()</span> 函数,同样会会创建
<code class="+ topic/ph pr-d/codeph ph codeph">tMyTask</code> 线程。上述示例中,使用
<span class="+ topic/keyword sw-d/cmdname keyword cmdname">ps</span> 或者 <span class="+ topic/keyword sw-d/cmdname keyword cmdname">list_thread</span>
命令可以看到该线程,是因为系统通过 <span class="+ topic/keyword pr-d/apiname keyword apiname">dlmodule_self()</span>
判断当前进程非模块执行进程,对应的进程链表就加入到了全局链表
<code class="+ topic/ph pr-d/codeph ph codeph">information-&gt;object_list</code>
中。</p></div>
2025-04-23 17:54:31 +08:00
</li>
<li class="- topic/li li" data-ofbid="d159493e437__20250409171049">
2025-01-23 16:37:00 +08:00
<div class="- topic/div div section" id="dm_app_run_compile__id15"><strong class="+ topic/ph hi-d/b ph b">后台进程保活</strong>
2025-04-23 17:54:31 +08:00
<p class="- topic/p p" data-ofbid="d159493e443__20250409171049">当调用 <span class="+ topic/keyword pr-d/apiname keyword apiname">my_thread_init()</span> 函数返回后,常规情况是执行
2025-01-23 16:37:00 +08:00
<span class="+ topic/keyword pr-d/apiname keyword apiname">dlclose(module)</span> 来清理动态加载的模块。如果
<span class="+ topic/keyword pr-d/apiname keyword apiname">my_thread_init()</span>
函数创建的所有子进程希望作为后台进程运行,则不能调用
<span class="+ topic/keyword pr-d/apiname keyword apiname">dlclose(module)</span> 来卸载动态库。因为一旦调用
<span class="+ topic/keyword pr-d/apiname keyword apiname">dlclose(module)</span>,动态库就会被卸载,而此时后台进程可能仍在运行并需要访问动态库中的数据。一旦有新的内存分配就会覆盖原动态模块的数据,触发
2025-04-23 17:54:31 +08:00
CPU 异常。 </p><p class="- topic/p p" data-ofbid="d159493e460__20250409171049">为了避免这种情况,可以在不调用
2025-01-23 16:37:00 +08:00
<span class="+ topic/keyword pr-d/apiname keyword apiname">dlclose(module)</span>
的情况下让程序持续运行,或者在程序结束时手动清理动态库占用的资源。</p></div>
2025-04-23 17:54:31 +08:00
</li>
</ol>
2025-01-23 16:37:00 +08:00
</div>
2025-04-23 17:54:31 +08:00
</li>
</ul>
</section>
</div>
</article></main></div>
2025-01-23 16:37:00 +08:00
</div>
<nav role="navigation" id="wh_topic_toc" aria-label="On this page" class="col-lg-2 d-none d-lg-block navbar d-print-none">
<div id="wh_topic_toc_content">
<div class=" wh_topic_toc "><div class="wh_topic_label">在本页上</div><ul><li class="section-item"><div class="section-title"><a href="#dm_app_run_compile__section_xj1_mvm_tdc" data-tocid="dm_app_run_compile__section_xj1_mvm_tdc">编译和运行可执行文件<span class="+ topic/ph sw-d/filepath ph filepath">.mo</span></a></div></li><li class="section-item"><div class="section-title"><a href="#dm_app_run_compile__section_lbt_kvm_tdc" data-tocid="dm_app_run_compile__section_lbt_kvm_tdc">编译和运行库文件 <span class="+ topic/ph sw-d/filepath ph filepath">.so</span></a></div></li><li class="section-item"><div class="section-title"><a href="#dm_app_run_compile__section_s2t_m5m_tdc" data-tocid="dm_app_run_compile__section_s2t_m5m_tdc">实例代码注意事项</a></div></li></ul></div>
</div>
</nav>
</div>
</div>
</div>
<footer class="navbar navbar-default wh_footer">
<div class=" footer-container mx-auto ">
<title>footer def</title>
2025-04-23 17:54:31 +08:00
<style>
2025-01-23 16:37:00 +08:00
.p1 {
font-family: FangZhengShuSong, Times, serif;
}
.p2 {
font-family: Arial, Helvetica, sans-serif;
}
.p3 {
font-family: "Lucida Console", "Courier New", monospace;
}
2025-04-23 17:54:31 +08:00
</style>
2025-01-23 16:37:00 +08:00
<div class="webhelp.fragment.footer">
2025-04-23 17:54:31 +08:00
<p class="p1">Copyright © 2019-2025 广东匠芯创科技有限公司. All rights reserved.</p>
</div>
<div>
2025-01-23 16:37:00 +08:00
<div class="generation_time">
2025-04-23 17:54:31 +08:00
Update Time: 2025-04-09
2025-01-23 16:37:00 +08:00
</div>
2025-04-23 17:54:31 +08:00
</div>
2025-01-23 16:37:00 +08:00
</div>
</footer>
<div id="go2top" class="d-print-none">
<span class="oxy-icon oxy-icon-up"></span>
</div>
<div id="modal_img_large" class="modal">
<span class="close oxy-icon oxy-icon-remove"></span>
<div id="modal_img_container"></div>
<div id="caption"></div>
</div>
<script src="${pd}/publishing/publishing-styles-AIC-template/js/custom.js" defer="defer"></script>
</body>
</html>