1.前言
在Windows下使用Linux常見的軟件存在許多困難。選擇之一是使用VMWare和VirtualBox之類的虛擬機軟件,另一種選擇是使用類UNIX模擬環境,如Cygwin,GNUWin32和MSYS2。
GNUWin32軟件比較少。Cygwin軟件還算比較多,但是使用體驗糟糕。MSYS2使用體驗較好,軟件數目中等。而本文將介紹的WSL(windows下的Linux子系統)則是另一種選擇。
由於WSL默認環境是Ubuntu,軟件比較舊,所以建議通過alwsl腳本將其替換為ArchLinux環境。注意:WSL Arch Linux 已在 Microsoft Store 上可用,ArchLinux安裝可以跳過以下步驟。
2.Alwsl安裝ArchLinux環境
2.1准備工作
* 先決條件:Windows10
* 設置-->更新和安全-->針對開發人員-->啟用“開發人員模式”
* 控制面板-->程序和功能-->啟用或關閉Windows功能-->勾選“適用於Linux的Windows子系統”-->確認后重啟
2.2 安裝ArchLinux
* 獲取alwsl腳本: git clone https://github.com/alwsl/alwsl
* 執行alwsl腳本:
E:\alwsl>alwsl.bat install alwsl (info): Cleaning old files. alwsl (info): Installing base lxss fs from xenial server image. This will take A WHILE. 警告: lxrun.exe 僅用於配置適用於 Linux 的 Windows 子系統的舊分發版。 可以通過訪問 Windows 應用商店來安裝分發版: https://aka.ms/wslstore 這將在 Windows 上安裝由 Canonical 分發的 Ubuntu,其授權所遵循的條款請參見此鏈接: https://aka.ms/uowterms 正在從 Windows 應用商店下載... 69%
* 耐心等待安裝完成。注:腳本中 lxrun /install /y 這一步下載速度較慢,可能需要一個小時。
* 下載完成后開始安裝。注:腳本中的waitforexit的會等待,可以刪除 %HOMEPATH%\AppData\Local\lxss 目錄下的 temp 目錄。
* 安裝完成,輸入Y確認。
正在從 Windows 應用商店下載... 100% 正在提取文件系統,這將需要幾分鍾的時間... 安裝成功! 文檔在以下網址提供: https://aka.ms/wsldocs alwsl (info): Downloading alwsl rootfs (this might take a while)... alwsl (info): Copying rootfs to temporary extraction destination. alwsl (info): Unsquashing rootfs. Parallel unsquashfs: Using 4 processors 32080 inodes (29982 blocks) to write [==============================================================================- ] 29889/29982 99% created 24621 files created 2004 directories created 5668 symlinks created 11 devices created 0 fifos alwsl (info): Transforming... 移動了 1 個目錄。 移動了 1 個目錄。 移動了 1 個目錄。 alwsl (info): Checking installation success... alwsl (info): Renaming shortcut. alwsl (info): All done. Have fun Do you want to start bash now? [Y,N]?
* 確認后的界面。
Do you want to start bash now? [Y,N]?Y ------------------------------------------------------------------------------------------------------------------------ , _ _ _ | ___ ___ _______ ___ /#\ __ _ _ __ ___| |__ | (_)_ __ _ ___ __ | | Y | _ | | /###\ / _` | '__/ __| '_ \| | | '_ \| | | \ \/ / | |. | | 1___|. | /#####\ | (_| | | | (__| | | | | | | | | |_| |> < | |. / \ |____ |. |___ /##,-,##\ \__,_|_| \___|_| |_|_|_|_| |_|\__,_/_/\_\ | |: |: 1 |: 1 | /##( )##\ | |::.|:. |::.. . |::.. . | /#.-- --.#\ | '--- ---'-------'-------' /` `\ A simple, elegant gnu/linux distribution. | edition (v0.05) by turbo. ------------------------------------------------------------------------------------------------------------------------ root@computer:/mnt/e/alwsl#
* 開始菜單ArchLinux快捷方式修改。右鍵->更多->打開文件位置。右鍵->屬性->快捷方式->目標 內容修改為: %systemroot%\System32\bash.exe -c "cat /usr/share/issue && cd ~ && bash"
* 然后就可以開始愉快的玩耍了。
2.3 設置環境和更新軟件
* 進入 %HOMEPATH%\AppData\Local\lxss\rootfs\etc
* 修改pacman.conf:SigLevel改為Never,加入[archlinuxcn]。
# # /etc/pacman.conf # # See the pacman.conf(5) manpage for option and repository directives # # WSL: Try not to fuck around in here. [options] HoldPkg = pacman glibc Architecture = auto IgnorePkg = pacman fakeroot makepgk Color CheckSpace SigLevel = Never LocalFileSigLevel = Optional [core] Include = /etc/pacman.d/mirrorlist [extra] Include = /etc/pacman.d/mirrorlist #[community-testing] #Include = /etc/pacman.d/mirrorlist [community] Include = /etc/pacman.d/mirrorlist #[multilib] #Include = /etc/pacman.d/mirrorlist [archlinuxcn] Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$arch
* 修改pacan.d/mirrorlist為:
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch #Server = https://mirrors.kernel.org/archlinux/$repo/os/$arch #Server = http://mirror.f4st.host/archlinux/$repo/os/$arch
* 更新軟件 pacman -Syyu 。
3.安裝X Server,測試圖形環境
* 為了使用簡單的圖形界面,需要安裝X Server軟件,如VcXsrv、X-Win32、XMing、MobaXterm。有些不支持OpenGL,有些比較大,個人推薦安裝XMing。
* XMing安裝好后啟動,會自動隱藏任務欄,如圖所示:
* 安裝mesa-demos: pacman -S mesa-demos ,測試teapot:
注意:XMing 6.9不支持GLX1.3,還是用MobaXterm吧。
4.安裝Root/Cling以及常見軟件
ROOT是歐洲核子研究中心CERN開發的基於C++,可與Fortran、Python等多種語言交互的數據處理軟件。本文主要介紹root/cling作為C++的解釋器和REPL來使用。
4.1安裝Root/Cling
* 輸入 yaourt -S root ,一路點確定,需要編輯的地方一律跳過。在 ~/.bashrc 文件后面追加 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/root :
* 安裝完成后先啟動Xming,再輸入 root ,如圖所示:
* 在啟動界面之后,進入交互環境:
root@computer:/mnt/c/Users/wurui# root ------------------------------------------------------------ | Welcome to ROOT 6.10/06 http://root.cern.ch | | (c) 1995-2017, The ROOT Team | | Built for linuxx8664gcc | | From tag v6-10-06, 19 September 2017 | | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' | ------------------------------------------------------------ root [0]
* 輸入官網Root Primer的例子,包括簡單的繪圖:
4.2安裝SageMath和Spyder
* 輸入命令: pacman -S sagemath python-matplotlib spyder3
* Sage測試:
* Spyder3測試:
4.3安裝Java和JVM語言
* 輸入: pacman -S kotlin scala groovy clojure java-openjfx intellij-idea-community-edition ,選擇默認的 jdk8-openjdk 。完成后,在文件后追加: export PATH=$PATH:/usr/lib/jvm/java-8-openjdk/bin ,然后重新打開bash。
JVM語言測試:
* 輸入 idea.sh 啟動IDEA,測試JavaFX的例子,將 /usr/lib/jvm/java-8-openjdk/jre/lib/ext/jfxrt.jar 加入工程依賴的庫中:
注:安裝Eclipse后啟動失敗了,所以例子改成了IDEA。
5.Root/Cling作為C++解析器和REPL
5.1Root/Cling作為C++ REPL使用
* Root是以為Cling基礎的,預先加載了一些庫,使用起來更加友好一些。Cling沒有過多的預加載項,作為C++ REPL更純粹一些。
* Cling的例子,輸入 .help 可以看到一些指令解釋,其中: .files 表示列舉加載的文件, .class 表示列舉已經加載的類。
root@computer:~# cling ****************** CLING ****************** * Type C++ code and press enter to run it * * Type .q to exit * ******************************************* [cling]$ /*C Example*/ [cling]$ #include <stdio.h> [cling]$ #include <string.h> [cling]$ #include <stdlib.h> [cling]$ char str[] = "hello" (char [6]) "hello" [cling]$ strlen(str) (unsigned long) 5 [cling]$ puts(str) hello (int) 6 [cling]$ //C++ Example [cling]$ #include <iostream> [cling]$ #include <string> [cling]$ #include <vector> [cling]$ auto s = "hello" (const char *) "hello" [cling]$ #include <cstring> [cling]$ using namespace std; [cling]$ strlen(s) (unsigned long) 5 [cling]$ string(s).size() (unsigned long) 5 [cling]$ vector<string> ss{"hello","world"} (std::vector<std::string> &) { "hello", "world" } [cling]$ ss.size() (unsigned long) 2
* Root的例子,輸入 root -l ,其中 -l 表示不顯示logo。與Cling相比,Root更強大一點,有一定的語法高亮和自動補全。這可能是由於Root已經預先加載了一些庫,並且使用了 using namespace std; 。
5.2Root/Cling作為C++解析器,以腳本形式運行C++
* 以SDL和OpenGL作為一個例子:

1 // OpenGL headers 2 // #define GLEW_STATIC 3 #include <GL/glew.h> 4 #include <GL/glu.h> 5 #include <GL/gl.h> 6 7 // SDL headers 8 #include <SDL2/SDL_main.h> 9 #include <SDL2/SDL.h> 10 #include <SDL2/SDL_opengl.h> 11 12 #ifdef __TINYC__ 13 typedef int bool; 14 #define false 0 15 #define true 1 16 #endif 17 18 bool quit; 19 20 SDL_Window* window; 21 SDL_GLContext glContext; 22 SDL_Event sdlEvent; 23 24 int main(int argc, char *argv[]) 25 { 26 quit = false; 27 28 //Use OpenGL 3.1 core 29 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); 30 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); 31 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); 32 33 // Initialize video subsystem 34 if(SDL_Init(SDL_INIT_VIDEO) < 0) 35 { 36 // Display error message 37 printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); 38 return false; 39 } 40 else 41 { 42 // Create window 43 window = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN ); 44 if( window == NULL ) 45 { 46 // Display error message 47 printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); 48 return false; 49 } 50 else 51 { 52 // Create OpenGL context 53 glContext = SDL_GL_CreateContext(window); 54 55 if( glContext == NULL ) 56 { 57 // Display error message 58 printf( "OpenGL context could not be created! SDL Error: %s\n", SDL_GetError() ); 59 return false; 60 } 61 else 62 { 63 // Initialize glew 64 glewInit(); 65 } 66 } 67 } 68 69 // Game loop 70 while (!quit) 71 { 72 while(SDL_PollEvent(&sdlEvent) != 0) 73 { 74 // Esc button is pressed 75 if(sdlEvent.type == SDL_QUIT) 76 { 77 quit = true; 78 } 79 } 80 81 // Set background color as cornflower blue 82 glClearColor(0.39f, 0.58f, 0.93f, 1.f); 83 // Clear color buffer 84 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 85 // Update window with OpenGL rendering 86 SDL_GL_SwapWindow(window); 87 } 88 89 //Destroy window 90 SDL_DestroyWindow(window); 91 window = NULL; 92 93 //Quit SDL subsystems 94 SDL_Quit(); 95 96 return 0; 97 }
* Root運行這個例子的命令行:
root -e '.L /usr/lib/libSDL2.so' -e '.L /usr/lib/libGL.so' -e '.L /usr/lib/libGLEW.so' -e 'int argc=0;char **argv=NULL' -e '.x main.c(argc,argv)' -q -l -b
* Cling運行OpenGL會有問題,所以給了一個Qt的例子cling_qt.cpp:

1 #include <QtCore/QtCore> 2 #include <iostream> 3 using namespace std; 4 void cling_qt() 5 { 6 QString s = "hello"; 7 qDebug()<<s.size(); 8 cout<<s.toStdString()<<endl; 9 }
* Cling運行這個例子的命令行:
cling -I/usr/include/qt -lQt5Core -fPIC cling_qt.cpp
注:所以的例子都可以在我的GitHub上找到。
5.3Root/Cling和Jupyter Notebook (Python)
* 檢查cling-jupyter是否安裝好了:
[wurui@computer wurui]$ pacman -Qs cling-jupyter local/cling-jupyter-git r4078.00a59858-1 Interactive C++ interpreter built on the top of LLVM and Clang libraries.
* 輸入 jupyter-notebook 打開notebook,可以看到C++的選擇。
* 選擇C++14,輸入代碼進行測試:
* Root可以輸入 root --notebook 開啟notebook,或者從Python Jupyter Notebook中導入【注:可以參考官網的例子】。
6.結束語
一直對C和C++的解釋器比較感興趣,C語言的解釋器一直用的是TCC,C++的解釋器偶有所聞,卻未嘗試。最近在Manjaro下成功運行了Root/Cling,覺得使用體驗還算可以。
可惜,Root/Cling在Windows沒有可用的二進制版本。准確來說:Root5有一個Windows版本,比較舊了。Cling也可以用VS編譯成功,還有Anaconda的版本,但是兼容性問題比較嚴重,基本不能用。
此外,一直有聽到WSL的消息,以前一直覺得WSL不成熟,所以並未嘗試。最近,嘗試在WSL下安裝了Root/Cling,發現居然可以用。特整理成文,記錄於此,作為一份備忘錄。