標籤:coreclr 開源 .net helloworld
本文介紹利用編譯出來的coreclr來執行C#編寫的Hello coreclr程式。我的機器環境是Win 7 x64 En,VS2013.
(1)編譯test程式,根據開發guide,執行build指令如下:
<repo_root>\tests\buildtest.cmd x64 release clean
在我的環境中,編譯成功了,沒有出錯誤,開發guide中說明了如果出錯誤的解決辦法。
同樣編譯完成後,會產生一個VS工程檔案。為何要編譯test,原因是我這邊的Win7下,如果直接運行HelloCoreclr.exe程式的話,會提示找不到dll,而這些dll利用test編譯後,
能夠得到。
(2)設定環境變數CORE_ROOT
CORE_ROOT的路徑指向<..\coreclr\binaries\Product\x64\debug>下
(3)編譯helloworld程式
自己用C#寫一個hello world程式,編譯後產生exe檔案,並將該檔案拷貝粘貼到此路徑下
<..\coreclr\binaries\Product\x64\debug>
(4)可以直接用命令好啟動corerun.exe,運行helloworld程式。如為我建立的簡單的hello coreclr!程式,由corerun.exe啟動後的執行結果。此時已經脫離的預設的.net運行庫,而是用開源的coreclr運行庫運行哦。
(5)api-ms-win-core-winrt-string-l1-1-0.dll找不到問題
實際上在第四步執行時,並不順利,在win7下會出現dll找不到的錯誤,錯誤框如下。解決辦法是將第一步中test編譯出來的檔案夾<..\coreclr\tests\src\packages\Microsoft.DotNet.CoreCLR.TestDependencies.1.0.0-prerelease\lib\aspnetcore50>
的dll全部拷貝一份到<..\coreclr\binaries\Product\x64\debug>下面,就ok了。這個錯誤很常見,github上很多人都遇到了這個錯誤。
(6)coreclr的debug
開發guide提供了如何對coreclr進行debug的方法,原文如下,主要需要做的就是將coreclr.sln工程中的INSTALL項目屬性進行設定。根據說明,設定情況
Debugging CoreCLR
- Perform a build of the repo.
- Open \binaries\Cmake\CoreCLR.sln in VS.
- Right click the INSTALL project and choose ‘Set as StartUp Project’
- Bring up the properties page for the INSTALL project
- Select Configuration Properties->Debugging from the left side tree control
- Set Command=
$(SolutionDir)..\product\$(Platform)\$(Configuration)\corerun.exe
- This points to the folder where the built runtime binaries are present.
- Set Command Arguments=
<managed app you wish to run> (e.g. HelloWorld.exe)
- Set Working Directory=
$(SolutionDir)..\product\$(Platform)\$(Configuration)
- This points to the folder containing CoreCLR binaries.
- Press F11 to start debugging at wmain in corerun (or set a breakpoint in source and press F5 to run to it)
- As an example, set a breakpoint for the EEStartup function in ceemain.cpp to break into CoreCLR startup.
Steps 1-8 only need to be done once, and then (9) can be repeated whenever you want to start debugging. The above can be done with Visual Studio 2013.
按F11執行後,進入程式入口如下,然後就可以單步調試了,(PS:我這邊環境變數沒有設定也是單步調試的時候發現的,因為單步調試啟動自己的Hello World程式時,會提示coreclr.dll載入失敗,原因就是沒有設定環境變數。)
綜上,windows下以開源coreclr運行hello world程式就實現了。想瞭解coreclr底層的東西,單步跟著代碼走就行了。
.Net開源之corefx、coreclr初探(三)