C++ For Mac

Avoid Eclipse for C/C development for now on Mac OS X v10.6 (Snow Leopard). There are serious problems which make debugging problematic or nearly impossible on it currently due to GDB incompatibility problems and the like. See: Trouble debugging C using Eclipse Galileo on Mac.

  1. The Mac Observer's Videos. How can we improve Microsoft Visual Studio for Mac? Support C in Visual Studio for Mac Visual Studio for Mac enables developers to create applications using.NET.
  2. C/C is NOT supported on Visual Studio for Mac. To see what languages are supported, click here. Support for every codebase. CMake, Clang, mingw, and more. Open a codebase from any environment and get to work right away. Use MSBuild with the Microsoft Visual C compiler or a 3rd party toolset like CMake with Clang or mingw to build and debug.

In this tutorial, you configure Visual Studio Code on macOS to use the Clang/LLVM compiler and debugger. After configuring VS Code, you will compile and debug a simple program to get familiar with the VS Code user interface. After completing this tutorial, you will be ready to create and configure your own workspace, and to explore the VS Code documentation for further information about its many features. This tutorial does not teach you about Clang or the C++ language. For those subjects, there are many good resources available on the Web.

If you have any problems, feel free to file an issue for this tutorial in the VS Code documentation repository.

Prerequisites

To successfully complete this tutorial, you must do the following steps:

  1. Install Visual Studio Code and follow the setup instructions in Visual Studio Code on macOS.

  2. Install the C++ extension for VS Code.

Add VS Code to your PATH

In order to start VS Code from the command line, we'll need to add it to the PATH environment variable. This only needs to be done once, on first use.

  1. Open VS Code

  2. Press ⇧⌘P (Windows, Linux Ctrl+Shift+P) to open the Command Palette.

  3. Start typing 'Shell' and from the list of suggestions choose Shell Command: Install 'code' command in PATH.

  4. You should see a notification in the lower right of the VS Code window that tells you that VS Code was successfully added to the PATH.

  5. Close VS Code.

Start VS Code in a folder

C++ For Mac

In the macOS Terminal, create an empty folder called 'projects' and then a subfolder called 'helloworld'. Navigate into it, and open VS Code (code) in that folder (.) by entering the following commands:

The code . command opens VS Code in the current working folder, which becomes your workspace. Before we can get IntelliSense support, or compile and debug our code, we have to configure VS Code to use Clang/LLDB. After completing the configuration, we will have three files in a .vscode subfolder:

  • c_cpp_properties.json (compiler path and IntelliSense settings)
  • tasks.json (build instructions)
  • launch.json (debugger settings)

To reuse the configuration, you can copy these files to a new workspace and adjust the program name and other settings as needed.

Configure the compiler path

  1. Press ⇧⌘P (Windows, Linux Ctrl+Shift+P) to open the Command Palette. It looks like this:

  2. Start typing 'C/C++' and then choose Edit Configurations (UI) from the list of suggestions. This opens the C/C++ Configurations page. When you make changes here, VS Code writes them to a file called c_cpp_properties.json in the .vscode folder.

  3. Find the Compiler path setting. VS Code will attempt to populate it with a default compiler based on what it finds on your system. For Clang on macOS, the path should look like this: /usr/bin/clang.

    The Compiler path setting is the most important setting in your configuration. The extension uses it to infer the path to the C++ standard library header files. When the extension knows where to find those files, it can provide lots of useful information to you as you write code. This information is called IntelliSense and you'll see some examples later in this tutorial.

  4. Set IntelliSense mode to ${default}, which on macOS is clang-x64.

  5. You only need to modify the Include path setting if your program includes header files that are not in your workspace or in the standard library path.

  6. On macOS, you must set the macFrameworkPath to point to the system header files.

Microsoft Visual C++ For Mac

Visual Studio code places these settings in .vscode/c_cpp_properties.json. If you open that file directly, it should look like this (depending on your specific path):

Create a build task

Next, we need to create a tasks.json file to tell VS Code how to build (compile) the program. This task will invoke the Clang compiler to create an executable file based on the source code. C# for mac.

  1. From the main menu, choose View > Command Palette and then type 'task' and choose Tasks: Configure Default Build Task. In the dropdown, select Create tasks.json file from template, then choose Others. VS Code creates a minimal tasks.json file and opens it in the editor.
  2. Go ahead and replace the entire file contents with the following code snippet:

The label value is used to identify the task in the VS Code Command Palette; you can name this whatever you like. The args array specifies the command-line arguments that will be passed to the compiler that was specified in the previous step. These arguments must be specified in the order expected by the compiler.

The isDefault': true value in the group object specifies that this task will be run when you press ⇧⌘B (Windows, Linux Ctrl+Shift+B). The --debug argument causes debug symbols to be produced, which is required for stepping through code when you debug.

Configure debug settings

Next, we'll configure VS Code to launch the LLDB debugger when you press F5.

  1. From the Command Palette, type 'launch' and then choose Debug: Open launch.json. Next, choose the GDB/LLDB environment.

  2. For program, use the program name ${workspaceFolder}/helloworld.out (which matches what you specified in tasks.json).

  3. By default, the C++ extension adds a breakpoint to the first line of main. The stopAtEntry value is set to true to cause the debugger to stop on that breakpoint. You can set this to false if you prefer to ignore it.

  4. Set externalConsole to true to display the program output in an external Terminal window. (Currently on Mac the output cannot be directed to the integrated Terminal window.)

Your complete launch.json file should look something like this:

C++ Compiler For Mac

VS Code is now configured to use Clang on macOS. The configuration applies to the current workspace. To reuse the configuration, just copy the three JSON files to a .vscode folder in a new workspace and change the names of the source file(s) and executable as needed.

The remaining steps are provided as an optional exercise to help you get familiar with the editing and debugging experience.

Add a source code file

  1. In the main VS Code menu, click on File > New File and name it helloworld.cpp.

  2. Paste in this source code:

  3. Now press ⌘S (Windows, Linux Ctrl+S) to save the file. Notice how all the files we have just edited appear in the File Explorer view in the left panel of VS Code:

    Android File Transfer is a tool developed by Google. This tool enables users to browse and transfer files between Mac computer and Android device. For the users who wish to transfer files from Android device to Mac computer or from Mac to Android, using this software would be a good option. However, some users reject to download and install. Android file transfer for mac 10.5. Android File Transfer. Browse and transfer files between your Mac computer and your Android device. Download now. For Mac OS X only. No extra software is needed for. Dec 20, 2017  Download Android File Transfer 1.0.11 (1.0.442.1500). Transfer files via USB from your Android to your Mac. Android File Transfer is an official app developed by Google with which you can easily transfer files from your Android device to any computer running Mac OS X. To transfer any file. Sep 27, 2019  2. Android File Transfer for Mac by Google. Android File Transfer Mac by Google is the only native utility that could be rather helpful in file transfer, but unfortunately it has not been updated for several years and the interface is not easy to understand. Also it doesn't let you transfer more than one file at a time, so it may just take.

This same panel is also used for source control, debugging, searching and replacing text, and managing extensions. The buttons on the left control those views. We'll look at the Debug View later in this tutorial. You can find out more about the other views in the VS Code documentation.

Explore IntelliSense

In your new helloworld.cpp file, hover over vector or string to see type information. After the declaration of the msg variable, start typing msg. as you would when calling a member function. You should immediately see a completion list that shows all the member functions, and a window that shows the type information for the msg object:

Build the program

  1. To run the build task that you defined in tasks.json, press ⇧⌘B (Windows, Linux Ctrl+Shift+B) or from the main menu choose View > Command Palette and start typing 'Tasks: Run Build Task'. The option will appear before you finish typing.
  2. When the task starts, you should see an integrated terminal window appear below the code editor. After the task completes, the terminal shows output from the compiler that indicates whether the build succeeded or failed. For a successful Clang build, the output looks something like this:
  1. As the message instructs, press any key to close the integrated terminal.

Start a debugging session

  1. You are now ready to run the program. Press F5 or from the main menu choose Debug > Start Debugging. Before we start stepping through the code, let's take a moment to notice several changes in the user interface:
  • The Debug Console appears and displays output from the debugger.

  • The code editor highlights the first statement in the main method. This is a breakpoint that the C++ extension automatically sets for you:

Mac
  • The workspace pane on the left now shows debugging information. These windows will dynamically update as you step through the code.
  • At the top of the code editor, a debugging control panel appears. You can move this around the screen by grabbing the dots on the left side.

C++ Macro For Logging

Step through the code

Now we're ready to start stepping through the code.

  1. Click or press the Step over icon in the debugging control panel.

    This will advance program execution to the first line of the for loop, and skip over all the internal function calls within the vector and string classes that are invoked when the msg variable is created and initialized. Notice the change in the Variables window on the left. In this case, the errors are expected because, although the variable names for the loop are now visible to the debugger, the statement has not executed yet, so there is nothing to read at this point. The contents of msg are visible, however, because that statement has completed.

  2. Press Step over again to advance to the next statement in this program (skipping over all the internal code that is executed to initialize the loop). Now, the Variables window shows information about the loop variables.

  3. Press Step over again to execute the cout statement. Your application is now running in a macOS Terminal window. Press Cmd+Tab to find it. You should see Hello output there on the command line.

  4. If you like, you can keep pressing Step over until all the words in the vector have been printed to the console. But if you are curious, try pressing the Step Into button to step through source code in the C++ standard library!

    To return to your own code, one way is to keep pressing Step over. Another way is to set a breakpoint in your code by switching to the helloworld.cpp tab in the code editor, putting the insertion point somewhere on the cout statement inside the loop, and pressing F9. A red dot appears in the gutter on the left to indicate that a breakpoint has been set on this line.

    Then press F5 to start execution from the current line in the standard library header. Execution will break on cout. If you like, you can press F9 again to toggle off the breakpoint.

Set a watch

Sometimes you might want to keep track of the value of a variable as your program executes. You can do this by setting a watch on the variable.

  1. Place the insertion point inside the loop. In the Watch window, click the plus sign and in the text box, type word, which is the name of the loop variable. Now view the Watch window as you step through the loop.

  2. Add another watch by adding this statement before the loop: int i = 0;. Then, inside the loop, add this statement: ++i;. Now add a watch for i as you did in the previous step.

  3. To quickly view the value of any variable while execution is paused on a breakpoint, you can simply hover over it with the mouse pointer.

Next steps

C++ For Mac

C++ For Mac Free Download

  • Explore the VS Code User Guide.
  • Review the Overview of the C++ extension
  • Create a new workspace, copy your .json files to it, adjust the necessary settings for the new workspace path, program name, and so on, and start coding!