{"id":31635,"date":"2023-01-31T00:31:43","date_gmt":"2023-01-31T00:31:43","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cppblog\/?p=31635"},"modified":"2024-04-03T09:34:58","modified_gmt":"2024-04-03T09:34:58","slug":"vs-code-c-extension-january-update-create-definitions-and-declarations","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/vs-code-c-extension-january-update-create-definitions-and-declarations\/","title":{"rendered":"VS Code C++ Extension January Update: Create Definitions and Declarations"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">Starting with the <a href=\"https:\/\/github.com\/microsoft\/vscode-cpptools\/releases\">1.13.6 version<\/a> of the C++ Extension in VS Code, we are happy to share a <a href=\"https:\/\/github.com\/microsoft\/vscode-cpptools\/issues\/664\">much requested<\/a> feature: Auto creation of definitions or declarations for functions! You can now quickly create a declaration in a header file for a function you only have a definition for, or vice versa. These generated definitions and declarations already include all function arguments, so no need for constant copying, pasting, and double checking. <\/span><\/p>\n<p><span style=\"font-size: 12pt;\">Let\u2019s see it in action:\u00a0\u00a0<\/span><a style=\"color: #006f94; text-decoration-line: underline; outline-width: 0px;\" href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/FirstDefthenDecl-1.gif\"><img decoding=\"async\" class=\"aligncenter wp-image-31682\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/FirstDefthenDecl-1.gif\" alt=\"From a header file, a definition is clicked using a code action, which creates the declaration in a separate file. In that new file, a definition is added, which then is clicked via code action to invoke a declaration being added to the header file. \" width=\"732\" height=\"526\" \/><\/a><\/p>\n<h3><\/h3>\n<h3><\/h3>\n<h3><b><span data-contrast=\"auto\">How can I create a Definition or Declaration?\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\">You can either:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<ul>\n<li data-leveltext=\"-\" data-font=\"Calibri\" data-listid=\"3\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Calibri&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;-&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"none\">Click on your class function definition, then the Code Action (lightbulb icon) on the left. This will open a dropdown where you can select \u201cCreate a Declaration\u201d. For creating a definition, it is the same process, just click on the function declaration, then select the Code Action for \u201cCreate a Definition\u201d for your function. <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/InvokeCodeAction.png\"><img decoding=\"async\" class=\"aligncenter wp-image-31665\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/InvokeCodeAction-300x134.png\" alt=\"Clicking on the function, a lightbulb will appear on the left, where you can select the Option to &quot;Create a Definition&quot;\/Declaration, depending on what you need. \" width=\"423\" height=\"189\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/InvokeCodeAction-300x134.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/InvokeCodeAction.png 416w\" sizes=\"(max-width: 423px) 100vw, 423px\" \/><\/a><\/span><\/li>\n<\/ul>\n<ul>\n<li data-leveltext=\"-\" data-font=\"Calibri\" data-listid=\"2\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Calibri&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;-&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><span data-contrast=\"none\">Right click a function\u2019s declaration or definition and select the \u201cCreate Definition\/Declaration\u201d from the context menu. A definition or declaration will be created depending on what is needed.\u00a0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\"><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/Dropdownselectcreatedefdecl.png\"><img decoding=\"async\" class=\"aligncenter wp-image-31664\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/Dropdownselectcreatedefdecl-300x166.png\" alt=\"Right click a function\u2019s declaration or definition and select the \u201cCreate Definition\/Declaration\u201d from the context menu. A definition or declaration will be created depending on what is needed.\" width=\"383\" height=\"212\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/Dropdownselectcreatedefdecl-300x166.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/Dropdownselectcreatedefdecl.png 410w\" sizes=\"(max-width: 383px) 100vw, 383px\" \/><\/a> <\/span><span style=\"color: #006f94;\">\n<\/span><\/li>\n<\/ul>\n<ul>\n<li data-leveltext=\"-\" data-font=\"Calibri\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Calibri&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;-&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"none\">Select the function, then use the Command Palette (Ctrl + Shift + P) and type in the command <\/span><b><span data-contrast=\"none\">Create Declaration\/Definition.\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\n<\/ul>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\"><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/commandpalletecreatedefdecl.png\"><img decoding=\"async\" class=\"aligncenter wp-image-31663\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/commandpalletecreatedefdecl-300x34.png\" alt=\"Select the function, then use the Command Palette (Ctrl + Shift + P) and type in the command Create Declaration\/Definition.\u00a0\u00a0\" width=\"606\" height=\"69\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/commandpalletecreatedefdecl-300x34.png 300w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/commandpalletecreatedefdecl.png 731w\" sizes=\"(max-width: 606px) 100vw, 606px\" \/><\/a><\/span><\/p>\n<h3><\/h3>\n<p>&nbsp;<\/p>\n<h3><b><span data-contrast=\"none\">How do I know where the definition or declaration will be created? <\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\">Definitions and declarations will be created based on the patterns you have already established in your code. For example, if you previously had added definitions and declarations in the same file, we will also add any new ones to that same file. Note that we will maintain function order automatically, so if your definition is between the functions Addition() and Subtraction() in Math.cpp and ask for a declaration, we will create the new declaration between the declarations of Addition() and Subtraction(). <\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\"><a style=\"color: #006f94; text-decoration-line: underline; outline-width: 0px;\" href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/SameFileMyCalculator-1.gif\"><img decoding=\"async\" class=\"aligncenter wp-image-31683 size-full\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/SameFileMyCalculator-1.gif\" alt=\"This shows that when the class is already defined in the same file, invoking the create declaration command will declare a function in that same file. \" width=\"799\" height=\"492\" \/><\/a><\/span><\/p>\n<p><span data-contrast=\"auto\">Otherwise, if you have defined declarations or definitions in a different file from your source file, we will follow that convention.\u00a0 In the common case of having a header and a source file with matching names, we will identify the other file, even if the header file has not been included in the source file. In this case, once the declaration or definition has been added, w<\/span><span data-contrast=\"none\">e will automatically include your header file to your source file for you. <\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"none\">If you do not have any precedent of where you create definitions or declarations, we will create a header or source file with a matching name to your current file for you. <\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<h3><b><span data-contrast=\"none\">How does this work for namespaces and templates? <\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"none\">The functionality is the same for namespaces as classes. For templates, if a function template is declared in a header file, the definition of that function template will be created in the same header file, as shown below. This also applies for non-template member functions of class templates. <\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:2,&quot;335551620&quot;:2,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:2,&quot;335551620&quot;:2,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\"><span style=\"color: #006f94;\"><u><a style=\"color: #006f94; outline-width: 0px;\" href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/TemplatesMyCaclculator-1.gif\"><img decoding=\"async\" class=\"aligncenter wp-image-31681 size-full\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2023\/01\/TemplatesMyCaclculator-1.gif\" alt=\"For templates, template functions will be defined in the same file when the feature is called, even if you have already defined\/declared other functions in other files. \" width=\"866\" height=\"587\" \/><\/a><\/u><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p aria-level=\"1\"><b><span data-contrast=\"none\">What do you think?<\/span><\/b><span data-ccp-props=\"{&quot;134245418&quot;:true,&quot;134245529&quot;:true,&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-vscode.cpptools\"><span data-contrast=\"none\">Download the C++ extension for Visual Studio Code <\/span><\/a><span data-contrast=\"none\">\u202ftoday, give it a try, and let us know what you think.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"none\">If you have any questions, feel free to start a discussion in <\/span><a href=\"https:\/\/github.com\/microsoft\/vscode-cpptools\/discussions\"><span data-contrast=\"none\">our Github repository<\/span><\/a><span data-contrast=\"none\">, otherwise if you run into any issues please report them in the \u202f<\/span><a href=\"https:\/\/github.com\/Microsoft\/vscode-cpptools\/issues\"><span data-contrast=\"none\">issues section<\/span><\/a><span data-contrast=\"none\">. We can be reached via the comments below or in email at\u202f <\/span><a href=\"mailto:visualcpp@microsoft.com\"><span data-contrast=\"none\">visualcpp@microsoft.com<\/span><\/a><span data-contrast=\"none\">. You can also find our team on Twitter at\u202f<\/span><a href=\"https:\/\/twitter.com\/visualc\"><span data-contrast=\"none\">@<\/span><\/a><a href=\"https:\/\/twitter.com\/visualc\"><span data-contrast=\"none\">VisualC<\/span><\/a><span data-contrast=\"none\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Starting with the 1.13.6 version of the C++ Extension in VS Code, we are happy to share a much requested feature: Auto creation of definitions or declarations for functions! You can now quickly create a declaration in a header file for a function you only have a definition for, or vice versa. These generated definitions [&hellip;]<\/p>\n","protected":false},"author":101195,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-31635","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus"],"acf":[],"blog_post_summary":"<p>Starting with the 1.13.6 version of the C++ Extension in VS Code, we are happy to share a much requested feature: Auto creation of definitions or declarations for functions! You can now quickly create a declaration in a header file for a function you only have a definition for, or vice versa. These generated definitions [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/31635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/101195"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=31635"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/31635\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=31635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=31635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=31635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}