Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 262
Paul Warnick Avatar asked Jun 01 '16 17:06

Paul Warnick


People also ask

How to compile C program with Visual Studio Code?

Compiling C program with Visual Studio Code using Windows Compilation Compilation is a process of converting the source code into machine or object code. It is done with the help of the compiler. The compiler checks the source code for the errors, and if no error arises, then it generates the machine code.

Can I use Visual Studio Code on Linux?

Another option for Linux development with VS Code is use a Windows machine with the Windows Subsystem for Linux (WSL). With WSL, you can install and run Linux distributions on Windows. This enables you to develop and test your source code on Linux while still working locally on a Windows machine.

How to install Linux development with C++ in Visual Studio?

Install Workload for Linux development with C++. Visual Studio 2017 and 2019 include the C/C++ Linux Development workload. To install it, start the Visual Studio installer and choose to either install or modify an existing installation. Scroll to the bottom. Under the section “Other Toolsets” you will find Linux Development with C++.

Which C++ compiler should I use for Linux development?

We generally recommend Visual Studio’s native support for CMake for all C++ Linux development. Visual Studio’s CMake support allows you to target multiple platforms (Windows, a remote Linux system, the Windows Subsystem for Linux, embedded targets…) with a single CMake project.


3 Answers

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.

like image 127
AliciaBytes Avatar answered Oct 10 '22 21:10

AliciaBytes


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)

like image 34
A.J.Bauer Avatar answered Oct 10 '22 21:10

A.J.Bauer


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.

like image 44
technik Avatar answered Oct 10 '22 22:10

technik