20
Created by Storming Robots Elizabeth | STORMING ROBOTS Page 1 of 20 Using the Windows Linux Subsystem last update: August 25th, 2019 Table of Contents Installing the Windows Linux Subsystem................................................................................................................ 3 Potential issues on conflicts in C/C++ installation: ......................................................................................... 8 Install a programming editor .............................................................................................................................. 9 Compiling and linking without using an IDE ........................................................................................................ 10 Sample codes ................................................................................................................................................ 10 how to build (compile + link + execute) ............................................................................................................ 11 The three most basic compiler options you should know: ........................................................................... 11 Basic linux commands you should know .............................................................................................................. 13 Sample Scripting ............................................................................................................................................... 16 Show how to do factorial of a number using “while” .................................................................................. 16 Show how to do factorial of a number using “for”....................................................................................... 16 Automate a test with usaco’s #.in test files .................................................................................................. 16 Read one line at a time from a file. .............................................................................................................. 17 Function Script Samples ............................................................................................................................... 18

Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 1 of 20

Using the Windows Linux Subsystem

last update: August 25th, 2019

Table of Contents

Installing the Windows Linux Subsystem................................................................................................................ 3

Potential issues on conflicts in C/C++ installation: ......................................................................................... 8

Install a programming editor .............................................................................................................................. 9

Compiling and linking without using an IDE ........................................................................................................ 10

Sample codes ................................................................................................................................................ 10

how to build (compile + link + execute) ............................................................................................................ 11

The three most basic compiler options you should know: ........................................................................... 11

Basic linux commands you should know .............................................................................................................. 13

Sample Scripting ............................................................................................................................................... 16

Show how to do factorial of a number using “while” .................................................................................. 16

Show how to do factorial of a number using “for” ....................................................................................... 16

Automate a test with usaco’s #.in test files .................................................................................................. 16

Read one line at a time from a file. .............................................................................................................. 17

Function Script Samples ............................................................................................................................... 18

Page 2: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 2 of 20

...................... 18

doFact() { fname=$1 fact=1 echo "Read from file: $fname" read num < $1 # or # num=$(cat $fname) echo "got $num" for ((i=2; i<=num;i++)) { fact=$((fact * i)) } echo "$num! = $fact" } doFact "1.in"

Page 3: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 3 of 20

Installing the Windows Linux Subsystem

1. Run the PowerShell as an Administrator

a. Type in powershell in the Search window

b. Right mouse click Windows PowerShell and click “Run as administrator”

2. In the PowerShell window type the command:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Page 4: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 4 of 20

You will then see this…

3. Reboot your machine

4. Install Linux

a. Run Microsoft Store

b. Type linux in the search window

c. Select Ubuntu

d. Click Install

e. When finished click Launch

f. You will need to wait a few minutes as the installation completes

Here are some screenshots:

Page 5: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 5 of 20

5. Create user

a. You will be prompted for a username and password

b. Don’t forget your password

Sample user name … DO NOT Use the same one… this is just a sample.

Page 6: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 6 of 20

6. Update Linux

a. Type sudo apt update && sudo apt upgrade (or sudo apt-get …)

b. When prompted to continue, hit Y and enter.

You will see a lot of status display like this:

7. Install Compilers

a. Type sudo apt install gcc

b. When prompted to continue, hit Y and enter.

c. Type “ sudo apt install g++”

d. When prompted to continue, hit Y and enter.

$ sudo apt install gcc

$ sudo apt install g++

Page 7: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 7 of 20

8. Test compiler installation

a. Use the default editor “nano” to create a sample code, such as: main.cpp. At complete

b. Test it out with some simple sample code, such as this:

c. Hit ctrl-x to save and exit

d. Now type: g++ -lm -std=c++0x main.cpp -o main

e. It should have compiled ok. If so type in: ./main

9. You should be set up properly.

$ nano main.cpp

#include <iostream> using namespace std; int main ( ) { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; }

Page 8: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 8 of 20

POTENTIAL ISSUES ON CONFLICTS IN C/C++ INSTALLATION:

Conflict in the g++ installation :

You need to rebuild the dependency :

If this still does not resolve the conflict, you may need to reinstall the following packets. However, do it one at

a time, and watch for messages…

There may be time when it told you to do the following, do it as instructed:

$ sudo apt-get install libgmpv4-dev

$ sudo apt-get install g++

sudo apt-get install gcc-5-multilib

sudo apt-get install lib32gcc1

sudo apt-get install libx32gcc1

sudo apt-get install gcc-5

sudo apt-get install libx32gcc-5-dev

$ sudo apt autoremove

Page 9: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 9 of 20

INSTALL A PROGRAMMING EDITOR

There are quite a few programming editors available. Here lists 3 commonly used ones. To install : “sudo apt-

get update” before installing another package

1) Nano

⎯ Light weight text editor, but the most primitive one. Just run it from the shell command

window.

⎯ Pretty easy to navigate, nothing fancy though. No plug-ins features, but may utilize its

configuration file to customize somewhat.

⎯ Should come pre-installed.

2) Kate

⎯ This is a hefty graphical editor. Will need to run a graphical terminal windows in order to run

this. Cannot run this in the simple command shell window.

⎯ Some find it much more intuitive to navigate. Some plug-ins.

⎯ Need to install yourself : “ sudo apt-get install kate “

3) VIM

⎯ This is a light weight editor. Can run this in the simple command shell window.

⎯ Some find it difficult to navigate. Many plug-ins available, more flexible than Kate. Based on

SLANT community , Vim is ranked 1st. It is a lighter weight and fast editor.

⎯ Mostly come pre-installed, but you may need to install the plugins for some advanced features.

“…Vim is a powerful, portable, keyboard based text editor. Being text-based, vim is lightning fast,

with an incredible set of features developed over its multi decade existence….” By SLANT.

I highly recommend to learn how to use VI.

Reference this packet to have a crash course using VI.

Concise lookup sheet:

- https://www.cheatography.com/typo209/cheat-sheets/comprehensive-vim-cheat-sheet/pdf

- http://www.nathael.org/Data/vi-vim-cheat-sheet.svg

Interactive vi Editor Tutorial: Here you can practice Vim with context-aware help menu

You may want to customize color right at the beginning. Here is how:

➢ Look up available default color scheme from /usr/share/vim/vim74/colors/

➢ Edit .vimrc

➢ E.g. to add murphy scheme, add “color murphy” into the .vimrc

Page 10: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 10 of 20

Compiling and linking without using an IDE

SAMPLE CODES

//main.cpp - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #include <stdio.h> #include <myH1.h> #include <myH2.h> int main () { ... write the code to test the functions.. e.g. cout << "Enter number to find fib: "; cin >> num; cout << fib(num) << endl; }

// <myH1.h> int fact (int i); int fib (int);

// <myH2.h> int gcd (int, int);

// myFuncs2.cpp - - - - - - - - - - - - - #include <iostream> using namespace std; #include <stdio.h> #include <myH2.h> int gcd(int a, int b) { … }

//myFuncs1.cpp - - - - - - - - - - - - - #include <iostream> using namespace std; #include <stdio.h> #include <stdlib.h> #include <string.h> #include <myH1.h> int fact(int i) { … } int fib(int order) { … }

Page 11: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 11 of 20

HOW TO BUILD (COMPILE + LINK + EXECUTE)

1) How to build (Compile and link) a single executable out of several files

2) How to build a static library file (*.a) out of several files (no main() ) , then link the static library *.a

file(s) with your main file (the one with main() ) .

3) How to build a shared library file (*.so) out of several files (no main() ) , then link the shared library

*.so file(s) with your main file (the one with main() ) .

THE THREE MOST BASIC COMPILER OPTIONS YOU SHOULD KNOW :

-I : (upper case i) include path for header files -L : Library path -D : Preprocessor macro

Compile and link a single executable out of several files. Note the main.cpp is the only file containing the

function main()

g++ -Wall main.cpp myFuncs1.cpp myFuncs2.cpp -I./ -o runThis

Build a static library file (*.a) out of several files (no main() ) , then link the static library *.a file(s) with your

main file (the one with main() ) .

g++ -Wall -c myFuncs1.cpp myFuncs2.cpp -I./ ar -cvq libSample.a myFuncs1.o myFuncs2.c ar -t libSample.a g++ -Wall main.cpp -L./ - lSample -I./ -o runThis2

How to build a shared library file (*.so) out of several files (no main() ) , then link the shared library *.so file(s)

with your main file (the one with main() ) .

g++ -Wall -c -fPIC myFuncs1.cpp myFuncs2.cpp -I./ g++ -shared -o libmySample.so myFuncs1.o myFuncs2.o g++ -Wall main.cpp -L./ - lmySample - I./ -o runThis3

Note that the “-L./” and “-I./” indicates the path where the build will find the library file and header file

respectively.

Page 12: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 12 of 20

May use LD_LIBRARY_PATH to add in customize library path, C_INCLUDE_PATH to add in customized include

path.

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./ # if your *.so file is in ./ export C_INCLUDE_PATH=$C_INCLUDE_PATH:./ # if your *.h files are in ./ g++ -Wall main.cpp - lmyFuncs -o runThis3 # do not need -L , -I

To demonstrate how the “-D” commonly being used:

g++ -Wall main.cpp myFuncs1.cpp myFuncs2.cpp -I./ -o runThis -D_SR_ ./runThis

Try this, and observe the difference from build it without -D_SR_

Reference (for windows OS instead):

⎯ Setting the Path and Environment Variables for Command-Line Builds ⎯ Walkthrough: Compiling a C Program on the Command Line

Page 13: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 13 of 20

Basic linux commands you should know

Here is a very small set of commands that you should know. Try them out and observe what it does.

To look up full description of a command, use “man”. See below.

***(Observe the behavior after each command. Be Inquisitive)

1. ls # l = lower case of L 2. man ls 3. cd / 4. pwd 5. cd ~/; pwd 6. mkdir playDir; pwd 7. cd playDir 8. ls - l # - l = lower case of L 9. ls - la 10. ls - ltra 11. cd .. 12. cd /mnt/c # This is your C Drive. Be curious! Check out the sub-folders... 13. ls - l 14. ls - ltRa | more # this is LONG! So do “Ctrl-C” at any time to get out 15. cd ~/; pwd 16. ls - l 17. mv playDir play 18. ls - l 19. echo “This is the 1st line.” > test1.txt 20. echo “This is the 2nd line.” >> test1.txt 21. cat test1.txt 22. echo “This is a hidden file.” > .test2.txt 23. ls - ltra > test1.txt 24. ls - l | wc -l >> test1.txt 25. echo “This is the end \! ” >> test1.txt 26. cat test1.txt 27. grep “this is” test1.txt 28. grep -i “this is” test1.txt 29. cd ~ 30. pwd

Page 14: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 14 of 20

31. clear 32. ls - lr # - l = lower case of L 33. dir - lr 34. ls - lR 35. dir - lR 36. ls - l | grep " \̂.” # list all hidden files and directories 37. ls - l | grep “̂ d” # list sub-directories 38. ls - ltR | grep “̂ d” 39. ls - l | wc -l # count of nodes (files, directories, links, etc.) 40. ls - lR | grep " d̂” 41. alias ldir=’ls - lR | grep " d̂"’ #you just created a new command! 42. ldir 43. clear 44. date 45. date +%D 46. date +"%T" 47. time 48. man -? > help.txt 49. tail -f help.txt 50. ls /root/ 51. sudo ls /root/ 52. wget https://www.stormingrobots.com/prod/pdf/csSyllabus.pdf & 53. du 54. du -sh 55. du -h * -d 1 56. du – h * -d 3 57. du -h * | sort -n # show and sort files by disk space usage size 58. du -h * | sort -nr 59. du -a . | sort -nr | head 60. du -a . | sort -nr | tail 61. df 62. df -h 63. df /home 64. echo "echo \"Hello World \" " > test.sh 65. cat test.sh 66. ls - l test.sh # you see: -rw-r- -r- - 1 … test.sh 67. ./test.sh # fail to run 68. chmod +x test.sh 69. ls - l test.sh # you see: -rwxr-xr-x 1 … test.sh

#& == runs in background

Page 15: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 15 of 20

70. ./test.sh 71. ps -ef 72. watch -n 1 "ps -ef" & 73. ps # find the PID (process ID) for this watch command 74. kill 3000 # replace 3000 with the PID of the “watch” process 75. ps # now you see the process is gone 76. clear 77. history 78. !! 79. !w Find a number corresponding to one of the previous commands. E.g. one of them is 50:

80. !50 # if 50 exists in your history commands list 81. top 82. Ctrl+Z # this suspend the process “top” 83. jobs # find the job # , e.g. [9] 84. fg 9 # type the # 9 from, the previous finding 85. bg 9 # put back to the background . Now, you should try to find the process and kill it…

86. find . -name test.sh # locate test.sh from all sub-folders under current dir 87. find . -print | grep -i lx # only list those files with “lx” 88. uptime 89. lscpu 90. ps ahuxf | sort | head -5 # short top 5 cpu usage processes 91. htop # show processes with top cpu usage ; you may need to install it first

Special note about redirection ( > )

About standard file descriptors : 1, 2 == standard output and standard errors

⎯ Syntax: command > file same as command 1> file

⎯ echo “hello” > file == echo “hello” 1> file

⎯ echo “hello” >&2 file == echo “hello” 1>&2 file

⎯ echo “hello” 1>/dev/null == suppress the standard output

Page 16: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 16 of 20

SAMPLE SCRIPTING

Note: should always start the script with “#!/bin/bash”

SHOW HOW TO DO FACTORIAL OF A NUMBER USING “WHILE”

echo "do Fact ... " echo "Enter a number: " read num fact=1 echo "get Factorial($num) with while" #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - while [ $num -gt 1 ] do fact=$((fact * num)) num=$((num - 1)) done echo $fact

SHOW HOW TO DO FACTORIAL OF A NUMBER USING “FOR”

for ((i=2; i<=num;i++)) { fact=$((fact * i)) }

AUTOMATE A TEST WITH USACO’S #.IN TEST FILES

printf "runThis.sh <exeName> <max # test>...\n\n " read exeName num let i=1 while [ $((i)) - le $num ] do printf "test $i.in...\n" cp $i.in $exeName.in diff $i.out $exeName.out i=$((i+1)) done

Page 17: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 17 of 20

READ ONE LINE AT A TIME FROM A FILE.

#!/bin/bash echo "display the whole file- - - - - - -" cat file.txt echo "display whole file without linebreak-- - - - -" echo $(cat file.txt) echo "display each word-- - - - - - - " for word in $(cat file.txt) do echo "$word" done echo "display each line-- - - - - - " old_IFS=$IFS IFS=$'\n' # new field separator for line in $(cat file.txt) do echo ":: $line" done IFS=$old_IFS echo "display each line use file redirect - - - - - - - " while read line do echo -e ">> $line \n" done < file.txt

Page 18: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 18 of 20

FUNCTION SCRIPT SAMPLES

The following samples show how to read a number from a file.

Sample 1: passing in a string argument

doFact() { fname=$1 fact=1 echo "Read from file: $fname" read num < $1 # or # num=$(cat $fname) echo "got $num" for ((i=2; i<=num;i++)) { fact=$((fact * i)) } echo "$num! = $fact" } doFact "1.in"

Page 19: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 19 of 20

Sample2: passing in 2 arguments

More reference: Learn more on important Linux commands.

doFact() { num=$1 fact=1 echo "Read from file: $2" if [[ $2 = 1 ]] then echo "do while ... " echo "get Factorial($num) with while" while [ $num -gt 1 ] do fact=$((fact * num)) num=$((num - 1)) done echo $fact else echo "do for ... " echo "get Factorial($num) with for" for ((i=2; i<=num;i++)) { fact=$((fact * i)) } echo $fact fi } echo -e "do Factorial ...: e.g.\n func.sh (do factorial using for)" echo -e "\n func.sh 1 (do factorial using while)\n\n" echo "Enter a number: " read num doFact $num "file.txt"

Page 20: Using the Windows Linux Subsystem - Storming …...Created by Storming Robots Elizabeth | STORMING ROBOTS Page 3 of 20 Installing the Windows Linux Subsystem 1. Run the PowerShell

Created by Storming Robots

Elizabeth | STORMING ROBOTS Page 20 of 20