I'm Michael Suodenjoki - a software engineer living in Kgs. Lyngby, north of Copenhagen, Denmark. This is my personal site containing my blog, photos, articles and main interests.
I'm Michael Suodenjoki - a software engineer living in Kgs. Lyngby, north of Copenhagen, Denmark. This is my personal site containing my blog, photos, articles and main interests.
Updated 2016.10.02 17:50 +0200 |
By Michael Suodenjoki, August 2016.
I wanted to play with and test Visual Studio Code's (VSCode) features for doing C++ development on the Mac platform.
At work and in the past 20+ years I've mainly been using Windows, so this is somewhat different and new interesting territory for me. Previously I've used C++ on the Mac at a basic level, but do have *nix experience, e.g. working with GCC compiler, GDB debugger, NetBeans etc.
Nevertheless, this document contain my VSCode experience results, with small how-to guides for getting up and running.
To follow, I do expect that you have a minimal understanding of working with text editors, C++ build/compilation, C++ debugging and terminal shell commands.
Test machine and installed setup:
Let's take a simple Hello World program:
#include <iostream>
using namespace std;
int main()
{
cout << "hello Michael. It works!" << endl;
}
VSCode requires (or works best) when a project is residing in its own main folder. This also makes the VSCode EXPLORER pane work appropriately.
test
(use Finder or Terminal)test.cpp
using File, Save As... or Shift(⇧)+Command(⌘)+S.To setup include path to point to the Standard C++ headers, I've added a new path /Applications/...
to the includePath
setting in the c_cpp_properties.json
file in the "Mac" configuration section.
... "configurations": [ { "name": "Mac", "includePath": ["/usr/include", "/Applications/XCode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include"], ... } ] ...
This is also where you would need to add the include path for/if you use a third-party library, e.g. boost.
To format C++ source code with clang-format, clang-format must be available (installed).
clang-format can most easily be installed by using the Homebrew package manager. See also http://macappstore.org/clang-format/
Activate Terminal with Command(⌘)+Space, type Terminal
and press Enter.
Install Homebrew package manager (skip if already installed) by executing shell command:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install clang-format using Homebrew by executing command:
brew install clang-format
Verify by running command:
clang-format --version
Should output something like, e.g.:
clang-format version 3.9.0 (tags/google/stable/2016-06-27)
To format your C++ source code file run Shift(⇧)+Alt(⌥)+F. Alternatively start the VSCode's View, Command Palette Shift(⇧)+Command(⌘)+P and search for and run Format Code
command.
Tip. You can place your own .clang-format file specifying your own configuration file for the formatting. For options see Clang Format Style Options
As far as I understand there is, as of this writing (August 5, 2016), no such thing as an integrated build command for clang in VSCode. Perhaps it is possible to establish a task runner for it.
I can see several options - at least:
Both of these options can be used with VSCode's integrated terminal.
Since, I could see that VSCode already have CMake extensions available and know that CMake is quite popular nowadays I though this could be interesting to use.
I'm not at all familiar with CMake, except at the most basic level. I understand what it is. I also know that some people believe it to be difficult to work with. Build tools usually are complex - particular in non-trivial, i.e. real development, environments. So this is not surprising.
To install CMake:
brew install cmake
to install CMake via Homebrew.cmake --version
to verify version installed. I worked with version 3.6.1.CMake
.Create a CMakeLists.txt
file in the main project folder with the content of:
cmake_minimum_required(VERSION 3.0) project(TEST) set(SOURCE test.cpp) add_executable(${PROJECT_NAME} ${SOURCE})
Note: Above does not specify full debug symbols.
Open Terminal and execute:
mkdir Debug cd Debug
to create a new Debug
sub-directory. This will eventually also contain a debug version of the executable. Source
Execute command:
cmake -DCMAKE_BUILD_TYPE=Debug ..
This will generate the Makefile
which can build debug executable from the test.cpp
C++ source file using XCode's C++ compiler (AppleClang).
Execute make
to build the debug executable - named TEST.
Output may look something like:
$ make Scanning dependencies of target TEST [ 50%] Building CXX object CMakeFiles/TEST.dir/test.cpp.o [100%] Linking CXX executable TEST [100%] Built target TEST
Run the executable with ./TEST
. You should see output like:
hello Michael. It works!
You can also debug your executable from within VSCode.
Start VSCode's debug mode using View, Debug or Shift(⇧)+Command(⌘)+D. You'll see that left pane changes to DEBUG and states C++ Launch.
Activate the settings (wheel) icon to open the launch.json
file and ensure that the sections referencing "program" is specifying the main executable file TEST
.
"program": "${workspaceRoot}/Debug/TEST",
Save the launch.json
file.
Select your test.cpp
file and set a breakpoint in a interesting code line, e.g. line 7 where std::cout << ...
is. You set a breakpoint on a line by either by double-clicking at the beginning of the line (left of line number) or placing the cursor at the line and hit fn 9 (F9). You should notice that the line is added to the lower BREAKPOINTS section in the DEBUG pane, e.g. showing "test.cpp 7" telling that you've set a breakpoint at line 7 in test.cpp file. There is even a checkbox there where you can disable/enable the breakpoint.
Start debugging using fn 5(F5) key. Hopefully you will see that system hits your breakpoint. Also you should see a small panel where you can control how to go along with execution, e.g.:
If your code contains variables, the current values at the breakpoint is shown in the Local VARIABLES part of the DEBUG pane.
What I've got:
Makefile
.It do take some time to setup the environment. It is not plug and play. All in all I have a good feeling about VSCode and want to learn more.
Could need (I will add to this list as I found more nice/need-to-have).