.Net開源之corefx、coreclr初探(三)

來源:互聯網
上載者:User

標籤: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

  1. Perform a build of the repo.
  2. Open \binaries\Cmake\CoreCLR.sln in VS.
  3. Right click the INSTALL project and choose ‘Set as StartUp Project’
  4. Bring up the properties page for the INSTALL project
  5. Select Configuration Properties->Debugging from the left side tree control
  6. Set Command=$(SolutionDir)..\product\$(Platform)\$(Configuration)\corerun.exe
    1. This points to the folder where the built runtime binaries are present.
  7. Set Command Arguments=<managed app you wish to run> (e.g. HelloWorld.exe)
  8. Set Working Directory=$(SolutionDir)..\product\$(Platform)\$(Configuration)
    1. This points to the folder containing CoreCLR binaries.
  9. Press F11 to start debugging at wmain in corerun (or set a breakpoint in source and press F5 to run to it)
    1. 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初探(三)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.