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.
revision control system
editor with at least syntax highlighting
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.
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 automating 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 for the GUI to update the shown content, and are not as easy to automate.
Unfortunately Windows does not provide such an environment out-fo-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.
If one prefers to work in a more Windows-like environment, then please do you a favour and use powershell as interactive shell and avoid
cmd.exe as much as possible. Note that not using cygwin will influence your choice about compiler and other tools.
If one does not like cygwin, there is also mingw, WSL or one can setup a virtual machine. Note that in those cases (except for mingw) the built binaries are not Windows native executables.
The compiler is GCC. Note that most distributions also provide clang, and as it has the same interface, using GCC or clang wont make any difference.
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 developing, a revision control system help to save and organize your work now, and can help you in the future once you start collaborating with other people for fun or professionally.
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 to support many platforms, and is already used by many project. 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 ports exists).
An IDE that supports debugging and the build system out-of-the-box would be better, but I have not found anything which I would recommend over anything else.
For the beginning, and for 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.
QtCreator is the IDE I would probably recommend as it has good support for cmake out-of-the box. Unfortunately it wont work 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.
C++ and C are not easy languages. They have a lot of gotchas and undefined behaviour. 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, tools that can find logical errors in your source code. For example, most compilers support optional diagnostic that can be enabled. For GCC, a good starting point would be
-Wall -Wextra -pedantic. There are a lot of other useful warning, unfortunately those are not enabled by default, and it can get 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 biggets advantage is that it can be used with no configuration on nearly every project, and does not depend on the build system.
The easiest way to get confident with source code is sperimenting 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
As already mentioned, C++ is not an easy language. Best way to start is with a good book that explains the basic, a little bit 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 good book, does not matter how good, can only help you so much.
When encountering cryptic compiler errors, strange bugs or have some fundamental questions how some things works, a browser with 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:
The environment I’ve presented is portable in the sense that except for the editor (which is of course important) most programs have been ported to all systems. This makes it easier to give advices 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 recomend 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 here.