OS X Lion C++11, Boost and other questions - c++

I am an experienced Mac/iOS developer, but I initially started to program in C++. I haven't touched C++ for years, and now, it's time to do so, because I want to have multi platform support for a new kind of file type I am trying to create.
C++ has evolved over the years, to what I find is a very bloated mess of 3rd party libraries and an effort by the C++ standards committee to control the evolution of the language. Hence, my questions.
I want to have as much advanced tools at my disposal as possible, and at the same time conform to standards as possible. My main development platform is OS X Lion, and I have access to a fedora 16 installation. I want to target Lion and above, Windows XP and above, and the latest Linux kernels, so backwards compatibility is not an issue here.
One choice is to install the Boost libraries to my machine, but to what I have experienced a long time ago it is a very painful process, with compile-time errors and quirks that have to be done to OS X. I don't know if that experience will be the same if I try that now. The other choice is to stick with TR1 which Lion currently offers. However, TR1 is not a standard as I understand, it is a de facto popular implementation of things that were scheduled to be done in C++11. That way I lose a lot of advanced features that Boost offers.
With these two options in hand, what is the recommended way to have advanced C++ features at your disposal and conform to standards as much as possible? If it's Boost, is it recommended to compile Boost as static libraries in order to avoid installing Boost on end-user machines?
What is the current support of Xcode 4.3 for C++11 features?
I would appreciate any comments on the above questions as well as any other thoughts on the matter. I am trying to get in sync with the current version and features of C++ and I begin to realize that this may not be so easy as I initially thought.
Thanks.

First of all, there are two very distinct parts in Boost:
those which are header-only
those which require compiled libraries
If you stick to header-only, it will definitely be much easier. Note that some libraries, like Asio, have two compilation modes (header-only and library). A number of useful libraries, however, such a boost::regex, do require a compiled library.
Second, C++11 support is moving fast. GCC 4.7 and the upcoming Clang 3.1 support all major features of the Standard, except from atomics (discussions are still ongoing on the best implementation strategies), so on Linux and Mac, things are great... however Visual Studio is lagging behind, and Microsoft is not really interested in moving fast, so on Windows support is minimal still (and advertised supported features are based on older versions of the Standard and not 100% compatible with the last version). It does not mean you cannot compile for Windows, merely that you should use Mingw or equivalent and thus forgo interaction with existing Windows DLLs.
Based on those two observations, I would recommend:
try to avoid C++11 for now if you want to interact with Windows DLL, otherwise I would recommend Clang (for its integration in XCode)
try to avoid Boost libraries and stick to the header-only parts (there is still much goodness)
if you want libraries, you can use DLL as long as you distribute them alongside the executable or use a package system or whatever, but it does present a greater difficulty (for the installation) than just static linking.

Note I'm not a Mac developer but I'd prefer Boost. Many C++11 standards are taken from it and you can expect more Boost features will be ported to C++ standards.
And as a Qt enthusiast: take a look at QtCore. It's very powerful and cross-platform.

Related

Can a C++ program run on Mac OS?

I have watched a video in which one of the director of scs software (who makes a EURO TRUCK SIMULATOR game) saying that the game is written entirely in C++, yet the game runs on Windows and Mac OS as well. How could they achieve it? Is it possible to run a C++ program on MAC OS?
Yes.
...OK, stack overflow wants at least 30 characters. OS X comes with the C++ compiler clang++, which is built on top of llvm. In fact, Apple stopped supplying a port of gcc some time ago, so developers either have to build it themselves or adapt to clang. According to Wikipedia, Apple itself develops clang.
It is possible, either to run gcc in terminal or to use Xcode to compile it, however you cannot then use Cocoa but alternatives such as Qt are available for mac.
Sure, there are C++ compilers for Mac OS X as well. One is even included in xcode (the standard development environment on a Mac).
But that does not mean, that you can compile just any C++ program on a Mac. It has to be designed to use libraries, that are available on both platforms (instead of MFC on Windows or Cocoa on the Mac). This is possible for example by using wxwidgets or QT. With these two libraries it is even possible to compile the program for Unix platforms like Linux as well.
Regardless of language a multi-platform program relies on some basic support parts that are implemented differently on each platform. With pure standard C++ this is the standard library and the runtime support, but in practice a game, say, will also rely on 3rd party libraries with platform specific implementations. So given that the platform itself is compatible with the basic platform assumptions of the C++ language, and the Mac is, achieving such portability is conceptually very straightforward, although difficult to get perfect.
As an example of a platform that's not entirely compatible with C++, Microsoft's .NET virtual machine is apparently so at odds with the ideas of C++ that Microsoft has found it necessary to define two different non-standard variants of C++ for it, and offers no way to produce GUI programs for it in pure standard C++. It's not impossible to use only standard C++ for .NET, and indeed one main reason to use C++ with .NET is to combine standard C++ parts with more .NET-y parts; it's just that using only standard C++ the code becomes complex, inefficient and generally impractical. For example, my first questions here on SO concerned how to do a simple .NET message box in standard C++ by way of COM technology as an intermediate layer, and that worked, but with a ridiculous amount of complex non-general support code.
As another example, using C++ in web pages is very much an impedance mismatch, so to speak. Again Microsoft is the main player who has attempted this, with their ActiveX technology. It turned out to be a good way ensure that PCs all over the world got infested with malware, because C++ is not the kind of language where it's easy to constrain what the code can do. As far as I know only Microsoft's own Internet Explorer browser ever supported ActiveX in web pages. And as of Windows 10 the IE browser is being phased out and replaced by the newer Edge browser, that does not support ActiveX in web pages.

Difference between Win-builds vs MinGW-builds

Does anybody know the difference between Win-builds and MinGW-builds? I'm really interested in C++11 (14) compiler for Windows, but unfortunately MSVC is not yet fully C++11 compliant.
The difference is simple: they have little bit different goals.
In particular, if you want to develop on Windows (i.e. don't need cross-compilation), then you better off with MinGW-builds project which essentially provides several feature-rich variants of the MinGW-w64 toolchain. The table in the link gives good summary of the features I'm talking about. You can basically pick the compiler based on your requirements to addressing model, threading model, exception model. I consider it mature and stable as I use it for everyday work and I'm very satisfied with the quality.
On the other hand, Win-builds project's primary goal seems to be cross-compilation. You'll notice that in their thesis in the link, and you'll also notice that it is less feature-rich than MinGW-builds, hence you have less flexibility with it. It is also relatively new compared to MinGW-builds, so I would expect to potentially experience some issues with it.
NOTE: Please, understand that both of these projects are simply builds (distributed ready-to-use binaries) of the MinGW-w64 toolchain (GCC port for Windows which supports both x86 and x64 targets) on whose web-site both links above are pointing. They are provided only for user convenience, so that we (users) don't have to hassle with compilation of the MinGW-w64 toolchain ourselves, but nobody prevents you from doing that if you're experienced enough and know what you want.
For further reading on related topic and C++11, I would recommend my other answer which you might find interesting.
Good luck.

Can I run C and C++ on any platform?

If I write in C or C++ on for example: Windows. Is it guaranteed than I can compile and run it on any other operating system such as Mac OS, Linux, Unix-like systems? So, does it mean C or C++ is cross-platform language?
If you write a C or C++ program that strictly complies with some standard, then the program should work on any platform that supplies a tool chain that complies with that standard. In that sense, C and C++ are cross-platform languages.
No.... there are C and C++ compilers for many many many platforms, but different compilers have their own quirks, and the libraries they link to are completely different on various platforms. Mozilla had a guideline of what features to use and what to avoid to make your software cross platform.
There are environments like cygwin that help with cross platform compatibility in windows *nix.
You can write libraries that are stock standard C that don't have dependencies on platform libraries that will be pretty portable
If you directly access any Windows API, it will fail to run (or even compile) on other platforms. If you use a standard function which indirectly accesses the correct API, or if you add #ifdef guards and access the correct platforms API, then the answer is better. The former should be cross platform. Latter will work on platforms your code caters to.
For Mac OSX, Linux, and, of course, Windows, you can write and compile C++. From personal experience, I've always found Windows to be easiest for C++ usage, closely followed by Linux, and with Max OSX trailing distantly behind. The compilers tend to be temperamental in my experience, and either because the support community is better for C++ on Windows, or because it's naturally better for programming, I've always had less problems with Windows. Though I labeled it in second place, I don't have that much experience in Linux.
Edit: You say "guaranteed" to run and compile. For basic C++ this is definitely the case, but some more advanced features may have varying support across platforms.

GCC worth using on Windows to replace MSVC?

I currently develop in C++ on Windows, using Visual Studio 2010. After the official announcement of C++11, I have begun to use some of its features that are already available in MSVC. But, as expected, the great majority of the new changes are not supported.
I thought maybe the upcoming version of Visual Studio would add these new features. However, after reading this it looks like very little is going to change.
And so, I'm curious about the feasibility of using GCC on Windows rather than MSVC, as it appears to support the great majority of C++11 already. As far as I can tell, this would mean using MinGW (I haven't seen any other native Windows versions of GCC). But I have questions about whether this would be worth trying:
Can it be used as a drop-in replacement for cl.exe, or would it involve a lot of hacks and compatibility issues to get Visual Studio to use a different compiler?
The main selling point for Visual Studio, in my opinion, is it's debugger. Is that still usable if you use a different compiler?
Since GCC comes from the *nix world, and isn't native to Windows, are there code quality issues with creating native Windows applications, versus using the native MSVC compiler? (If it matters: most of my projects are games.)
In other words, will the quality of my compiled exe's suffer from using a non-Windows-native compiler?
MSVC has the huge advantage of coming with an IDE that has no equals under Windows, including debugger support.
The probably best alternative for MinGW would be Code::Blocks, but there are worlds in between, especially regarding code completion and the debugger.
Also, MSVC lets you use some proprietary Microsoft stuff (MFC, ATL, and possibly others) that MinGW has no support for, and makes using GDI+ and DirectX easier and more straightforward (though it is possible to do both with MinGW).
Cygwin, as mentioned in another post, will have extra dependencies and possible license issues (the dependency is GPL, so your programs must be, too). MinGW does not have any such dependency or issue.
MinGW also compiles significantly slower than MSVC (though precompiled headers help a little).
Despite all that, GCC/MinGW is an entirely reliable quality compiler, which in my opinion outperforms any to date available version of MSVC in terms of quality of generated code.
This is somewhat less pronounced with the most recent versions of MSVC, but still visible. Especially for anything related to SSE, intrinsics, and inline assembly, GCC has been totally anihilating MSVC ever since (though they're slowly catching up).
Standards compliance is a lot better in GCC too, which can be a double-edged sword (because it can mean that some of your code won't compile on the more conforming compiler!), as is C++11 support.
MinGW optionally also supports DW2 exceptions, which are totally incompatible with the "normal" flavour and take more space in the executable, but on the positive side are "practically zero cost" in runtime.
I want to add some information because the field may have changed since the question was asked.
The main problem for switching away from MSVC was the lack of a good IDE that flawlessly integrates with MinGW . Visual Studio is a very powerful tool and was the only player on Windows for quite some time. However, Jetbrains released a preview version of their new C++ IDE CLion some days ago.
The main benefit comes when working on cross platform applications. In this case, a GCC based tool chain can make life much easier. Moreover, CLion narrowly integrates with CMake, which is also a big plus compared to Visual Studio. Therefore, in my opinion, it is worth to consider switching to MinGW now.
GCC's C++11 support is quite phenomenal (and quite up to par with standards conformance, now that <regex> has been implemented).
If you replace your compiler, you'll need to make sure every dependency can be built with that new compiler. They're not made to be substitutable plugins (although Clang is working on becoming that way).
GCC is a fine compiler, and can produce code that has pretty much the same performance, if not better, than MSVC. It is missing some low-level Windows-specific features though.
Apart from this, to answer your questions:
To get VS to use GCC as a compiler, you'd pretty much need to turn to makefiles or custom build steps all the way. You'd be much better off compiling from the commandline and using CMake or something similar.
You cannot use the VS debugger for GCC code. GCC outputs GDB compatible debug information, and the VS debug format is proprietary, so nothing will change in that area anytime soon.
Code quality is just as good as you'd want it. See above.
No, the quality of your code will actually increase, as GCC will point out several assumed standard extensions MSVC would hide from you. All self-respecting open source projects can be compiled with GCC.
I my humble opinion, it's depends how someone started to code in the first place. I've been using g++ and gcc for more than 20 years now but the reason why i keep using gcc is mainly for licensing reasons. Although i like it too when i don't have a bunch of runtime dependencies or dll's to bundle with my stuff since i came from the DOS era, i still like my stuff small and fast. gcc for windows comes with standard win32 libraries and common control but i had to develop my own win32 controls for stuff that might require mcf shit to work properly or just to look nicer.
Although gcc might have strong support over internet, when it comes to win32 stuff, many rely on mcf and vc proprietary stuff so again, one may have to work his own issues around and be creative when difficulty arises.
I think it's all about needs and circumstances. If you are just a hobbyist coders and have the time for researches, creating you own libs and stuff but you want a solid compiler that's around since the late 80's and free, gcc sound perfect for the job.
But in the industry visual studio is a must if you want to be competitive and stay in the race. Many hardware manufacturers would prefer bundling visual studio compatible libraries for they hardware over some opensource gnu stuff.
That's my two cents.
To be honest, C++ should be handled with MS Visual Studio. If you want to make cross-platform or Unix apps, use GCC. GCC works and can be used with any IDE other than Visual Studio. Even Visual Studio Code can use GCC. Code::Blocks, Eclipse IDE for C/C++ developers, CLion, Notepad++ and even the good ol' tool we've always known, Notepad works with GCC. And finally, on a PC with low disk space, installing Visual Studio's "Desktop Development with C++" is something like 5 GB, if it was to be useful. And this is where GCC hits MSVC hard. It has native C support. MSVC can compile C, but only with a lot of fine-tuning. It takes a lot of time and effort to finally be able to compile. The final verdict:
If MSVC works, it hella works! If MSVC doesn't work, it HELLA DON'T WORK.
If GCC installs, it works, and if it doesn't work, it's the IDE's problem.
GCC is for people who don't mind spending 4 hours at the computer making it work properly. MSVC is for those who don't care about C and want it to install without any pokin' around.
It can't be used as a direct swap-out replacement for the microsoft compilers, for a start it has a vastly different set of command line arguments and compiler specific options.
You can make use of MinGW or Cygwin to write software but introduce extra dependencies ( especially in the case of cygwin ).
One not often touted advantage of gcc over cl is that gcc can be used with ccache to drastically speed up rebuilds or distcc to build using several other machines as compiler slaves.
Consider the Intel compiler (or "Composer" as they seem to have taken to calling it) as another option. I'm not too sure where its C++11 support is at compared with MS (certainly it has lambdas), but it does integrate very nicely with VisualStudio (e.g different projects within a solution can use the Intel or MS compilers) and there's also been some efforts made to match the MS compiler commandline options.
GCC and MSVC use different name mangling conventions for C++. C++ dlls compiled by one compiler can not be used in applications compiled with the other. I believe this is the main reason we don't see more widespread use of gcc in windows.

How do I determine which C/C++ compiler to use?

I am trying to figure out which C/C++ compiler to use. I found this list of C/C++ compilers at Wikipedia:
http://en.wikipedia.org/wiki/List_of_compilers#C.2FC.2B.2B_compilers
I am fairly certain that I want to go with an open source compiler. I feel that if it is open source then it will be a more complete compiler since many programmer perspectives are used to make it better. Please tell me if you disagree.
I should mention that I plan on learning C/C++ mainly to program 2D/3D game applications that will be compatible with Windows, Linux, MAC and iPhone operating systems. I am currently using Windows Vista x64 OS.
First of all, IMHO as a beginner your development environment (IDE) matters a lot more than the compiler.
I think that people place too much emphasis on compiler choice early on. While it is not Java, C++ is meant to be portable.
If the program you're writing only works with specific compilers, you're probably doing the wrong thing or can work a little on making it more portable.
If you get to a point where compiler choice makes a significant performance impact for you, then you've already perfected everything else in your program and you're in a good state and you are also quite advanced in your abilities. We used to teach the differences between compilers at fairly advanced stages in the CS curriculum.
If you use a UNIX based machine (Linux, Mac, actual Linux), then pretty much GNU (g++) is the way to go and is fairly much standard. If it's good enough to compile your OS, it's probably good enough for you. On a mac you can use XCode as your IDE, and it interfaces well with g++. On Linux some people prefer command line tools, though you might like the Eclipse C++ support, it is much better today than it was 3-4 years ago.
Things on Windows are trickier. If you can afford it, have access to, or are eligible for one of the free editions (e.g., via a school), I think the Microsoft Visual C++ Environments (or whatever they are called now) are pretty good for learning and they are used in production. I think there's actually a lightweight visual studio now with an emphasis on C++ that could be a good start. If you don't, you can probably find a distribution of Eclipse that is specific for C++ and includes an implementation of the GNU compilers.
Use gcc and g++ while you're still learning these languages, a big enough task for now. If you need a specialized compiler down the road, you'll want to have much deeper understanding of the language and your problem domain to properly evaluate candidates.
I feel that if it is open source then it will be a more complete compiler since many programmer perspectives are used to make it better.
That's not necessarily true. You could also say that if you use Microsoft's compiler, it will be optimal for Windows, since Microsoft knows best how to optimize a compiler for Windows.
Microsoft has Visual C++ Express Edition which is free and ofcourse includes a nice IDE that's very well suited for Windows development.
But if you're interested in making portable software, look at GCC, which is the default compiler on Linux and which is also available on the Mac. (The iPhone works totally different and requires special tools that only run on Mac OS X). You can get GCC for Windows with Cygwin or MinGW.
Get the Visual Studio Express (easier and quicker IMO, to setup) and learn with it; when you think you know enough about C++ and how "things" work, you could start using something like QT or GCC (with cygwin) and learn to port to different platforms.
For windows u can use CodeBlocks I believe it uses gcc and its pretty user friendly
I strongly suggest going with MinGW.
It is:
Open-source
Available on all major platforms
Comes with standard Win32 headers and libraries
The key to writing portable C++ code is:
Use a cross-platform version control system (subversion is a great choice), because this makes it easier to
Compile and test your code on other platforms early and often