Hello and welcome to C++ weekly. I'm your host Jason Turner and available for on-site training code reviews and
Contracting. Now in the last episode I started a series on learning modern C++
by using the tools that are available and
I'm going to continue that now in this episode with setting up your first project and writing a simple hello world program
Now at the moment you might be thinking I know how to write hello world
I don't have anything to learn from that
But there's a chance that I will actually show a couple of things that you have not considered before
Now before I continue
I would like to address the question of why I am using Visual Studio in case you had any questions about that
Particularly since I tend to be a Linux user and I use the compiler Explorer a lot in my examples
Now the reason is because Visual Studio with the clang power tools
Which I demonstrated the installation of in the last episode give us a relatively unique ability to have
Almost all of the advantages of doing cross-platform development
While operating in a single IDE that is we can use
Microsoft's compiler which is CL.exe
and get its unique warnings and capabilities while being able to call clang-tidy and
clang++ and getting its unique warnings and
abilities and
We can integrate cppcheck and the clang-format tool now
It'll probably be a few episodes until they actually get to clang-format, but we will discuss the other things
Leading up to that Visual Studio is also one of the world's most widely used
if not the world's most light widely used IDE so there is no question that many people use it and
starting with the
Community Edition Visual Studio
Which we are using here
It is free so pretty much anyone in the world can download
The Visual Studio Community Edition and be able to follow along with some of these examples
So I think these are all good reasons to use Visual Studio for these examples
Getting the exact to same capabilities on Linux is virtually impossible
because of the lack of ability to call Microsoft's compiler and
Using a different tool such as Visual Studio code does not give us the same integration with clang-tidy that I am looking for
so that's basically where we are at the moment there might have been some other options, but I
Think this was a good decision
So we're going to start by setting up our new project and we want to use an empty C++ project
You can see here that I have it in my recent project templates, but if you don't have that in your list yet
You can just type empty and it will autocomplete for you, and you can choose empty C++ project
And we are going to name this one C++. Weekly and
Again empty project. This is going to have absolutely no files in it at all at the moment
We're not going to be adding it to source control although
I would highly recommend that you use the github
Integration and create your own private repository for actually keeping track of the stuff that you're doing
So we have no project here at all
we're going to go ahead and create our first source file which is going to be a
C++ file that we're just going to call main.cpp
This will be
Pretty much where we'll do all of the work for these examples although in a real world project you would want some sort of organization
and structure
So we're going to create another file
now granted Visual Studio doesn't have built-in support for the particular file that I want to create but I
Think it'll cooperate with us here
We're going to tell it that we're creating a C++ file, but we're just going to name it .clang-tidy
and it should be okay with us and
sure enough it is and
This is going to be the file that our clang-tidy is going to load its settings from and we are using this file
Because this again kind of gets us working towards the mentality of cross-platform
We're not going to save the settings that we want for clang-tidy in the Visual Studio IDE
We're going to save it in our directory folder here
And then if we were to run clang-tidy from any other platform. It would still pick up the same things
So clang-tidy has many many checks that it can perform
We are going to enable all of them in a real-world project you might not want to enable all of them, but
All of the rules and client ID exist for a some well-thought-out reason that someone had some of them are security
flaws that can find others are just coding best practices
Modernization that kind of thing we're just going to enable them all for right now because we're doing this as a learning experience
Now we also want to make the clang power tools easily accessible to us
And we're going to right click here on our menu bar and enable the clang power tools menu
So now we have these options and we're going to go into our settings, and we're going to tell clean power tools
That yes we want it to fix errors that it finds this will become interesting in a moment and
We want it to grab the list of checks from our clang-tidy config file
So that should be all that we need to do here now. We're also going to set up our cppcheck options and
We want to enable this
inconclusive checks here
which gives us some very interesting results so that chop check box is checked and
We have here --enable=all I added that
Previously and this has enable all of the possible checks that cppcheck can do
Including things that it's not sure about now cppcheck. I have never seen it
Give me a false positive or maybe very very rarely a false positive on one of its warnings
But in a couple of cases it might contradict with the warnings that we get from our other tools
and these will be good learning experiences when our tools contradict each other, so we're going to leave that there and
Now finally we want to set our project settings in Visual Studio now. I have zoomed the UI
So we don't actually are able to actually see the okay button down here. Which is a bit disconcerting?
Even if I try to resize this it doesn't quite work
But I think it will be fine. We want to specify warning level 4 (/W4) to start with and
We want to double check that
/permissive- is set which means it's not going to let us do things that are outside of the C++ standard this is
This is great
We really want this as our default setting and Visual Studio
and I'm very happy to see that it seems to have been the default setting here and
Finally we want to set the language standard to /stdc++latest now Visual Studio does not support all of C++
17 yet and
It does not support really you know just pieces of C++ 20 yet, but really that's okay because no one is supporting C++
20 yet, and we're only into the first few standards meetings of what C++
20 even means so hopefully we have our settings all set we will hit enter
to close that dialog to make sure it's set the settings and
There we go we should be able to create our first. Hello world example
so if you watched my video on
Using puts() you might be inclined to do write your hello world that looks something like this as
I would be
Inclined to do this is just going to write
"Hello world" to the screen and do that an exit so we're going to save this file
So we're going to start by compiling with visual studio, and we're just going to hit build solution
And we don't really expect it to find anything here, and it does not now
We're going to go ahead and run our clang-tidy checks
Now remember clang-tidy is configured to automatically fix anything that it finds
So I'm going to hit this on our toolbar tidy code, so it has applied one fix for us
It says here clang inclusion of deprecated C++
Header stdio.h consider using cstdio instead so
Stdio.h is not actually part of the c++ standard if you want to use these c functions like puts()
Then you need to use cstdio, and so it has automatically applied that fix for us
Now we are going to say well. You know what this is not really C++
Anyhow let's go ahead and use our iostream header
And if you're following along with examples on the Internet you probably see code that looks like this
This is a fairly typical example so if we compile and run this now
We are again not getting any warnings from Visual Studio, which is not terribly surprising because we're not really doing anything wrong
But if we run this with clang-tidy instead
We get this warning from the Google
Coding standards which are sometimes maligned?
But those that are implemented in clang-tidy are actually fairly helpful
This says do not use namespace using directives
And this is true many people agree that "using namespace" is bad
Even in a C++ file because it imports all of those identifiers into the global namespace
So it is suggesting that if we wanted to instead we could do something like
using
and
Pull that into the local namespace, and we can even see here that now we do not have a squiggly under std::cout
But we do under std::endl and this starts to become
You know
a bit difficult to manage so a
lot of people today would suggest that it is better to just
Explicitly name these things depending on the context
There are contexts where you can't but in these we can explicitly give the std namespace
And do this now. This is not with clang-tidy running going to give us any warnings
I presume and neither will cppcheck, but if you are a fan of these videos, then you would probably
Notice the fact that I would not be using std::endl here
And let's go ahead and run cppcheck just to be sure
And
We did not get any errors from cppcheck
So I would suggest to use our "\n" newline character here
So this is the one of the cases where our tools?
Will fail us if you will now it granted
This is one of my best practice rules not one of the widely accepted best practice rules
But the tools that we have are not going to be infallible
But they will get us very far along the way as we are
practicing our C++ and trying to learn the current modern best practices so this wraps up episode 2 of
Creating our initial hello world for our learning modern C++. By using the tools that are available series
Be sure to subscribe follow me on Twitter and check out any of the links below