A minimal C++ development environment
Sometime I get asked what is the best way to get started programming in C++ (or C).
This is what for me would be a minimal C++ development environment.
-
posix-like environment
-
revision control system
-
editor with at least syntax highlighting
-
compiler
-
static analyzer
-
build system
-
good resource
-
internet connection (and browser)
This list is still "abstract", as I do not mention any program in particular. Because I like to have a consistent environment, and I like not to change my habits too often, I try to use, when possible, similar environments.
Bash-like shell/environment
Any Linux system comes with a bash-like shell, so probably no need to do anything.
I prefer to work from the command line (or at least do some kind of tasks on the command line), because it is so much easier to automate them, once you notice you are repeating the same sequence of commands multiple times.
With GUI it is also possible to automate many tasks, but often you need external programs, they might be slow as they need the GUI to update the shown content, and are not as easy to automate.
Unfortunately, Windows does not provide such an environment out of the box, but there is a very good port: cygwin.
The biggest limitation is that programs compiled with cygwin
should be executed from a cygwin
shell.
Variation
If one prefers to work in a more Windows-like environment, then please do yourself a favor and use Powershell as an interactive shell and avoid cmd.exe
as much as possible. Note that not using Cygwin will influence your choice of compiler and other tools.
If one does not like Cygwin, there is also MinGW, WSL, or one can set up a virtual machine and install a Linux system. Note that in those cases (MinGW excluded) the built binaries are not for Windows.
Compiler
The compiler is GCC.
Note that most distributions also provide Clang, and as it has the same interface, using GCC or Clang won’t make any difference.
revision control system
I would recommend git because
-
it is easier to use than Subversion
-
it works "offline", without needing a server
-
the most famous repositories all support git
-
it is probably the most used revision control systems
While not necessary for development, a revision control system helps to save and organize your work and can help you in the future once you start collaborating with other people for fun or professionally.
Build system
While it is possible to build programs by invoking the compiler by hand, it is best to leave this task to a build system.
CMake has the advantage of supporting many platforms and is already used by many projects.
Make is the de facto build system on POSIX systems and for many open-source projects, but it is not as easy to use, and it does not work well on Windows-like systems (even if there are ports for Windows).
An editor with at least syntax highlighting
An IDE that supports debugging and the build system out-of-the-box would be better, but I have not found anything that I would recommend over anything else.
For the beginning, and a minimal environment, it might be better to compile from the command line, thus an editor is sufficient, but it should at least support syntax highlighting. I believe most graphical editors can do that (except notepad.exe
) considering that even command line-based editors support it.
Variation
QtCreator is the IDE I would probably recommend as it has good support for CMake out of the box. Unfortunately, it does not work well with Cygwin and WSL.
On Windows, when working with MSVC, the choice would be Visual Studio, as installing Visual Studio and MSVC is the fastest way to have a development environment with an IDE.
At least one static analyzer
C++ and C are languages with many rules.
They have a lot of gotchas and undefined behavior.
This means that an incorrect program seems to work correctly, and unrelated changes can break parts of the program, like adding or removing a comment.
It is crucial to avoid such type of errors as soon as possible.
There are many static analyzers, which are tools that can find logical errors in your source code.
For example, most compilers support optional diagnostics that can be enabled.
For GCC, a good starting point would be -Wall -Wextra -pedantic
.
There are lot of other useful warnings, unfortunately, those are not enabled by default, and it can take some time to find the most useful.
Another static analyzer easy to use (even with a GUI for those who feel more conformable) and multiplatform, is Cppcheck. The biggest advantage is that it can be used with no configuration on nearly every project, and does not depend on the build system.
A test suite
The easiest way to get confident with source code is by experimenting with it.
Test suites like Catch2 make this task easy.
-
It’s just a header file, without additional dependencies
-
Simple syntax, way less macros compared to other frameworks
-
Useful error messages if a test fails
A good resource
As already mentioned, C++ is a complex language.
The best way to start is with a good book that explains the basics, a little bit of the background of the language, and provides some exercises.
Make sure to pick a "recent" book, since C++11 the language has changed a lot and continues to evolve.
Some good books are
-
A Tour of C++ (Second edition), updated for C++17
-
Effective Modern C++, with focus on C++11 and C++14
Browser and internet connection
A good book, no matter how good, can only help you so much.
When encountering cryptic compiler errors, or strange bugs, or if you have some fundamental questions about how some things work, a browser and a search engine (or a chat/forum/…) will help you to find the solution faster.
The difficult part is distinguishing between good resources and bad resources.
Some sites that probably everyone working with C++ should be aware of (in no particular order):
Why not recommend system-specific tools?
The environment I’ve presented is portable in the sense that most programs have been ported to all systems.
This makes it easier to give advice to people working in different Linux distributions and Windows versions.
Restricting oneself to a subset of tools working in all environments is not a very productive approach, but it is useful for getting started.
Once one has got the basics, it’s time to expand the toolbox and take advantage of platform and system-specific tools.
For example, if working on a GNU/Linux machine, I would absolutely recommend Valgrind.
Like Cppcheck, it normally does not require any setup.
I would like to recommend it to everyone, but it does not work on Windows systems!
Do you want to share your opinion? Or is there an error, some parts that are not clear enough?
You can contact me anytime.