raspberry pi_Raspberry Pi上的C语言中的Hello World

raspberry pi

This set of instructions won't suit everybody but I'll try to be as generic as possible. I installed the Debian Squeeze distribution, so the programming tutorials are based on that. Initially, I'm starting off by compiling programs on the Raspi but given its relative slowness to any PC in the last ten years, it's probably best to switch to developing on another PC and copying the executables over.

这组说明并不适合所有人,但我会尽量做到通用。 我安装了Debian Squeeze发行版,因此编程指南是基于此的。 最初,我从在Raspi上编译程序开始,但考虑到它在过去十年中相对于任何PC而言都相对较慢,最好切换到在另一台PC上进行开发并复制可执行文件。

I'll cover that in a future tutorial, but for now, it's about compiling on the Raspi.

我将在以后的教程中介绍它,但是现在,它是关于在Raspi上进行编译的。

为开发做准备 ( Preparing for Developing )

The starting point is you have a Raspi with a working distribution. In my case, it's Debian Squeeze which I burnt with instructions from the RPI Easy SD Card Setup. Make sure you bookmark the Wiki as it's got tons of useful stuff.

起点是具有有效分配的Raspi。 就我而言,这是Debian Squeeze,我根据RPI Easy SD Card Setup中的说明进行了刻录 。 确保将Wiki标记为书签因为它包含大量有用的内容。

If your Raspi has booted and you've logged in (username pi, p/w = raspberry) then type gcc - v at the command line. You'll see something like this:

如果您的Raspi已启动并且已经登录(用户名pi,p / w = raspberry),则在命令行中键入gcc-v。 您会看到以下内容:


Using built-in specs.
Target: arm-linux-gnueabi
Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib
--enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --disable-sjlj-exceptions --enable-checking=release --build=arm-linux-gnueabi
--host=arm-linux-gnueabi --target=arm-linux-gnueabi
Thread model: posix
gcc version 4.4.5

安装Samba ( Install Samba )

One of the first things I did and recommend to you if you have a Windows PC on the same network as your Raspi is to install and setup Samba so you can access the Raspi. Then I issued this command:

如果您的Windows PC与Raspi位于同一网络上,则我建议做的第一件事就是安装和设置Samba,以便您可以访问Raspi。 然后我发出了以下命令:


gcc -

To get the above listing into the file l.txt that I could view and copy on my Windows PC.

要将上面的清单放入文件l.txt中,我可以在Windows PC上查看和复制该文件。

Even if you are compiling on the Raspi, you can edit source code from your Windows box and compile on the Raspi. You can't just compile on your Windows box using say MinGW unless your gcc is configured to output ARM code. That can be done but let's learn to walk first and learn how to compile and run programs on the Raspi.

即使您在Raspi上进行编译,也可以从Windows框中编辑源代码并在Raspi上进行编译。 除非将gcc配置为输出ARM代码,否则您不能仅使用诸如MinGW这样的Windows窗口进行编译。 可以做到的,但让我们学会先走,学习如何在Raspi上编译和运行程序。

GUI或终端 ( GUI or Terminal )

I'll assume that you are new to Linux, so apologies if you know it already. You can do most of the work from the Linux terminal (= command line). But it can be easier if you fire up the GUI (Graphical User Interface) to have a look around the file system. Type startx to do that.

我假设您是Linux的新手,如果您已经知道的话,我们深表歉意。 您可以从Linux终端( =命令行 )完成大部分工作。 但是,如果启动GUI(图形用户界面)以浏览文件系统,则可能会更容易。 键入startx来执行此操作。

The mouse cursor will appear and you can click in the bottom left-hand corner (it looks like a mountain( to see the menus. Click on Accessories and run File Manager to let you view folders and files.

将会出现鼠标光标,您可以单击左下角(它看起来像山头(以查看菜单)。单击附件并运行文件管理器以查看文件夹和文件。

You can close it down any time and return to the terminal by clicking the little red button with a white circle in the bottom right-hand corner. Then click on Logout to return to the command line.

您可以随时关闭它,然后通过单击右下角带有白色圆圈的红色小按钮返回到终端。 然后单击注销以返回到命令行。

You may prefer to have the GUI open all the time. When you want a terminal click the bottom left button then click Other on the menu and Terminal. In the Terminal, you can close it by typing Exit or click the Windows like x in the top right-hand corner.

您可能希望始终打开GUI。 当您需要一个终端时,单击左下方的按钮,然后在菜单和终端上单击其他 。 在终端中,您可以通过键入Exit来关闭它,或单击右上角的x之类的Windows。

资料夹 ( Folders )

The Samba instructions on the Wiki tell you how to setup a public folder. It's probably best to do that. Your home folder (pi) will be readonly and you want to write to the public folder. I created a sub-folder in public called code and created the hello.c file listed below in it from my Windows PC.

Wiki上的Samba说明告诉您如何设置公用文件夹。 最好这样做。 您的主文件夹(pi)将是只读的,并且您想写入公用文件夹。 我在公开的代码中创建了一个子文件夹,并从Windows PC中创建了下面列出的hello.c文件。

If you prefer to edit on the PI, it comes with a text editor called Nano. You can run it from the GUI on the other menu or from the terminal by typing

如果您希望在PI上进行编辑,则它带有一个名为Nano的文本编辑器。 您可以通过其他菜单上的GUI或从终端中输入以下内容来运行它


sudo nano

The sudo elevates nano so it can write files with root access. You can run it just as nano, but in some folders that won't give you write access and you won't be able to save files so running things with sudo is usually best.

sudo提升了nano的功能,因此它可以通过root用户访问来写入文件。 您可以将其作为nano来运行,但是在某些文件夹中,该文件夹将不授予您写访问权限,并且您将无法保存文件,因此通常最好使用sudo运行。

你好,世界 ( Hello World )

Here's the code:

这是代码:


#include
int main() {
printf("Hello World\n");

Now type in gcc -o hello hello.c and it will compile in a second or two.

Take a look at the files in the terminal by typing in ls -al and you'll see a file listing like this:

and type in ./hello to execute the compiled program and see Hello World.

That completes the first of the "programming in C on your Raspberry Pi" tutorials.


#include
int main() {
printf("Hello World\n");

Now type in gcc -o hello hello.c and it will compile in a second or two.

Take a look at the files in the terminal by typing in ls -al and you'll see a file listing like this:

and type in ./hello to execute the compiled program and see Hello World .

That completes the first of the "programming in C on your Raspberry Pi" tutorials.

翻译自: https://www.thoughtco.com/hello-world-in-c-raspberry-pi-958619

raspberry pi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值