1

I want to learn C++ and I want to know if using Microsoft VC++ will tie me to Windows. I specially need my project to also run on Mac. My projects are all Class libraries and have no dependency to UI. They are just calculations or file/network IO.

I really like working in Visual Studio and Microsoft way overall. So my priority is to go VC++ unless it makes painful to build cross platform apps for Windows and Mac. I mean at the compiler, libraries and language level. I don't want my parallel app to fall apart on certain platforms.

morteza khosravi
  • 1,599
  • 1
  • 20
  • 36
  • 1
    Do you mean the Visual Studio IDE or the actual compiler/linker? You can use the IDE for cross-platform development, or any other IDE, but it's some work to get it up and running. Besides, if you want to use any OSX specific library or framework you're out of luck if you want to program in C++ as you need to develop on an actual OSX system using Xcode for full access to the OSX specific frameworks. – Some programmer dude May 03 '16 at 06:33
  • 1
    Microsoft has its own compiler for C++ (MSVC), LINUX got GCC and CLANG (which also exist in WINDOWS), and MAC also got them, the only thing I'd worry about is standard compliance, as long as you are writing with compliance to the standard, it will build normally, if you slip into MSVC way, it may not build, as they all differe a little ins some approaches. – Kiloreux May 03 '16 at 06:34
  • @JoachimPileborg I want to use Visual Studio on my PC but the project has to work on PC and Mac. It has calculation, multithreading and file and network I/O and I hope they can be done a cross-platform way. – morteza khosravi May 03 '16 at 06:50
  • @Kiloreux Thanks. Can you suggest a book to start? – morteza khosravi May 03 '16 at 07:05

2 Answers2

4

I personnaly use Visual Studio to build my code on Windows and other compilers to build the same code on other platforms (Mac, Linux, Android...).

To be sure you don't get locked with Windows, make sure (at least, it's not exhaustive):

  • You don't use any win32 API (prefer cross plateforme libraries like boost for instance for network, file system access...)
  • You don't use 3rd party libraries only available for Windows
  • Don't use CString, or anyother Microsoft class. Prefer STL.
  • Be careful with file system case sensitiveness too! (#include "Foo.h" while file is "foo.h" on disk will work on PC, not under Mac/Linux). Prefer naming ALL your files in lower case.
  • ...

You may want to have a look at CMake. This tool can help you generating compilation project file for the same source to be compiled on different plateform. Then, you can generate vcproj/sln files to compile your code with Visual Studio on Windows, MakeFile(s) to compile it under Linux and XCode project files to compile it under MacOS. If somewone wants to compile you code under Windows with another compiler than Microsoft, it's also possible!

Also note that recent version of VS (2015) propose to compile for other targets that Windows (at least Android). But never tried that.

jpo38
  • 20,821
  • 10
  • 70
  • 151
  • I saw on wikipedia that GCC can compile on Windows, Mac and PC. Isn't that enough or CMake does something else? – morteza khosravi May 03 '16 at 07:23
  • 1
    @mortezakhosravi: CMake will help you generating Makefile for GCC. You'll maintain CMakeLists.txt files instead of Makefiles. This will help you move from one compiler to another in the end. – jpo38 May 03 '16 at 07:25
  • 1
    @mortezakhosravi: You're mixing two levels of build tools. CMake and also Make are tools which figure out which files should be sent to GCC or Clang for compilation. With Visual Studio, the IDE hides the details, but there too the MSBuild engine decides what files should be compiled. – MSalters May 03 '16 at 08:07
  • @MSalters Now I got the picture. Thanks – morteza khosravi May 03 '16 at 08:58
2

The compiler itself is pretty standard confroming by know (if you turn off the extensions) and more importantly, you can use the clang frontend (which is the default compiler on Mac) directly from within VS2015, so it is definitvely possible to write cross-platform code in VS (although it's certainly easier to use Windows specific constructs than it where if you'd use e.g. cygwin).

However, the windows API for network I/O is different from the one for Mac, so you should probably use a corssplatform library like boost asio for that.

As far as multithreading is concerned, you can do it in a standard and portable manner using the standard library's functions, classes and synchronization primitives, but the past has shown that they are often not the most efficient way to go. However, in most situations you'll want to use a more highlevel library for parallel programming anyway. The default on Windows is the PPL which is - I believe - not cross-platform, but there are other libraries you can use

Finally, you can use VS to remotely build your applications on Mac or Linux PCs using their local compilers and libraries. However, I don'T know, how well that works in practice.

Community
  • 1
  • 1
MikeMB
  • 20,029
  • 9
  • 57
  • 102