{"id":563,"date":"2019-11-22T23:09:47","date_gmt":"2019-11-22T23:09:47","guid":{"rendered":"http:\/\/blog.davcloud.top\/?p=563"},"modified":"2020-06-11T08:23:30","modified_gmt":"2020-06-11T08:23:30","slug":"compile-and-debug-c-project-with-visual-studio-code-on-ubuntu","status":"publish","type":"post","link":"https:\/\/blog.davcloud.top\/?p=563","title":{"rendered":"Compile and debug C++ project with Visual Studio Code on Ubuntu"},"content":{"rendered":"\n<h2>Abstract<\/h2>\n\n\n\n<p>This article aims to record how to compile and debug C++ project with Visual Studio Code on Ubuntu.<\/p>\n\n\n\n<h2>Step 1. Install gcc and g++ compiler<\/h2>\n\n\n\n<p>To compile and debug for the C++ project on Ubuntu, gcc and g++ compiler are required. With the command below to install gcc and g++ with the latest version:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install gcc g++<\/code><\/pre>\n\n\n\n<p>And after the installation, check the version to confirm whether it can work or not:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gcc --version\ng++ --version<\/code><\/pre>\n\n\n\n<h2>Step 2. Install Visual Studio Code<\/h2>\n\n\n\n<p>Download Visual Studio Code from the official website: <a href=\"https:\/\/code.visualstudio.com\/\">https:\/\/code.visualstudio.com\/<\/a>.<\/p>\n\n\n\n<p>Install the deb package with the command below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo dpkg -i &lt;package name><\/code><\/pre>\n\n\n\n<h2>Step 3. Install C\/C++ extension in VSCode<\/h2>\n\n\n\n<p>To support C++ language environment, C\/C++ extension is required to install to the VSCode editor. Click &#8220;Extensions&#8221; option in the left bar and search &#8220;C\/C++&#8221; to install the extension.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"710\" height=\"174\" src=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2019\/11\/c-extension.png\" alt=\"\" class=\"wp-image-594\" srcset=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2019\/11\/c-extension.png 710w, https:\/\/blog.davcloud.top\/wp-content\/uploads\/2019\/11\/c-extension-300x74.png 300w\" sizes=\"(max-width: 710px) 100vw, 710px\" \/><\/figure><\/div>\n\n\n\n<h2>Step 4. Create a project work folder and open in VSCode<\/h2>\n\n\n\n<p>Assume that the project is on the desktop with a folder named &#8220;hello&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd ~\/Desktop\nmkdir hello\ncd hello\n# Open the current dir in VSCode\ncode .<\/code><\/pre>\n\n\n\n<h2>Step 5. Create a cpp file in the work dir<\/h2>\n\n\n\n<p>Create &#8220;hello.cpp&#8221; file in the &#8220;explorer&#8221; option of the left bar of the interface as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video controls src=\"https:\/\/blog.davcloud.top\/wp-content\/uploads\/2019\/11\/create-cpp-file.mp4\"><\/video><\/figure>\n\n\n\n<h2>Step 6. Configure default build task<\/h2>\n\n\n\n<p>Press &#8220;Ctrl+Shift+P&#8221; and choose the option &#8220;Configure default build task&#8221;, then choose the option &#8220;g++ build active file&#8221;, a default build task json &#8220;task.json&#8221; will be generated, which can be seen in the left bar in &#8220;Explorer&#8221; option.<\/p>\n\n\n\n<h2>Step 7. Configure default launch configuration<\/h2>\n\n\n\n<p>Press &#8220;F5&#8221; on the keyboard and choose the option &#8220;C++(GDB\/LLDB)&#8221; and choose &#8220;Default configuration&#8221;.<\/p>\n\n\n\n<p>Edit the generated launch.json file, replace the parameter &#8220;program&#8221; from the below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"program\": \"enter program name, for example ${workspaceFolder}\/a.out\"<\/code><\/pre>\n\n\n\n<p>to the new one:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"program\": \"${workspaceFolder}\/${fileBasenameNoExtension}\"<\/code><\/pre>\n\n\n\n<p>and then save the launch.json file.<\/p>\n\n\n\n<h2>Step 8. Compile and debug<\/h2>\n\n\n\n<p>Write codes in the file &#8220;hello.cpp&#8221;, and press &#8220;Ctrl+Shift+B&#8221; to compile and build the project, press &#8220;F5&#8221; to debug the project.<\/p>\n\n\n\n<p>It is OK now to compile and debug the C++ project with Visual Studio Code on Ubuntu.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Abstract This article aims to record how to compile and debug C++ project with Visual Studio Code on Ubuntu. Step 1. Install gcc and g++ compiler To compile and debug for the C++ project on Ubuntu, gcc and g++ compiler are required. With the command below to install gcc and g++ with the latest version: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,13,542],"tags":[121,25,122],"_links":{"self":[{"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/posts\/563"}],"collection":[{"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=563"}],"version-history":[{"count":35,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/posts\/563\/revisions"}],"predecessor-version":[{"id":1155,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=\/wp\/v2\/posts\/563\/revisions\/1155"}],"wp:attachment":[{"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.davcloud.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}