{"id":490,"date":"2019-02-24T14:53:40","date_gmt":"2019-02-24T22:53:40","guid":{"rendered":"https:\/\/doubleecpu.com\/?page_id=490"},"modified":"2021-07-28T00:14:34","modified_gmt":"2021-07-28T07:14:34","slug":"cpp_setup","status":"publish","type":"page","link":"https:\/\/doubleecpu.com\/index.php\/shop\/cpp_setup\/","title":{"rendered":"C++ Setup"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Setting up visual studio <a href=\"https:\/\/code.visualstudio.com\/docs\/languages\/cpp\">MSDOCs<\/a><\/h2>\n\n\n\n<p>If using Visual Studio Code the C++ extension must be installed and a compiler must be installed on Windows OS. If Compiling For WSL Linux must have a compiler installed <\/p>\n\n\n\n<p>MinGWx64 on Windows 10 via&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.msys2.org\/\" target=\"_blank\">MSYS2<\/a>,, <br>CLang on MacOs can be installed using XCODE<br>GNU compiler on Ubuntu\/Debian WSL, <br>can be installed with command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install build-essential gdb<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Creating C++ Work Folder<\/h2>\n\n\n\n<p>C++ Hello World<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir HelloWorld\ncd HelloWorld\ncode .<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"> Build Instructions <\/h2>\n\n\n\n<p>The tasks.json  file list the instructions for VS Code to build(compile) the program into an executable file. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{\n    \"tasks\": [\n        {\n            \"type\": \"cppbuild\",\n            \"label\": \"C\/C++: g++.exe build active file\",\n            \"command\": \"C:\\\\MinGW\\\\bin\\\\g++.exe\",\n            \"args\": [\n                \"-g\",\n                \"${file}\",\n                \"-o\",\n                \"${fileDirname}\\\\${fileBasenameNoExtension}.exe\"\n            ],\n            \"options\": {\n                \"cwd\": \"${fileDirname}\"\n            },\n            \"problemMatcher\": [\n                \"$gcc\"\n            ],\n            \"group\": {\n                \"kind\": \"build\",\n                \"isDefault\": true\n            },\n            \"detail\": \"Task generated by Debugger.\"\n        }\n    ],\n    \"version\": \"2.0.0\"\n}<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>type<\/li><li>label value is what you will see in the tasks list; you can name this whatever you like<\/li><li>command setting specifies the program to run; in this case that is g++. <\/li><li>args array specifies the command-line arguments that will be passed to g++. These arguments must be specified in the order expected by the compiler. This task tells g++ to take the active file (${file}), compile it, and create an executable file in the current directory (${fileDirname}) with the same name as the active file but with the .exe extension (${fileBasenameNoExtension}.exe), resulting in helloworld.exe for our example.<\/li><li>options<\/li><li>problemMatcher<\/li><li>group<ul><li>kind<\/li><li>&#8220;isDefault&#8221;: true value in the group object specifies that this task will be run when you press Ctrl+Shift+B. This property is for convenience only; if you set it to false, you can still run it from the Terminal menu with Tasks: Run Build Task.<\/li><\/ul><\/li><li>detail<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"_modifying-tasksjson\">Modifying tasks.json#<\/h3>\n\n\n\n<p>You can modify your&nbsp;<code>tasks.json<\/code>&nbsp;to build multiple C++ files by using an argument like&nbsp;<code>\"${workspaceFolder}\\\\*.cpp\"<\/code>&nbsp;instead of&nbsp;<code>${file}<\/code>. This will build all&nbsp;<code>.cpp<\/code>&nbsp;files in your current folder. You can also modify the output filename by replacing&nbsp;<code>\"${fileDirname}\\\\${fileBasenameNoExtension}.exe\"<\/code>&nbsp;with a hard-coded filename (for example&nbsp;<code>\"${workspaceFolder}\\\\myProgram.exe\"<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Add Hello World source code#<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">#include &lt;iostream&gt;\n\nusing namespace std;\n\nint main()\n{\n    cout &lt;&lt; \"Hello World\" &lt;&lt; endl;\n}<\/pre>\n\n\n\n<p>Save the File then Select the&nbsp;<strong>Terminal<\/strong>&nbsp;&gt;&nbsp;<strong>Run Build Task<\/strong>&nbsp;command (Ctrl+Shift+B) <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"946\" height=\"289\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2021\/07\/image-1.png\" alt=\"\" class=\"wp-image-2444\"\/><\/figure>\n\n\n\n<p>An executable file, HelloWorld.exe will be created and can be run to verify all steps are functioning. <\/p>\n\n\n\n<p>Modify the HelloWorld.cpp file to a more elaborate Hello World <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;string&gt;\n\nusing namespace std;\n\nint main()\n{\n    vector&lt;string&gt; msg {\"Hello\", \"C++\", \"World\", \"from\", \"VS Code\", \"and the C++ extension!\"};\n\n    for (const string&amp; word : msg)\n    {\n        cout &lt;&lt; word &lt;&lt; \" \";\n    }\n    cout &lt;&lt; endl;\n}<\/pre>\n\n\n\n<p>Running the code . command will generate files:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>tasks.json<\/code>&nbsp;(build instructions)<\/li><li><code>launch.json<\/code>&nbsp;(debugger settings)<\/li><li><code>c_cpp_properties.json<\/code>&nbsp;(compiler path and IntelliSense settings)<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Setup Debugging<\/h2>\n\n\n\n<p>The instructions for debugging are listed in launch.json file, pressing F5 will launch the listed instructions. The file is generated when Select <strong>Run<\/strong>&nbsp;&gt;&nbsp;<strong>Add Configuration&#8230;<\/strong>&nbsp;choose&nbsp;<strong>C++ (GDB\/LLDB)<\/strong> and on the dropdown <strong>g++.exe build and debug active file<\/strong> is selected from the predefined configurations<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{\n    \"version\": \"0.2.0\",\n    \"configurations\": [\n        {\n            \"name\": \"g++.exe - Build and debug active file\",\n            \"type\": \"cppdbg\",\n            \"request\": \"launch\",\n            \"program\": \"${fileDirname}\\\\${fileBasenameNoExtension}.exe\",\n            \"args\": [],\n            \"stopAtEntry\": false,\n            \"cwd\": \"${fileDirname}\",\n            \"environment\": [],\n            \"externalConsole\": false,\n            \"MIMode\": \"gdb\",\n            \"miDebuggerPath\": \"C:\\\\MinGW\\\\bin\\\\gdb.exe\",\n            \"setupCommands\": [\n                {\n                    \"description\": \"Enable pretty-printing for gdb\",\n                    \"text\": \"-enable-pretty-printing\",\n                    \"ignoreFailures\": true\n                }\n            ],\n            \"preLaunchTask\": \"C\/C++: g++.exe build active file\"\n        }\n    ]\n}<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>program setting specifies the program you want to debug. set to the active file folder ${fileDirname} and active filename with the .exe extension ${fileBasenameNoExtension}.exe, which if helloworld.cpp is the active file will be helloworld.exe.<\/li><li>By default, the C++ extension won&#8217;t add any breakpoints to your source code and the stopAtEntry value is set to false.<\/li><li>Change the stopAtEntry value to true to cause the debugger to stop on the main method when you start debugging.<\/li><li>The&nbsp;<code>preLaunchTask<\/code>&nbsp;setting is used to specify task to be executed before launch. Make sure it is consistent with the&nbsp;<code>tasks.json<\/code>&nbsp;file&nbsp;<code>label<\/code>&nbsp;setting<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Start Debugging<\/h2>\n\n\n\n<p>To start debugging press F5 or from the main menu choose Run > Start Debugging.<br>The Integrated Terminal will appear at the bottom of the source code editor.<br>The editor highlights the first statement in the main method, a breakpoint that the C++ extension automatically sets <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step Through Code<\/h3>\n\n\n\n<p>The buttons include <br>Continue which runs until next breakpoint or completion<br>Step Over moves to the next line if there is no breakpoint in the code <br>Step Into moves to the next line of code <br>Step Out processes all code up to the next line of code outside of the function<br>Restart reloads code and starts a new debugging session<br>Stop exits the Debugging environment and code execution<br>Disconnect <br>breakpoints appear next to the line numbers.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2021\/07\/image-2.png\" alt=\"\" class=\"wp-image-2449\" width=\"733\" height=\"282\"\/><figcaption> To watch a variable, which is a way to monitor its value, highlight then right click and select Add To Watch. The variable and it&#8217;s current value will be listed under Watch<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1488\" height=\"502\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2021\/07\/image-3.png\" alt=\"\" class=\"wp-image-2450\"\/><\/figure>\n\n\n\n<p>Creating Classes and using Classes in cpp files<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Setting up visual studio MSDOCs If using Visual Studio Code the C++ extension must be installed and a compiler must be installed on Windows OS. If Compiling For WSL Linux must have a compiler installed MinGWx64 on Windows 10 via&nbsp;MSYS2,, CLang on MacOs can be installed using XCODEGNU compiler on Ubuntu\/Debian WSL, can be installed &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/doubleecpu.com\/index.php\/shop\/cpp_setup\/\" class=\"more-link\">Read more<span class=\"screen-reader-text\"> &#8220;C++ Setup&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":22,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-490","page","type-page","status-publish","hentry"],"featured_media_urls":[],"_links":{"self":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/490","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/comments?post=490"}],"version-history":[{"count":3,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/490\/revisions"}],"predecessor-version":[{"id":2451,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/490\/revisions\/2451"}],"up":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/22"}],"wp:attachment":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/media?parent=490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}