-
Notifications
You must be signed in to change notification settings - Fork 0
/
10d91204edcc.html
402 lines (284 loc) · 29.8 KB
/
10d91204edcc.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
<link rel="apple-touch-icon" sizes="180x180" href="/images/mylogo-128x128.png">
<link rel="icon" type="image/png" sizes="32x32" href="/images/mylogo-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/images/mylogo-16x16.png">
<link rel="mask-icon" href="/images/mylogo.svg" color="#222">
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
<script class="next-config" data-name="main" type="application/json">{"hostname":"renshengjihe.cn","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":"ture","bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果:${query}","hits_time":"找到 ${hits} 个搜索结果(用时 ${time} 毫秒)","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="在 Ubuntu 下搭建树莓派的交叉编译环境,在 PC 机上编译可以在树莓派上运行的代码。">
<meta property="og:type" content="article">
<meta property="og:title" content="树莓派交叉开发环境的搭建">
<meta property="og:url" content="https://renshengjihe.cn/10d91204edcc">
<meta property="og:site_name" content="人生几何">
<meta property="og:description" content="在 Ubuntu 下搭建树莓派的交叉编译环境,在 PC 机上编译可以在树莓派上运行的代码。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://renshengjihe-1257851699.cos.ap-guangzhou.myqcloud.com/191201-RaspberryPiTools/gccv.png">
<meta property="og:image" content="https://renshengjihe-1257851699.cos.ap-guangzhou.myqcloud.com/191201-RaspberryPiTools/run0nPC.png">
<meta property="og:image" content="https://renshengjihe-1257851699.cos.ap-guangzhou.myqcloud.com/191201-RaspberryPiTools/runOnPi.png">
<meta property="og:image" content="https://renshengjihe-1257851699.cos.ap-guangzhou.myqcloud.com/191201-RaspberryPiTools/make.png">
<meta property="article:published_time" content="2019-12-01T09:28:41.000Z">
<meta property="article:modified_time" content="2021-10-22T16:44:27.989Z">
<meta property="article:author" content="dhd">
<meta property="article:tag" content="树莓派">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://renshengjihe-1257851699.cos.ap-guangzhou.myqcloud.com/191201-RaspberryPiTools/gccv.png">
<link rel="canonical" href="https://renshengjihe.cn/10d91204edcc.html">
<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://renshengjihe.cn/10d91204edcc","path":"10d91204edcc.html","title":"树莓派交叉开发环境的搭建"}</script>
<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>树莓派交叉开发环境的搭建 | 人生几何</title>
<noscript>
<link rel="stylesheet" href="/css/noscript.css">
</noscript>
<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/style.css">
<!-- hexo injector head_end end --></head>
<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
<div class="headband"></div>
<main class="main">
<header class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-container">
<div class="site-nav-toggle">
<div class="toggle" aria-label="切换导航栏" role="button">
<span class="toggle-line"></span>
<span class="toggle-line"></span>
<span class="toggle-line"></span>
</div>
</div>
<div class="site-meta">
<a href="/" class="brand" rel="start">
<i class="logo-line"></i>
<h1 class="site-title">人生几何</h1>
<i class="logo-line"></i>
</a>
<p class="site-subtitle" itemprop="description">一个普通程序员的技术博客</p>
</div>
<div class="site-nav-right">
<div class="toggle popup-trigger">
</div>
</div>
</div>
<nav class="site-nav">
<ul class="main-menu menu">
<li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
<li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
<li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
<li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
</ul>
</nav>
</div>
<div class="toggle sidebar-toggle" role="button">
<span class="toggle-line"></span>
<span class="toggle-line"></span>
<span class="toggle-line"></span>
</div>
<aside class="sidebar">
<div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
<ul class="sidebar-nav">
<li class="sidebar-nav-toc">
文章目录
</li>
<li class="sidebar-nav-overview">
站点概览
</li>
</ul>
<div class="sidebar-panel-container">
<!--noindex-->
<div class="post-toc-wrap sidebar-panel">
<div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E4%BA%A4%E5%8F%89%E7%BC%96%E8%AF%91"><span class="nav-number">1.</span> <span class="nav-text">什么是交叉编译</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E7%94%A8%E4%BA%A4%E5%8F%89%E7%BC%96%E8%AF%91"><span class="nav-number">2.</span> <span class="nav-text">为什么要用交叉编译</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%8E%AF%E5%A2%83%E4%BB%8B%E7%BB%8D"><span class="nav-number">3.</span> <span class="nav-text">环境介绍</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#PC-%E6%9C%BA%E7%8E%AF%E5%A2%83%E4%B8%BA-Ubuntu-64%E4%BD%8D%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%EF%BC%8CInterx86%E6%9E%B6%E6%9E%84-CPU"><span class="nav-number">3.1.</span> <span class="nav-text">PC 机环境为 Ubuntu 64位操作系统,Interx86架构 CPU</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A0%91%E8%8E%93%E6%B4%BE%E7%8E%AF%E5%A2%83%E4%B8%BA-RaspBian-32%E4%BD%8D%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%EF%BC%8CARM%E6%9E%B6%E6%9E%84-CPU"><span class="nav-number">3.2.</span> <span class="nav-text">树莓派环境为 RaspBian 32位操作系统,ARM架构 CPU</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%8B%E8%BD%BD%E4%BA%A4%E5%8F%89%E7%BC%96%E8%AF%91%E5%99%A8"><span class="nav-number">4.</span> <span class="nav-text">下载交叉编译器</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A6%96%E5%85%88%E8%A6%81%E7%A1%AE%E4%BF%9D%E6%9C%89-git"><span class="nav-number">4.1.</span> <span class="nav-text">首先要确保有 git</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9C%A8%E7%94%A8%E6%88%B7%E7%9B%AE%E5%BD%95%E4%B8%8B%E5%BB%BA%E7%AB%8B-raspberrypi-%E7%9B%AE%E5%BD%95"><span class="nav-number">4.2.</span> <span class="nav-text">在用户目录下建立 raspberrypi 目录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%8E-github-%E4%B8%8B%E8%BD%BD%E6%89%80%E9%9C%80%E7%9A%84%E4%BA%A4%E5%8F%89%E7%BC%96%E8%AF%91%E5%99%A8"><span class="nav-number">4.3.</span> <span class="nav-text">从 github 下载所需的交叉编译器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B7%BB%E5%8A%A0%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F"><span class="nav-number">4.4.</span> <span class="nav-text">添加环境变量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BC%96%E8%AF%91%E4%B8%80%E4%B8%AAHelloWorld"><span class="nav-number">4.5.</span> <span class="nav-text">编译一个HelloWorld</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BB%BA%E7%AB%8B-PC-%E5%92%8C%E6%A0%91%E8%8E%93%E6%B4%BE%E7%9A%84%E9%80%9A%E4%BF%A1%E6%96%B9%E5%BC%8F"><span class="nav-number">5.</span> <span class="nav-text">建立 PC 和树莓派的通信方式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9C%A8-PC-%E6%9C%BA%E4%B8%8A%E6%89%A7%E8%A1%8C"><span class="nav-number">5.1.</span> <span class="nav-text">在 PC 机上执行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9C%A8%E6%A0%91%E8%8E%93%E6%B4%BE%E4%B8%8A%E6%89%A7%E8%A1%8C"><span class="nav-number">5.2.</span> <span class="nav-text">在树莓派上执行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%AF%8F%E6%AC%A1%E9%83%BD%E8%A6%81%E6%95%B2-ip-%E5%A4%AA%E9%BA%BB%E7%83%A6%EF%BC%9F"><span class="nav-number">5.3.</span> <span class="nav-text">每次都要敲 ip 太麻烦?</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BF%90%E8%A1%8CHelloWorld"><span class="nav-number">5.4.</span> <span class="nav-text">运行HelloWorld</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Makefile-%E6%96%87%E4%BB%B6%E7%BC%96%E5%86%99"><span class="nav-number">6.</span> <span class="nav-text">Makefile 文件编写</span></a></li></ol></div>
</div>
<!--/noindex-->
<div class="site-overview-wrap sidebar-panel">
<div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
<img class="site-author-image" itemprop="image" alt="dhd"
src="/images/avatar.png">
<p class="site-author-name" itemprop="name">dhd</p>
<div class="site-description" itemprop="description">个人博客,分享一些C/C++ Linux 嵌入式等有趣的技术</div>
</div>
<div class="site-state-wrap site-overview-item animated">
<nav class="site-state">
<div class="site-state-item site-state-posts">
<a href="/archives/">
<span class="site-state-item-count">23</span>
<span class="site-state-item-name">日志</span>
</a>
</div>
<div class="site-state-item site-state-categories">
<a href="/categories/">
<span class="site-state-item-count">12</span>
<span class="site-state-item-name">分类</span></a>
</div>
<div class="site-state-item site-state-tags">
<a href="/tags/">
<span class="site-state-item-count">13</span>
<span class="site-state-item-name">标签</span></a>
</div>
</nav>
</div>
<div class="links-of-author site-overview-item animated">
<span class="links-of-author-item">
<a href="https://github.com/MissYourSmile" title="GitHub → https://github.com/MissYourSmile" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
</span>
<span class="links-of-author-item">
<a href="mailto:[email protected]" title="E-Mail → mailto:[email protected]" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
</span>
</div>
</div>
</div>
</div>
</aside>
<div class="sidebar-dimmer"></div>
</header>
<div class="back-to-top" role="button" aria-label="返回顶部">
<i class="fa fa-arrow-up"></i>
<span>0%</span>
</div>
<noscript>
<div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>
<div class="main-inner post posts-expand">
<div class="post-block">
<article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://renshengjihe.cn/10d91204edcc">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.png">
<meta itemprop="name" content="dhd">
<meta itemprop="description" content="个人博客,分享一些C/C++ Linux 嵌入式等有趣的技术">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="人生几何">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
树莓派交叉开发环境的搭建
</h1>
<div class="post-meta-container">
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2019-12-01 17:28:41" itemprop="dateCreated datePublished" datetime="2019-12-01T17:28:41+08:00">2019-12-01</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2021-10-23 00:44:27" itemprop="dateModified" datetime="2021-10-23T00:44:27+08:00">2021-10-23</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E6%A0%91%E8%8E%93%E6%B4%BE/" itemprop="url" rel="index"><span itemprop="name">树莓派</span></a>
</span>
</span>
</div>
<div class="post-description">在 Ubuntu 下搭建树莓派的交叉编译环境,在 PC 机上编译可以在树莓派上运行的代码。</div>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="什么是交叉编译"><a href="#什么是交叉编译" class="headerlink" title="什么是交叉编译"></a>什么是交叉编译</h2><p>交叉编译是在一个平台上生成另一个平台上的可执行代码。同一个体系结构可以运行不同的操作系统;同样,同一个操作系统也可以在不同的体系结构上运行。</p>
<h2 id="为什么要用交叉编译"><a href="#为什么要用交叉编译" class="headerlink" title="为什么要用交叉编译"></a>为什么要用交叉编译</h2><p>原因主要有以下几点:</p>
<ul>
<li>目的平台上不允许或不能够安装我们所需要的编译器,而我们又需要这个编译器的某些特征;</li>
<li>目的平台上的资源贫乏,无法运行我们所需要编译器;</li>
<li>目的平台还没有建立,连操作系统都没有,根本谈不上运行什么编译器。</li>
</ul>
<h2 id="环境介绍"><a href="#环境介绍" class="headerlink" title="环境介绍"></a>环境介绍</h2><h3 id="PC-机环境为-Ubuntu-64位操作系统,Interx86架构-CPU"><a href="#PC-机环境为-Ubuntu-64位操作系统,Interx86架构-CPU" class="headerlink" title="PC 机环境为 Ubuntu 64位操作系统,Interx86架构 CPU"></a>PC 机环境为 Ubuntu 64位操作系统,Interx86架构 CPU</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">dhd@dhd-pc:~$ uname -a</span><br><span class="line">Linux dhd-pc 5.0.0-36-generic <span class="comment">#39~18.04.1-Ubuntu SMP Tue Nov 12 11:09:50 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux</span></span><br></pre></td></tr></table></figure>
<h3 id="树莓派环境为-RaspBian-32位操作系统,ARM架构-CPU"><a href="#树莓派环境为-RaspBian-32位操作系统,ARM架构-CPU" class="headerlink" title="树莓派环境为 RaspBian 32位操作系统,ARM架构 CPU"></a>树莓派环境为 RaspBian 32位操作系统,ARM架构 CPU</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">pi@raspberrypi:~ $ uname -a</span><br><span class="line">Linux raspberrypi 4.19.75-v7+ <span class="comment">#1270 SMP Tue Sep 24 18:45:11 BST 2019 armv7l GNU/Linux</span></span><br></pre></td></tr></table></figure>
<h2 id="下载交叉编译器"><a href="#下载交叉编译器" class="headerlink" title="下载交叉编译器"></a>下载交叉编译器</h2><h3 id="首先要确保有-git"><a href="#首先要确保有-git" class="headerlink" title="首先要确保有 git"></a>首先要确保有 git</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get install git</span><br></pre></td></tr></table></figure>
<h3 id="在用户目录下建立-raspberrypi-目录"><a href="#在用户目录下建立-raspberrypi-目录" class="headerlink" title="在用户目录下建立 raspberrypi 目录"></a>在用户目录下建立 raspberrypi 目录</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mkdir raspberrypi</span><br><span class="line"><span class="built_in">cd</span> raspberrypi</span><br></pre></td></tr></table></figure>
<h3 id="从-github-下载所需的交叉编译器"><a href="#从-github-下载所需的交叉编译器" class="headerlink" title="从 github 下载所需的交叉编译器"></a>从 github 下载所需的交叉编译器</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">clone</span> https://github.com/raspberrypi/tools.git</span><br></pre></td></tr></table></figure>
<p>ps:github 下载过慢的可以将仓库导入到码云(gitee)再进行下载。</p>
<h3 id="添加环境变量"><a href="#添加环境变量" class="headerlink" title="添加环境变量"></a>添加环境变量</h3><p>在 ~/.bashrc 中加入以下内容</p>
<p>如果是 32 位</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">export</span> PATH=<span class="variable">$PATH</span>:<span class="variable">$HOME</span>/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin</span><br></pre></td></tr></table></figure>
<p>如果是 64 位</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">export</span> PATH=<span class="variable">$PATH</span>:<span class="variable">$HOME</span>/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin</span><br></pre></td></tr></table></figure>
<p>保存退出,执行</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">source</span> ~/.bashrc</span><br></pre></td></tr></table></figure>
<p>现在,终端运行如下命令,如果没有提示命令不存在,则说明安装成功</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">arm-linux-gnueabihf-gcc -v</span><br></pre></td></tr></table></figure>
<p>我的运行结果如下:<br><img src="https://renshengjihe-1257851699.cos.ap-guangzhou.myqcloud.com/191201-RaspberryPiTools/gccv.png" alt="gccv"></p>
<h3 id="编译一个HelloWorld"><a href="#编译一个HelloWorld" class="headerlink" title="编译一个HelloWorld"></a>编译一个HelloWorld</h3><p>国际惯例,编译一个 HelloWorld</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">arm-linux-gnueabihf-gcc test.c -o <span class="built_in">test</span></span><br><span class="line">./<span class="built_in">test</span></span><br></pre></td></tr></table></figure>
<p>运行结果如图,现在在PC机上是无法运行这个二进制文件的<br><img src="https://renshengjihe-1257851699.cos.ap-guangzhou.myqcloud.com/191201-RaspberryPiTools/run0nPC.png" alt="run0nPC"></p>
<h2 id="建立-PC-和树莓派的通信方式"><a href="#建立-PC-和树莓派的通信方式" class="headerlink" title="建立 PC 和树莓派的通信方式"></a>建立 PC 和树莓派的通信方式</h2><p>通过 scp 将生成的二进制文件复制到树莓派下,这里记录一下用 ssh 免密登录的配置过程</p>
<h3 id="在-PC-机上执行"><a href="#在-PC-机上执行" class="headerlink" title="在 PC 机上执行"></a>在 PC 机上执行</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ssh-keygen</span><br></pre></td></tr></table></figure>
<p>一路回车,最后会在用户目录下生成一个 .ssh 目录,将其中的公钥文件 id_rsa.pub 复制到树莓派下备用</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">scp ~/.ssh/id_rsa.pub [email protected]:/home/pi/</span><br></pre></td></tr></table></figure>
<p>此处用户名和ip看各自情况而定。</p>
<h3 id="在树莓派上执行"><a href="#在树莓派上执行" class="headerlink" title="在树莓派上执行"></a>在树莓派上执行</h3><p>在树莓派的 .ssh 目录下新建一个 <code>authorized_keys</code> 文件,若已存在,跳过此步骤</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">touch authorized_keys</span><br></pre></td></tr></table></figure>
<p>把刚才 scp 到树莓派下的 PC 机的公钥文件追加到 <code>authorized_keys</code> 末尾</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cat ~/id_rsa.pub >> ~/.ssh/authorized_keys</span><br></pre></td></tr></table></figure>
<p>现在,主机再 ssh 到树莓派将直接使用密钥验证,无需输入密码。</p>
<h3 id="每次都要敲-ip-太麻烦?"><a href="#每次都要敲-ip-太麻烦?" class="headerlink" title="每次都要敲 ip 太麻烦?"></a>每次都要敲 ip 太麻烦?</h3><p>在 PC 机 .ssh 目录下新建一个 config 文件,在其中配置如下信息:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Host pi</span><br><span class="line">Hostname 192.168.123.106</span><br><span class="line">Port 22</span><br><span class="line">User pi</span><br></pre></td></tr></table></figure>
<p>这样就可以通过主机名 pi 来访问到树莓派了,如下</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ssh pi</span><br></pre></td></tr></table></figure>
<h3 id="运行HelloWorld"><a href="#运行HelloWorld" class="headerlink" title="运行HelloWorld"></a>运行HelloWorld</h3><p>把上一步编译出的二进制文件复制到树莓派下</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">scp <span class="built_in">test</span> pi:/home/pi</span><br></pre></td></tr></table></figure>
<p>在树莓派上运行结果如下:<br><img src="https://renshengjihe-1257851699.cos.ap-guangzhou.myqcloud.com/191201-RaspberryPiTools/runOnPi.png" alt="runOnPi"></p>
<h2 id="Makefile-文件编写"><a href="#Makefile-文件编写" class="headerlink" title="Makefile 文件编写"></a>Makefile 文件编写</h2><p>通过 Makefile 文件来更方便的进行编译和文件复制,以下是我写的一个简单的 Makefile 文件</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">CC=arm-linux-gnueabihf-gcc</span><br><span class="line">CP=scp</span><br><span class="line">OUT_FILE=test</span><br><span class="line">OUT_PATH=pi:/home/pi/source/</span><br><span class="line"></span><br><span class="line"><span class="section">all: <span class="variable">$(OUT_FILE)</span></span></span><br><span class="line"></span><br><span class="line"><span class="section">%: %.c</span></span><br><span class="line"> <span class="variable">$(CC)</span> -o <span class="variable">$@</span> <span class="variable">$^</span></span><br><span class="line"></span><br><span class="line"><span class="meta"><span class="meta-keyword">.PHONY</span>: clean install</span></span><br><span class="line"></span><br><span class="line"><span class="section">clean:</span></span><br><span class="line"> <span class="variable">$(RM)</span> <span class="variable">$(OUT_FILE)</span></span><br><span class="line"></span><br><span class="line"><span class="section">install:</span></span><br><span class="line"> <span class="variable">$(CP)</span> <span class="variable">$(OUT_FILE)</span> <span class="variable">$(OUT_PATH)</span></span><br></pre></td></tr></table></figure>
<p>下面是使用过程,还是很方便的<br><img src="https://renshengjihe-1257851699.cos.ap-guangzhou.myqcloud.com/191201-RaspberryPiTools/make.png" alt="make"></p>
</div>
<footer class="post-footer">
<div class="post-tags">
<a href="/tags/%E6%A0%91%E8%8E%93%E6%B4%BE/" rel="tag"># 树莓派</a>
</div>
<div class="post-nav">
<div class="post-nav-item">
<a href="/aa10dbf69217" rel="prev" title="把系统从 deepin 重装成 ubuntu">
<i class="fa fa-chevron-left"></i> 把系统从 deepin 重装成 ubuntu
</a>
</div>
<div class="post-nav-item">
<a href="/fb9641fc5ed6" rel="next" title="Latex笔记">
Latex笔记 <i class="fa fa-chevron-right"></i>
</a>
</div>
</div>
</footer>
</article>
</div>
<div class="comments gitalk-container"></div>
</div>
</main>
<footer class="footer">
<div class="footer-inner">
<div class="copyright">
©
<span itemprop="copyrightYear">2021</span>
<span class="with-love">
<i class="fa fa-heart"></i>
</span>
<span class="author" itemprop="copyrightHolder">dhd</span>
</div>
<div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
</div>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>
<script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-mml-chtml.js","integrity":"sha256-r+3itOMtGGjap0x+10hu6jW/gZCzxHsoKrOd7gyRSGY="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/gitalk.css" integrity="sha256-AJnUHL7dBv6PGaeyPQJcgQPDjt/Hn/PvYZde1iqfp8U=" crossorigin="anonymous">
<script class="next-config" data-name="gitalk" type="application/json">{"enable":true,"github_id":"MissYourSmile","repo":"blog","client_id":"90df46a08046f5b73ae9","client_secret":"fa93eaa0abad10d0f79e056b45ba8a0212d37e0d","admin_user":"MissYourSmile","distraction_free_mode":true,"proxy":"https://cors-anywhere.azm.workers.dev/https://github.com/login/oauth/access_token","language":"zh-CN","js":{"url":"https://cdn.jsdelivr.net/npm/[email protected]/dist/gitalk.min.js","integrity":"sha256-Pmj85ojLaPOWwRtlMJwmezB/Qg8BzvJp5eTzvXaYAfA="},"path_md5":"d9f2a83a54d3516304e8045e2f53fea0"}</script>
<script src="/js/third-party/comments/gitalk.js"></script>
</body>
</html>