Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there compiler settings in Visual Studio 2010 to ensure the writing of portable C++?

I receive C++ source code from a developer who is compiling using Visual Studio 2010, that I then need to recompile under various different compilers: GCC, LLVM, other versions of Visual Studio, etc. Sometimes the code that he sends me (that compiles without warnings in VS2010) fails to compile under the other compilers.

Are there any compiler settings he can set in VS2010 to increase the likelihood that his code will be cleanly portable?

like image 702
Rich Avatar asked Mar 15 '13 15:03

Rich


People also ask

What C compiler does Visual Studio use?

Microsoft C++ Compiler (MSVC) This is the default compiler for most Visual Studio C++ projects and is recommended if you are targeting Windows.

Does Visual Studio include C compiler?

The Visual Studio build tools include a C compiler that you can use to create everything from basic console programs to full Windows Desktop applications, mobile apps, and more.

How do I run C code in Visual Studio?

After writing the code, right-click on the program, as shown below. Click on the Run Code option or press Ctrl + Alt + N from the button.

How do I select a compiler in Visual Studio?

In Visual Studio You can set compiler options for each project in its Visual Studio Property Pages dialog box. In the left pane, select Configuration Properties, C/C++ and then choose the compiler option category.


1 Answers

At the language level, there is no silver bullet. The best you can do is to stick to the language standard as closely as possible. Most compilers have options to issue warnings or errors if you take advantage of an extension that's specific to a particular compiler (with Visual C++, /Za will disable non-standard language extensions). But this isn't perfect, as no compiler yet implements absolutely 100% of the standard, so you can still have portability problems even with strictly-compliant code.

Also be aware that lots of everyday code actually takes advantage of extensions or undefined- or compiler-defined behaviors, often without realizing it, so it might not be practical to compile in a fully standards-compliant mode.

You also have to be aware of things that the standards allow to be different. For example, types like int may be different sizes on different systems. Windows is LLP64, while most Unix-derived OSes are LP64.

At the system level, I don't know of a perfect way to make sure a programmer is not relying on something system-specific (e.g., <windows.h> or <pthreads.h>).

Your best bet is to make it simple for all developers to run test builds on all target platforms.

like image 167
Adrian McCarthy Avatar answered Sep 30 '22 17:09

Adrian McCarthy