Thursday, September 28, 2006
C# to C++ - learning C++ with wxWidgets
The invention of C# was a really clever move by Microsoft. It is similar enough in syntax to appeal to Java users, just as easy to use as VB, and only a fraction (5% IIRC) slower than C++. I started using it because I was taught Java, but wanted something that was faster and easy to use with regards to Windows programming, and C# is that. The bottom line is that for Windows programming, C# is by far and away the easiest language to use, unless you really need that extra 5%. This is borne out by the amount of C# software that is being written these days. Many companies don't advertise for MFC developers anymore, they want C# developers, and even ATI write their GUI for controlling their graphics drivers in .NET. And thanks to the efforts of the mono-project (which I've yet to try but apparently works quite well) C# programs can now be compiled and run on linux.
Yet there is no getting away from the fact that, sticking with our example of ATI, the graphics drivers themselves are almost certainly not written in C#. C# and .NET neatly hide away a lot of under-the-hood information about how software interacts with hardware, and how a program actually works. This is fine, but out in the wide-world I feel it's probably a good thing to get a broader view, and so I've finally taken the plunge and make the effort to start learning C++.
So, from my experiences in the last week or so, here are few bullet points with advce for those people who want to transfer their C# to C++:
Yet there is no getting away from the fact that, sticking with our example of ATI, the graphics drivers themselves are almost certainly not written in C#. C# and .NET neatly hide away a lot of under-the-hood information about how software interacts with hardware, and how a program actually works. This is fine, but out in the wide-world I feel it's probably a good thing to get a broader view, and so I've finally taken the plunge and make the effort to start learning C++.
So, from my experiences in the last week or so, here are few bullet points with advce for those people who want to transfer their C# to C++:
- Learn the basics: go to http://www.cplusplus.com/doc/tutorial/, and using Visual Studio create a new C++ console program, and work through the tutorials. Much of it is familiar from C#, but it gives you an idea of the how objects, class and function definitions, and memory referencing and pointers work in C++. Java and C# are very similar in terms of object orientation, and so this page contains a good overview of converting to C++, especially with regards to the use of header files, which puzzled me a little to start off with.
- Don't use MFC!: Assuming you're used to programming Windows software with C#, the first rule of C++ is not to use MFC!! It is, quite frankly, a nightmare. I spent one afternoon working through a couple of tutorials before giving up in disgust. Fortunately, there are several 'widget' toolkits that are much easier to use. The two big 'completely free' ones are SmartWin and wxWidgets. I started using both for evaluation, but it didn't take long to choose to concentrate on wxWidgets, as it is a bit more object-orientated (fewer macros), rather C#-like in the way it uses components, easier to set up, and there are more discussion lists and forums dedicated to it. There is a really superb newbie wxWidgets tutorial to be found here. Also wxWidgets is pretty platform independent, and you can use it to write GUIs for Windows, Linux and even (pah!) Mac.
- Decide on your compiler and IDE: Unless you use some form of setup wizard, creating a new C++ project and correctly compiling/linking C++ programs with Visual Studio (I use 2005) is, quite frankly, a pain in arse. There are a million and one options, debug/release/unicode, and you can waste a LOT of time doing it. Also, seeing as you don't want to use MFC using a different compiler IDE may be a good idea. I started using Dev-C++, it does integrate plugins very well, but if you're used to the feature-rich IDE like Visual Studio, it feels like stepping back in time to the stone-age. Fortunately, Code::Blocks is easy to setup and compile (much better than VS2005) and has quite a lot of very good features. It also runs on linux (built with wxWidgets, see?!) and it's what I'm using.