Linux系统下的七种编程语言的环境搭建

一、C

1.更新数据源

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.编译并运行

##生成c语言源代码的目标代码
gcc -c main.c -o main.o
##将目标代码编译成可执行文件
gcc -o main.o 
##运行
./main

运行成功会看到输出了hello world!


二、Java

1.更新数据源

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.编译并运行

##编译成java源代码的执行文件
javac main.java
##运行
java main

三、Go

1.更新数据源

apt update

2.安装goland

apt install golang
go version

3.使用vim编写

vim main.go
package main;

func main(){

        println("hello go!");

}

4.编译并运行

##将golang源代码编译成可执行文件
go build -o main_go  main.go
##运行
./main_go

四、Python

1.更新数据源

apt update

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

apt install python3
python3 --version

注意:大多数Linux发行版已经默认安装了Python
3.使用vim编写

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

4.运行

python3 hello.py

五、Rust

1.安装rust

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

2.创建新项目

cargo new hello-rust

这会生成一个名为 hello-rust 的新目录,其中包含以下文件:

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

Cargo.toml 为 Rust 的清单文件。其中包含了项目的元数据和依赖库。
src/main.rs 为编写应用代码的地方。
cargo new 会生成一个新的"Hello, world!"项目!我们可以进入新创建的目录中,执行下面的命令来运行此程序

cd hello-rust
cargo run

3.添加依赖
现在来为应用添加依赖。可以在 crates.io,即 Rust 包的仓库中找到所有类别的库。在 Rust 中,通常把包称作"crates"

在本项目中,使用了名为 ferris-says 的库

在 Cargo.toml 文件中添加以下信息(从 crate 页面上获取):

[dependencies]
ferris-says = "0.3.1"

接着运行:

cargo build

之后 Cargo 就会安装该依赖
4.一个 Rust 小应用

cd src
vim main.rs

现在我们用新的依赖库编写一个小应用,在 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();
}

保存完毕后运行

cargo run

六、Node.js

1.更新数据源

apt update

2.安装

apt install nodejs -y
node -v

3.使用vim编写

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

4.运行

node hello.js

七、C++

1.更新数据源

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.编译并运行

##生成c++源代码的目标代码
g++ -c hello.cpp -o hello.o
##将目标代码编译为可执行文件
g++ hello.o -o hello
##运行
./hello
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容