Paul Warnick Paul Warnick - 1 year ago 252
Linux Question

How to compile Windows Visual C++ code on Linux

Note: The intent of this question was to find if there exists a standard way of developing in VC for Windows and porting that code smoothly (meaning, making as few edits to the code as possible) over to a Linux system so that it can be compiled into an executable and run.

Based on the answers I've received I can see that there is a misunderstanding. Therefore, I'm going to break this question into two separate questions. One being my original (revised question) and the other being my specific issues and how to fix it. (I'll add the links to this question once they're posted).

Note: I'm pretty new to C++ and compiling with makefiles.

I've been creating a C++ program that will run on a Linux server and so far I've been using Visual Studio (Window's current release) to write all the code. To be safe, before I started writing my program I attempted to create some basic C++ files in VC, transfer them over to my server then compile them with g++ and run the executable. Everything worked out so I thought:

"Hey, I can go ahead and write my entire program, follow the same process and everything will be fine!"

Clearly, I was wrong and that's why I'm here.

When I run my makefile I get dozens of errors and I'm not exactly sure how to approach the situation. A large number of the error messages seem to be hinting at my use of vectors (which of course, run fine when compiling with VC).

I've had a look at the following questions:

How to compile in Visual Studio 2010 for Linux

Port Visual Studio C++ to Linux

Compiling Visual C++ code in Linux?

But I can't really find a direct solution to my issue (for example I'd prefer to avoid installing VC on a Linux platform and just working from there).

I've also looked into (and tried using) wineg++ but it didn't seem to change anything upon compilation.

How should I go about this issue?

Meaning: Is it common practice to develop on Windows VC then port over to Linux? If so, is there a standard way of ensuring that everything goes smoothly? Or is it just a matter of knowing how your compiler on Linux works and coding appropriately so that no errors occur?

Preferably a solution that allows me to keep developing on Windows and simply port over everything to Linux when I'm done. Also if possible, try to make any answers as simple as possible, I'm still very amateur when it comes to most of this stuff.

Edit: I'd also like to mention that I'm not really using any crazy libraries. Just math.h and vector.

Some examples of errors are:

Initializing a vector with some doubles:

enter image description here

Corresponding compilation error:

enter image description here

Answer Source

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 always compiles according to the most recent C++ standard they support and doesn't allow you to use a different standard. In the most recent version it's pretty much a mix between C++11 and C++14 I think.

  • 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download