How to compile Windows Visual C++ code on Linux

20,230

Solution 1

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.

Solution 2

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)

Solution 3

It's hard to know without seeing the exact errors, but it is posible that your errors are not due to the makefile. Compilers are not always strictly compliant with the standard, and sometimes they tolerate certain things that are considered errors in others.

Somethings to look at: - C++11 std support. Visual studio automatically supports some features of the standard, while some (not sure if all) versions of gcc require you to explicitly pass the -std=c++11 flag. - >> when using nested templates. Old versions of gcc will give an error when they see something like:

std::vector<std::pair<bool,bool>>

because they consider the end of that to be the >> operator. So if you use an old version of gcc, try putting a space in between.

Even if your errors may not be related to the makefile build system, I'd still consider giving cmake a try if you keep encountering troubles with portability.

Share:
20,230
Paul Warnick
Author by

Paul Warnick

Updated on July 25, 2022

Comments

  • Paul Warnick
    Paul Warnick over 1 year

    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

  • Paul Warnick
    Paul Warnick almost 8 years
    Actually, that was one of the errors I was getting but I managed to remove all occurrences. I'll edit my question to post some of the error messages I'm getting.
  • Paul Warnick
    Paul Warnick almost 8 years
    I'm pretty sure I'm not using g++/gcc to compile on Windows, just the standard compiler that comes with VC, but I'll give it a shot!
  • ThisGuyCantEven
    ThisGuyCantEven almost 8 years
    This actually might not work if you are using Visual Studio, which I don't think uses g++ (which you said while I was writing this comment). I would happily post a tutorial on how to use TDM gcc/g++ and Netbeans in windows.
  • ThisGuyCantEven
    ThisGuyCantEven almost 8 years
    I think @technik was on to something too, usually I have to declare my std vectors more like what he posted. For example: using namespace std; vector<double> WiHm = {4.71,0.33,0.52,0.066};
  • Paul Warnick
    Paul Warnick almost 8 years
    I'd like to note that I had to use the flag "-std=c++0x"