2020年10月23日 星期五

各種程式語言 Hello World!

Hello World!是每一種程式語言問世都會說的一句問候世界的話. 依據TIOBE的統計, 2013年10月份前20大廣受使用的程式語言如下:

就讓Raspberry Pi來以不同的程式語言說說Hello World!

  1. C語言:
    C語言的編譯器gcc原本就已經安裝了, 查看gcc的版本:

    pi@raspberrypi ~ $ gcc -v
    Using built-in specs.
    COLLECT_GCC=gcc
    COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/4.6/lto-wrapper
    Target: arm-linux-gnueabihf
    Configured with: ../src/configure -v --with-pkgversion='Debian 4.6.3-14+rpi1' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-sjlj-exceptions --with-arch=armv6 --with-fpu=vfp --with-float=hard --enable-checking=release --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
    Thread model: posix
    gcc version 4.6.3 (Debian 4.6.3-14+rpi1)

    pi@raspberrypi ~ $ nano helloworld.c
    pi@raspberrypi ~ $ cat helloworld.c
    #include <stdio.h>

    int main() {
    printf("Hello World! \n");
    return 0;
    }
    pi@raspberrypi ~ $ gcc -o helloworld helloworld.c
    pi@raspberrypi ~ $ ./helloworld
    Hello World!

  2. Java:
    Oracle推出ARM版的Java:

    pi@raspberrypi ~/workbench $ sudo apt-get install oracle-java7-jdk
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following extra packages will be installed:
    libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxi6 libxrender1 libxtst6 x11-common
    The following NEW packages will be installed:
    libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxi6 libxrender1 libxtst6 oracle-java7-jdk x11-common
    0 upgraded, 11 newly installed, 0 to remove and 0 not upgraded.
    Need to get 72.0 MB/72.3 MB of archives.
    After this operation, 163 MB of additional disk space will be used.
    Do you want to continue [Y/n]? y

    pi@raspberrypi ~/workbench $ nano helloworld.java
    pi@raspberrypi ~/workbench $ cat helloworld.java
    public class helloworld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
    

    }
    pi@raspberrypi ~/workbench $ javac helloworld.java
    pi@raspberrypi ~/workbench $ java helloworld
    Hello World!

  3. Objective-C
    Objective-C是非常OS X的東西, 主要是SmallTalk對C的擴展, 如過在Linux上要編譯Objective-C是可行, 但似乎不是有很大的意義, 通常需要安裝:

    $ sudo apt-get install gnustep gobjc

在此先不探討編譯Objective-C的做法.

  1. C++
    C++可以用gcc編譯, 也可以用g++編譯. 如果用g++編譯, 如下:

    pi@raspberrypi ~/workbench $ nano helloworld.cpp
    pi@raspberrypi ~/workbench $ cat helloworld.cpp
    #include

    int main()
    {
    std::cout << "Hello, world!\n";
    }
    pi@raspberrypi ~/workbench $ g++ -o helloworld helloworld.cpp
    pi@raspberrypi ~/workbench $ ./helloworld
    Hello, world!

如果是用gcc編譯, 就要指定stdc++的函式庫, 如下所示:

pi@raspberrypi ~/workbench $ gcc -o helloworld -lstdc++  helloworld.cpp 
pi@raspberrypi ~/workbench $ ./helloworld 
Hello, world!
  1. PHP:
    之前已經介紹如何安裝PHP-FPM, 現在要用命令列的方式來執行PHP腳本, 就要安裝php5:

    pi@raspberrypi ~/workbench $ sudo apt-get install php5

安裝好PHP命令列, 查看PHP的版本:

pi@raspberrypi ~/workbench $ php -v
PHP 5.4.4-14+deb7u5 (cli) (built: Oct 13 2013 04:49:27) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies

執行PHP版本的Hello World!:

pi@raspberrypi ~/workbench $ nano helloworld.php
pi@raspberrypi ~/workbench $ cat helloworld.php
<?php
  printf("Hello, world!
");
?>
pi@raspberrypi ~/workbench $ php helloworld.php 
Hello, world!
  1. C#
    C#是微軟.NET平台的語言, MONO (http://www.mono-project.com/Main\_Page)是一個開放原始碼版本的.NET平台, 可以在Linux系統中運行.NET系統, 相容性至少有90%, 也許更高. 在Raspbian中安裝MONO也很容易:

    pi@raspberrypi ~/workbench $ sudo apt-get install mono-complete

但是要安裝的套件有389MB之多:

Need to get 147 MB/147 MB of archives.
After this operation, 389 MB of additional disk space will be used.
Do you want to continue [Y/n]? y

會安裝這麼多的套件是因為要在Raspberry Pi中執行編譯的工作, 只好把整個MONO系統全部安裝進Raspbian中, 如果只是要執行Visual Studio中編譯好的程式, 只需要安裝runtime就好了:

sudo apt-get install mono-runtime

安裝的過程很久, 但總算是可以編譯C#了, 以下是過程:

pi@raspberrypi ~/workbench $ nano helloworld.cs
pi@raspberrypi ~/workbench $ cat helloworld.cs
using System;

public class HelloWorld
{
    public static void Main()
    {
        Console.WriteLine("Hello World!");
    }
}
pi@raspberrypi ~/workbench $ gmcs helloworld.cs
pi@raspberrypi ~/workbench $ ls -ls helloworld.exe 
4 -rwxr-xr-x 1 pi pi 3072 Oct 21 14:20 helloworld.exe
pi@raspberrypi ~/workbench $ mono helloworld.exe
Hello World!
  1. (Visual) Basic
    由於排在第11位的是Visual Basic .NET, 很顯然地, 這是Visual Basic或是一般的Basic. 在Linux上似乎沒有直接支援Basic, 但是有一個有趣的專案: Gambas (http://gambas.sourceforge.net/en/main.html), 這幾乎是Basic了. 在Debian上的安裝也很容易:

    pi@raspberrypi ~/workbench $ sudo apt-get install gambas3

但是Gambas是一個IDE(集成開發環境, Integrated Development Environment), 需要圖形介面, 因此找不到用命令列的方式來說Hello World!.

  1. Python
    很多人喜歡這個語言, 喜歡它的簡潔, Python是Raspberry Pi上受歡迎的語言, 只是, 它的執行速度不是很快, 目前的有兩個並行版本: 2.7.x和3.x, 目前在我的Raspberry Pi上的是2.7.3版.

    pi@raspberrypi ~/workbench $ python --version
    Python 2.7.3

由於Python是解譯語言, 因此可以直接由Python解譯:

pi@raspberrypi ~/workbench $ nano helloworld.py
pi@raspberrypi ~/workbench $ cat helloworld.py
print "Hello, World!" 
pi@raspberrypi ~/workbench $ python helloworld.py 
Hello, World!
  1. Transact-SQL
    這是SQL語言, 要它說Hello World!好像不切實際, 就跳過囉.

  2. Javascript
    這是網頁語言, 之前已經介紹過, 也安裝了node.js, Javascript天天都和世界說Hello.

11.Visual Basic .NET
這又回到了和C#一樣的MONO平台了. 需要安裝一個Visual Basic.NET的編譯器套件:

pi@raspberrypi ~/workbench $ sudo apt-get install mono-vbnc

安裝好vnbc之後, 就可以向世界說Hello了:

pi@raspberrypi ~/workbench $ nano helloworld.vb
pi@raspberrypi ~/workbench $ vbnc helloworld.vb 
Visual Basic.Net Compiler version 0.0.0.5943
Copyright (C) 2004-2010 Rolf Bjarne Kvinge. All rights reserved.

Assembly 'helloworld, Version=0.0, Culture=neutral, PublicKeyToken=null' saved successfully to '/home/pi/workbench/helloworld.exe'.
Compilation successful
Compilation took 00:00:12.4735250
pi@raspberrypi ~/workbench $ mono helloworld.exe 
Hello world!
  1. Perl
    Perl是一個腳本語言, 也是解譯語言, Perl幾乎存在於每一個Linux中, 對Linux來說, 更是一個重大的語言, 用Perl可以開發大型網站系統, 當然, 要Perl說Hello World!也很容易:
pi@raspberrypi ~/workbench $ nano helloworld.pl
pi@raspberrypi ~/workbench $ cat helloworld.pl
#!/usr/bin/perl
print "Hello World!\n";
pi@raspberrypi ~/workbench $ chmod +x helloworld.pl
pi@raspberrypi ~/workbench $ ./helloworld.pl 
Hello World!

資料來源: https://ithelp.ithome.com.tw/articles/10139347

沒有留言: