How should I compile source code in c++ on a mac to ensure it will compile correctly in Windows Visual Studio Compiler while adhering to ANSI/ISO

StackOverflow https://stackoverflow.com/questions/17388101

  •  02-06-2022
  •  | 
  •  

Question

I am taking a class and one the expectations are that the source code we submit will compile on our professors computer. Seems reasonable. I really do not want to install windows or use visualization. One of my classmates posted that the code below will do the trick. Any thoughts on this? Can it be improved to be closer to a strict ANSI/ISO c++ compile?

$ g++ -std=c++98 -Wall -Wextra -Werror main.cpp -o outputprogram

My OS is 10.8.x g++ is:

  i686-apple-darwin11-llvm-g++-4.2

And gcc is

 i686-apple-darwin11-llvm-gcc-4.2: 
Was it helpful?

Solution

How should I compile source code in c++ on a mac to ensure it will compile correctly in Windows Visual Studio Complier while adhering to ANSI/ISO

You really can't do that. Not possible. Compilers behave slightly differently in different places and sometimes that leads to compilation failures and/or slightly different program behavior. When it comes to Microsoft Compiler, it can happily compile code that baffles G++(typically templates), and vice versa, and that's even if you don't take into account non-standard function and extensions. So, the only sure way to test compiler is to use that compiler.

Issues I personally run into when developing code that is supposed to work with g++/cl.exe (cl.exe == microsoft compiler).

  1. Compared to cl.exe, g++ is insanely slow, at least on windows. (2..5x times slower)
  2. g++ sometimes can't compile template constructs cl.exe processes just fine.
  3. cl.exe cannot process insanely large string constants (compared to g++).
  4. cl.exe (VS2008 express) cannot detect UTF8 encoding without a BOM.
  5. Mixing encodings with cl.exe (when OS uses encoding A, source file is in encoing B, and you want to display wchar_t string using function that requires encoidng C) is a nightmare.
  6. C standard library functions are different on cl.exe/g++. One compiler can have a function that isn't present on another compiler (some of them don't have some string *ncmp function), compiler might have non-standard functions that isn't present in another compiler (strcpy_s), and certain functions might behave differently with certain arguments (sprintf with "%S")
  7. passing -Wall to cl.exe produces thousands of warnings for harmless code if windows.h is included. Those warnings cannot be fixed.

There were probably more, but that's what I can remember at the moment.

I really do not want to install windows

You could try using Wine to install Microsoft Compiler onto MacOS. I would expect that you won't be able to install Visual Studio IDE using wine (your mileage may wary), but you should be able to run command line-only microsoft compiler that is shipped with Windows Driver Kit. To use compiler this way you must be really familiar with it, though, so installing windows into virtual machine will be probably much easier.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top