Linux系統(tǒng)下的七種編程語(yǔ)言的環(huán)境搭建

一、C

1.更新數(shù)據(jù)源

apt update

2.下載gcc編譯器并查看是否安裝成功

apt install gcc 
gcc --version

3.使用vim編寫

vim main.c

編寫如下代碼

#include <stdio.h>

int main(){
    printf("hello world!\n");
    return 0;
}

4.編譯并運(yùn)行

##生成c語(yǔ)言源代碼的目標(biāo)代碼
gcc -c main.c -o main.o
##將目標(biāo)代碼編譯成可執(zhí)行文件
gcc -o main.o 
##運(yùn)行
./main

運(yùn)行成功會(huì)看到輸出了hello world!


二、Java

1.更新數(shù)據(jù)源

apt update

2.下載jdk

apt install openjdk-17-jdk -y
java -version

3.使用vim編寫

vim main.java

編寫如下代碼

public class main {
    public static void main(String[] args) {
        System.out.println("hello java!");
    }
}

4.編譯并運(yùn)行

##編譯成java源代碼的執(zhí)行文件
javac main.java
##運(yùn)行
java main

三、Go

1.更新數(shù)據(jù)源

apt update

2.安裝goland

apt install golang
go version

3.使用vim編寫

vim main.go
package main;

func main(){

        println("hello go!");

}

4.編譯并運(yùn)行

##將golang源代碼編譯成可執(zhí)行文件
go build -o main_go  main.go
##運(yùn)行
./main_go

四、Python

1.更新數(shù)據(jù)源

apt update

2.安裝python并查看是否安裝成功

apt install python3
python3 --version

注意:大多數(shù)Linux發(fā)行版已經(jīng)默認(rèn)安裝了Python
3.使用vim編寫

vim hello.py
print("hello python!");

4.運(yùn)行

python3 hello.py

五、Rust

1.安裝rust

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
cargo --version

2.創(chuàng)建新項(xiàng)目

cargo new hello-rust

這會(huì)生成一個(gè)名為 hello-rust 的新目錄,其中包含以下文件:

hello-rust
|- Cargo.toml
|- src
  |- main.rs

Cargo.toml 為 Rust 的清單文件。其中包含了項(xiàng)目的元數(shù)據(jù)和依賴庫(kù)。
src/main.rs 為編寫應(yīng)用代碼的地方。
cargo new 會(huì)生成一個(gè)新的"Hello, world!"項(xiàng)目!我們可以進(jìn)入新創(chuàng)建的目錄中,執(zhí)行下面的命令來(lái)運(yùn)行此程序

cd hello-rust
cargo run

3.添加依賴
現(xiàn)在來(lái)為應(yīng)用添加依賴??梢栽?crates.io,即 Rust 包的倉(cāng)庫(kù)中找到所有類別的庫(kù)。在 Rust 中,通常把包稱作"crates"

在本項(xiàng)目中,使用了名為 ferris-says 的庫(kù)

在 Cargo.toml 文件中添加以下信息(從 crate 頁(yè)面上獲取):

[dependencies]
ferris-says = "0.3.1"

接著運(yùn)行:

cargo build

之后 Cargo 就會(huì)安裝該依賴
4.一個(gè) Rust 小應(yīng)用

cd src
vim main.rs

現(xiàn)在我們用新的依賴庫(kù)編寫一個(gè)小應(yīng)用,在 main.rs 中添加以下代碼:

use ferris_says::say;
use std::io::{stdout, BufWriter};

fn main() {
    let stdout = stdout();
    let message = String::from("Hello fellow Rustaceans!");
    let width = message.chars().count();

    let mut writer = BufWriter::new(stdout.lock());
    say(&message, width, &mut writer).unwrap();
}

保存完畢后運(yùn)行

cargo run

六、Node.js

1.更新數(shù)據(jù)源

apt update

2.安裝

apt install nodejs -y
node -v

3.使用vim編寫

vim hello.js
console.log("hello node!");

4.運(yùn)行

node hello.js

七、C++

1.更新數(shù)據(jù)源

apt update

2.安裝g++

apt install g++
g++ --version

3.使用vim編寫

vim hello.cpp
#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

4.編譯并運(yùn)行

##生成c++源代碼的目標(biāo)代碼
g++ -c hello.cpp -o hello.o
##將目標(biāo)代碼編譯為可執(zhí)行文件
g++ hello.o -o hello
##運(yùn)行
./hello
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容