How to create, compile and run a project in Visual Studio for Mac.
- Build a C# Hello World application with.NET Core in Visual Studio 2017.; 3 minutes to read Contributors. In this article. This topic provides a step-by-step introduction to building, debugging, and publishing a simple.NET Core console application using C# in Visual Studio 2017.
- You can use Visual C++ in the Visual Studio integrated development environment (IDE) to create Standard C++ programs. By following the steps in this walkthrough, you can create a project, add a new file to the project, modify the file to add C++ code, and then compile and run the program by using Visual Studio.
Over the last few months, we have heard a lot of requests with respect to adding capability to Visual Studio Code to allow developers to build their C/C++ application. The task extensibility in Visual Studio Code exists to automate tasks like building, packaging, testing and deploying. This post is going to demonstrate how using task extensibility in Visual Studio Code you can call compilers, build systems and other external tasks through the help of the following sections:
Installing C/C++ build tools
In order to build your C++ code you need to make sure you have C/C++ build tools (compilers, linkers and build systems) installed on your box. If you can already build outside Visual Studio Code you already have these tools setup, so you can move on to the next section.
To obtain your set of C/C++ compilers on Windows you can grab the Visual C++ build tools SKU. By default these tools are installed at ‘C:Program Files (x86)Microsoft Visual C++ Build Tools’. You only need to do this if you don’t have Visual studio installed. If you already have Visual Studio installed, you have everything you need already.
If you are on a Linux platform which supports apt-get you can run the following commands to make sure you grab the right set of tools for building your C/C++ code.
2 4 | xcodebuild-find gcc xcodebuild-find clang |
Creating a simple Visual Studio Code task for building C/C++ code
To follow this specific section you can go ahead and download this helloworld C++ source folder. If you run into any issues you can always cheat and download the same C++ source folder with a task pre-configured.
If you are just picking up C++ and want to understand different components involved in performing a simple build you can review this guide.
In Visual Studio Code tasks are defined for a workspace and Visual Studio Code comes pre-installed with a list of common task runners. In the command palette (Ctrl+Shift+P (Win, Linux), ⇧⌘P (Mac)) you can type tasks and look at all the various task related commands.
On executing the ‘Configure Task Runner’ option from the command palette you will see a list of pre-installed tasks as shown below, in the future we will grow the list of task runners for popular build systems but for now go ahead and pick up the others template from this list.
This will create a tasks.json file in your .vscode folder with the following content:
2 4 6 8 | // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format 'command':'echo', 'args':['Hello World'], } |
Setting it up for Windows
The easiest way to setup Visual Studio Code on Windows for C/C++ building is to create a batch file called ‘build.bat’ with the following commands:
2 4 | call'C:Program Files (x86)Microsoft Visual Studio 14.0VCvcvarsall.bat'x64 set linkerflags=/OUT:hello.exe cl.exe%compilerflags%helloworld.cpp/link%linkerflags% |
Please note that the location of vcvarsall.bat file which sets up the right environment for building could be different on your machine. Also if you are using the Visual C++ build SKU, you will need to call the following command instead:
call“C:Program Files(x86)Microsoft VisualC++Build Toolsvcbuildtools.bat”x64 |
Once the build script is ready you can then modify your tasks.json to directly call your batch file on Windows by making the following changes to the automatically generated tasks.json file.
2 4 6 8 | // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format 'windows':{ 'isShellCommand':true, } |
Initiate a build by bringing up the command palette again and executing the ‘Run Build Task’ command.
This should initiate the build for our C++ application and you should be able to monitor the build progress in the output window.
Now even though this is a Windows specific example you should be able to re-use the same series of steps to call a build script on other platforms as well.
Calling Clang and GCC from Visual Studio Code task for building C/C++ code
Alright let us now see how we can achieve building our C/C++ application without calling an external batch file using some popular toolsets like GCC and Clang directly without a build system in play.
To follow this specific section you can go ahead and download this helloworld C++ source folder. If you run into any issues you can always cheat and download the same C++ source folder with a task pre-configured.
Tasks.json allow you to specify qualifiers like the one below for ‘OS X’. These qualifiers similar will allow you create specific build configurations for your different build targets or as shown in this case for different platforms.
2 4 6 8 10 12 14 16 18 20 22 24 | 'command':'clang++', '-Wall', '-v' 'isShellCommand':true, 'problemMatcher':{ 'fileLocation':[ '${workspaceRoot}' 'pattern':{ 'regexp':'^(.*):(d+):(d+):s+(warning|error):s+(.*)$', 'line':2, 'severity':4, } |
Another thing to highlight in this snippet is the ‘problemMatcher’ section. Visual Studio Code ships with some of the most common problem matchers out of the box but many compilers and other tools define their own style of errors and warnings. Need not worry you can create your own custom problem matcher as well with Visual Studio Code. This site which helps test out regex online might also come in handy.
The pattern matcher here will work well for Clang and GCC toolsets so just go ahead and use them. The figure below shows them in effect when you initiate the show problems command in Visual Studio Code (Cntrl+Shift+M (Win, Linux), ⇧⌘M (Mac)).
Calling Makefiles using Visual Studio Code task extensibility
Similar to the manner how you configure tasks.json to call the compiler, you can do the same for makefiles. Take a look at the sample tasks.json below, the one new concept in this tasks.json file is the nesting of tasks. Both ‘hello’ and ‘clean’ are tasks in the makefile where as ‘compile w/o makefile’ is a separate task but this example should show you how you can setup tasks.json in cases where there are
multiple build systems at play. You can find the entire sample here.
Note this is an OSX, Linux specific example but to obtain the same behavior on Windows you can replace ‘bash’ with ‘cmd’ and ‘args’ with ‘/C’.
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 | // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format 'osx':{ 'args':['-c'], 'showOutput':'always', 'options':{ }, { 'args':[ ], }, 'taskName':'clean', 'make clean' }, 'taskName':'compile w/o makefile', 'clang++ -Wall -g helloworld.cpp -o hello' 'echoCommand':true ] } |
Two more things to mention here is that whichever task you associate the ‘isBuildCommand’ with becomes your default build task in Visual Studio Code. In this case that would be the ‘hello’ task. If you would like to run the other tasks bring up the command palette and choose ‘Run Task’ option.
Then choose the individual task to run e.g. ‘clean’ task. Alternatively, you can also wire the build task as a different key binding. For doing so bring up File -> Preferences -> Keyboard shortcuts and add the following key binding to your task. Bindings currently only exist for build and test tasks but an upcoming fix in the October release will allow bindings for individual tasks as well.
2 4 6 | { 'command':'workbench.action.tasks.build' ] |
Calling MSBuild using Visual Studio Code task extensibility
MSBuild is already a pre-installed task that Visual Studio Code comes with. Bring up the command palette and choose MSBuild, this will create the following task.json it should be easy then to add your MSBuild solution, project name to the ‘args’ section and get going.
2 4 6 8 10 12 14 16 18 20 22 | // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format 'command':'msbuild', // Ask msbuild to generate full paths for file names. ], 'showOutput':'silent', { // Show the output window only if unrecognized errors occur. // Use the standard MS compiler pattern to detect errors, warnings and infos } } |
Calling CMake using Visual Studio Code extensibility
Visual Studio For Mac Ios
There are currently two Visual Studio Code extensions in the Visual Studio Code marketplace. The first extension provides the language service support for CMake the latter will allow for building your CMake targets. For a good CMake experience in Visual Studio Code install both extensions.
Once configured you should be able to build specific CMake targets and perform other CMake actions as illustrated in the figure below.
Wrap Up
This post provides some guidance with examples on how to use Visual Studio Code task extensibility to build your C/C++ application. If you would like us to provide more guidance on this or any other aspect for Visual Studio Code by all means do reach out to us by continuing to file issues at our Github page and keep trying out this experience and if you would like to shape the future of this extension please join our Cross-Platform C++ Insiders group, where you can speak with us directly and help make this product the best for your needs.
Thank you for downloading Visual Studio and starting your first C++ journey!
- First, understand the layout and views once you launch Visual Studio 2017:
- Next, review the standard build process for a Visual Studio project:
- If C++ is not an already installed language in Visual Studio, you need to install Desktop development with C++ through the Visual Studio Installer:
- After installing the Desktop development with C++ workload, you can choose the Win32 Console Application template and create your HelloWorld project:
- Click Finish to exit the Win32 Application Wizard
- You can see your first C++ project:
- Replace the code with the following:
- Next, add a breakpoint by clicking the grey area in front of line 12:
- Compile and run your project by clicking the green triangle in the tool bar (Local Windows Debugger) or press F5. Visual Studio allows single-click for build and debugging.
Click Yes to build the project:
- You can see “Hello World!” in the console window.
Troubleshooting:
If the console window closes immediately, you need to set the breakpoint in step 8.
If you are on Windows 8.1, you need to re-run the installer for Visual Studio, click modify, select languages, and choose C++.
Congratulations on your first C++ project!
Mac Version Of Visual Studio
Last updated on April 5, 2019Live Chat | English only
Mon – Fri, excluding holidays
Contact us for help with installation, IDE, languages, tools & utilities
Visual Studio 2017
Visual Studio 2019
Technical Support
Get supportAccount questions and help unlocking a paid copy of Visual Studio
Get supportGet help with Visual Studio licenses & downloads for your company
Volume Licensing help