linux编译运行c,c++,java 运行C
安装gcc
1 [root@localhost ~]# yum install gcc
就地写一个C程序
1 2 3 4 5 6 7 8 9 10 11 12 13 [root@localhost ~]# vi hello.c #include <stdio.h> int main () { int n; int sum=0 ; scanf ("%d" ,&n); for (int i=1 ;i<=n;i++){ sum+=1 ; } printf ("sum is %d\n" ,sum); return 0 ; }
编译输出为hello.out
1 2 3 4 5 6 7 8 9 [root@localhost ~]# gcc hello.c hello.c: In function ‘main’: hello.c:6:2: error: ‘for’ loop initial declarations are only allowed in C99 mode for(int i=1;i<=n;i++){ ^ hello.c:6:2: note: use option -std=c99 or -std=gnu99 to compile your code [root@localhost ~]# gcc -std=c99 hello.c -o hello.out [root@localhost ~]# ls anaconda-ks.cfg hello.c hello.out mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar
运行
1 2 3 [root@localhost ~]# ./hello.out 12 sum is 12
运行C++
安装gcc-c++
1 [root@localhost ~]# yum install gcc-c++
就地写一个C++程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@localhost ~]# vi hello.cpp #include <iostream> #include <algorithm> using namespace std ;int main () { int s[10 ]; for (int i=0 ;i<5 ;i++){ cin >>s[i]; } sort(s,s+5 ); for (int i=0 ;i<5 ;i++){ if (i) cout <<" " ; cout <<s[i]; } cout <<endl ; return 0 ; }
编译
1 2 3 [root@localhost ~]# g++ hello.cpp -o hellocpp.out [root@localhost ~]# ls anaconda-ks.cfg hello.c hello.cpp hellocpp.out hello.out mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar
运行
1 2 3 [root@localhost ~]# ./hellocpp.out 12 32 535 6 34 6 12 32 34 535
运行java jdk包名:jdk-15.0.1_linux-x64_bin.tar.gz
java环境文件丢到/lib
1 2 3 4 5 6 7 8 9 [root@localhost ~]# mkdir /lib/java [root@localhost ~]# mv ./jdk-15.0.1_linux-x64_bin.tar.gz /lib/java [root@localhost ~]# cd /lib/java [root@localhost java]# ls jdk-15.0.1_linux-x64_bin.tar.gz [root@localhost java]# tar -zxvf jdk-15.0.1_linux-x64_bin.tar.gz jdk-15.0.1/bin/jaotc jdk-15.0.1/bin/jar ...
配置全局变量
1 2 3 4 5 6 7 8 [root@localhost java]# vi /etc/profile # shift +g 末尾加入 export JAVA_HOME=/lib/java/jdk-15.0.1 export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib : wq
重新导入变量
1 2 3 4 5 [root@localhost java]# source /etc/profile [root@localhost java]# java -version java version "15.0.1" 2020-10-20 Java(TM) SE Runtime Environment (build 15.0.1+9-18) Java HotSpot(TM) 64-Bit Server VM (build 15.0.1+9-18, mixed mode, sharing)
就地写一个java程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@localhost java]# vi hello.java import java.util.Scanner;public class hello { public static void main (String args[]) { Scanner scan = new Scanner(System.in); System.out.println("next" ); if (scan.hasNext()){ String str1=scan.next(); System.out.println("输入的数据为:" +str1); } scan.close(); } }
编译运行
1 2 3 4 5 [root@localhost ~]# javac hello.java [root@localhost ~]# java hello next asd 输入的数据为:asd
依赖库
全局的库路径在/lib64,用户库路径/usr/lib64