简体   繁体   中英

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:

在此处输入图片说明

Corresponding compilation error:

在此处输入图片说明

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)

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.

In both machines (windows and linux) open a terminal and type "g++ --version" and "gcc --version" to make sure you are using the same compiler version, this may not be related to your problem but it is worth a look. I think your problem is the syntax of the initialization. I had never initialized an std::vector before by just using a variable name and brackets. Your declaration does not include the type as far as I can tell. I tested both ways using my TDM g++ 5.1. and leaving out std::vector<double> from std::vector<double> WiHm = {4.71,0.33,0.52,0.066}; caused a compiler error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM