views:

101

answers:

3

Is there a way to make the msvc compiler as strict as gcc? MSVC lets me do some pretty crazy things that result in hundreds of errors when I compile in linux.

Thanks

+2  A: 

To get started we'd need the version you are on (for MSVC), what sort of errors you hit (compile time, link time or run time) and so on.

Assuming you are on a relatively current version (MSVC 2008 SP1) and being bugged by compiler errors, I'd suggest the following:

  • Your program's entry point is called main and not _tmain or WinMain
  • /W4: Treat warnings as errors
  • Remove any function that begins with _ -- they are implementation specific
  • No Win32 APIs either
  • No platform specific threading
  • Check if you have same signedness for char
  • How do the two implementations treat wchar_t
  • No Safe Exception Handling or inline assembly magic
  • No COM/MFC either
dirkgently
My problem is not errors in msvc, the problem is msvc lets too many things slide by while gcc is more strict
Milo
@Milo: The above list is indicative of what may cause grief. Without any example of from your side your question becomes difficult to answer.
dirkgently
+3  A: 

A better question would be: Are MSVC, g++, or any other compiler standard compliant, and if so, to the same standard version? You shouldn't rely upon a common set of non-standard behavior. Personally, I'm responsible for over 500KLOC of C++ that compile on both g++ 4.1.x and VC7.1. There can be alot of give and take from both compilers.

Both compilers have either language or library extensions. g++ tends to be better about putting library extensions in a separate namespace. VC, at least older version, not so good. Both have language extensions that are on by default, or can be enabled (or disabled) with compiler switches. You're best disabling all language extensions.

The higher the warning level on each compiler you can set, the better - and don't disregard warnings from either compiler without just reason.

For windows, by default I use the following options: /W3 /wd4355 /wd4805 /wd4710. I'd like to use /W4, but 3rd party libs make this level unbearable. For g++, I use -Wall -Wextra. I'd also like to use -Wold-style-cast, but I rely on too many 3rd party libraries that violate this one.

Each compiler warns about different constructs, or warns about them differently. You're best off to pay attention to output from both compilers and find the subset of code between them that produces zero warnings, at as high of a warning level as you can possibly set.

Nathan Ernst
+1. There are several options you can set that will help, but at the end of the day there is no substitute for building the project with both compilers on a regular basis. I've been building the same code with g++ and Visual Studio every day for 5+ years and aside from a couple of things like std::tr1 quirks and the use of `typename` there are very few things that work on one but not the other. Write portably and you shouldn't have problems.
the_mandrill
+1  A: 

The /Za (Disable Language Extensions) option disables a number of Microsoft-specific keywords and extensions.

bk1e