How to compile Windows Visual C++ code on Linux
As long as you write your code in a portable manner (not using OS/compiler specific functionality like windows.h
or specific compiler extensions) and use portable libraries it should compile with both Visual studio and GCC.
The thing is that whilst they both work, they do so a little differently. Mostly it's different default settings, that you might have to explicitly override. As an example:
Visual Studio 2017 and later defaults to C++14. Use the /std option to specify a different standard.
GCC has a default set, but allows you to change the standard you use for compilation. Compiling with
g++ example.cpp
uses the default standard (C++98 standard for GCC before version 6 and C++14 for GCC after version 6). Your code seems to fail because you use C++11 features but your compiler uses an older standard by default.
g++ -std=c++11 example.cpp
should make some of your errors disappear by explicitly specifying the standard, in this case C++11 standard.
These are just different trade offs that the compilers choose. Having only one standard supported probably makes the support and fixing errors easier, since you don't have different compiler + standard version combinations that could possibly have different bugs. Being able to change the standard used for compilation makes it easier to test if a program works well with a new standard or what breaking changes you have to fix etc...
In general GCC is more of minimal and has you explicitly specify it if you want it to do some extra stuff. I'd recommend using something like:
g++ -Wall -Wextra -pedantic -std=c++11 example.cpp
Using -Wall
and -Wextra
give a decent warning level to start out with -pedantic
tells you if you're trying to use a compiler extension that works but would make your code less portable, and last but not least you should always specify the standard you want to use, be it -std=c++11
, -std=c++14
or the older -std=c++98
.
You might also like to check out the possibility of developping and remote debugging using VS 2015 and the Linux Development extension. Visual C++ for Linux Development (March 30, 2016)