本篇使用的是 ubuntu 发行版
gcc/g++ 编译运行 C/C++ 代码
安装编译器
先建立并进入文件夹
1
2
3
4mkdir programme
cd programme/
mkdir c
cd c/
输入gcc --version检查是否安装gcc(ubuntu20.04版本是默认自带安装的)
若显示:
1
2
3
4gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
表示已安装,没有的话输入如下命令通过apt(ubuntu/debian)安装(centOS用yum, manjaro/Arch用pacman):
1sudo apt install build-essential
一次性安装gcc, g++, make,安装后同样输入gcc --version检查是否成功安装gcc,若成功回显版本信息,则成功安装
写出你的 Hello world!
这里使用vim编辑器(当然你要是喜欢vi或者nano还有gedit编辑器也不是不可以)
输入vim test.c,进入 vim 编辑器界面,小写状态下按下 i 键进入insert模式,输入如下代码
1
2
3
4
5
6#include
int main(){
printf("Hello world!\n");
return 0;
}
输入后,按下esc,输入:wq并enter以写入并退出
用gcc test.c -o test.o进行编译并生成可执行文件test.o(如果是c++则用g++命令)
用./test.o运行可执行文件
1
2root@LacLic-S-LaPToP:~/programme/c# ./test.o
Hello world!
至此,C/C++编译器配置完成(make是用来编译安装软件的,这里不作演示了)
python3 运行 python 代码
安装python3
先创建并进入python文件夹
1
2
3cd ~/programme/
mkdir python
cd python/
通过输入命令python3或python3 --version查看版本,如果你使用python3,则会进入python交互式编程,在这里可以输入print("hello world")来输出hello world
输入exit()或按下ctrl+Z来退出交互式编程界面
1
2
3
4
5
6
7root@LacLic-S-LaPToP:~/programme/python# python3
Python 3.8.2 (default, Jul 16 2020, 14:00:26)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world")
hello world
>>> exit()
1
2root@LacLic-S-LaPToP:~/programme/python# python3 --version
Python 3.8.2
如果没有安装python3,推荐使用sudo apt install python3快速安装python3
或者,可以用wget下载linux下安装包,通过make编译安装,并自行添加环境变量和建立pip与python的软链
之后,同样通过输入命令python3或python3 --version查看版本,就不过多赘述
执行 python 代码文件
通过echo快速建立test.py文件并写入
1
2echo "print('\n'.join([''.join([('ZhangDong'[(x-y)%9]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else' ')for x in range(-30,30)])for y in range(15,-15,-1)])) # 一个大大的爱心,该版本为python3.x,若为python2.x版本请去掉最外层的括号" > test.py
python3 test.py
然后通过python3 test.py来执行代码
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
31python3 test.py
hangDongZ ngDongZha
ngZhangDongZhangD ZhangDongZhangDon
ongZhangDongZhangDongZhangDongZhangDongZh
ongZhangDongZhangDongZhangDongZhangDongZhan
ongZhangDongZhangDongZhangDongZhangDongZhangD
ngZhangDongZhangDongZhangDongZhangDongZhangDo
gZhangDongZhangDongZhangDongZhangDongZhangDon
ZhangDongZhangDongZhangDongZhangDongZhangDong
hangDongZhangDongZhangDongZhangDongZhangDongZ
angDongZhangDongZhangDongZhangDongZhangDongZh
gDongZhangDongZhangDongZhangDongZhangDongZh
ongZhangDongZhangDongZhangDongZhangDongZh
ngZhangDongZhangDongZhangDongZhangDongZha
hangDongZhangDongZhangDongZhangDongZh
ngDongZhangDongZhangDongZhangDongZh
DongZhangDongZhangDongZhangDongZh
gZhangDongZhangDongZhangDongZ
angDongZhangDongZhangDong
DongZhangDongZhangDon
ZhangDongZhangD
gDongZhan
gZh
h
当然你也可以和写入c文件一样,使用文本编辑器来写代码