Since C++ uses headers for now, sometimes one header includes another and you fail to notice it, as a result of that the code may compile with one compiler and not with the other, or maybe one feature is implemented in one compiler but not the other, so, I was wondering if there's a way to have visual studio check if the project will compile with both VS compiler and g++ somehow? Or do I just have to manually recompile with both compilers every time? If I have to recompile with both compilers, how can I automate that process with visual studio so I don't have to do it manually?
-
The solution is to include the headers you need. – juanchopanza Aug 07 '16 at 23:25
-
@juanchopanza what about features being implemented by one compiler and not the other? Keeping track of them is tedious, I want to automate this process. – Pavel Aug 07 '16 at 23:26
-
You're asking about headers, not about non-standard features. The C++ standard specifies which header(s) each name is declared or defined in. – juanchopanza Aug 07 '16 at 23:27
-
@juanchopanza I did ask about features as well. And I'm not talking about non-standard ones. – Pavel Aug 07 '16 at 23:27
-
Try using cmake to generate both a MSVS solution file and a Makefile. You'll need g++ and various gnu tools installed (try cygwin) to build via Makefile. – rubicks Aug 07 '16 at 23:29
-
@Pavel Then you need to clarify the first sentence. – juanchopanza Aug 07 '16 at 23:30
-
@rubicks okay, I'll try to look into that, I'm not very familiar with these things. – Pavel Aug 07 '16 at 23:30
-
@juanchopanza I meant if header A includes header B in one library, you can include header A and B will already be there, but if the same is not true for another library, then you have to include headers A and B, as a result if the first library is bundled with one compiler and the second with the other, then you get compiler errors is what I meant, isn't that true? – Pavel Aug 07 '16 at 23:32
-
@Pavel I know what you meant, and you claim you're talking about something else. So clarify your question. – juanchopanza Aug 07 '16 at 23:33
-
@juanchopanza I talk about both headers and features. What is there to clarify? – Pavel Aug 07 '16 at 23:33
-
OK. Then no. Because, which g++ version? Which compiler flags? Why not other compilers too? – juanchopanza Aug 07 '16 at 23:36
-
@juanchopanza why exactly do you need to know those things? I just want to be able to compile with both VC++ and G++, why do flags etc matter in this case? The question boils down to what I should look into to make visual studio compile my project with multiple compilers, I choose G++ and not other compilers because it's what I care about for now... – Pavel Aug 07 '16 at 23:40
-
@Pavel I am mentioning reasons why it would be hard for VS to implement such a feature. – juanchopanza Aug 07 '16 at 23:41
-
Almost certainly not. Too many variations of MSVC and GCC. Fortunately this is what adhering to the standard should protect you from. – user4581301 Aug 07 '16 at 23:41
-
@juanchopanza so, if I want to write cross platform things I should just use G++? – Pavel Aug 07 '16 at 23:42
-
@user4581301 yea, but everyone makes mistakes is my point... – Pavel Aug 07 '16 at 23:42
-
No. You should use all of them. It's also helpful in debugging. Each compiler will catch different mistakes different ways. – user4581301 Aug 07 '16 at 23:42
-
What the hell is wrong with people down-voting? – Pavel Aug 08 '16 at 00:32
-
Great... Now some stupid motherf*cker not only down-voted here, but went above and beyond and down-voted my other questions. Hahahaha. – Pavel Aug 08 '16 at 00:40
-
@Pavel Nothing is wrong about voting. People express their opinion on the question's quality. Some people even suggest ways for improving the question (with or without downvoting - there is no way to know who voted which way). BTW unfair downvoting will be reversed automatically in 24 hours. – anatolyg Aug 08 '16 at 00:47
-
@anatolyg I think it's pretty ridiculous. I once asked a question on math stackexchange that receieved 16 upvotes and then someone went ahead and downvoted... – Pavel Aug 08 '16 at 00:49
2 Answers
The development cycle of gcc is faster than that of MS Visual Studio. So asking Visual Studio "will my code be compiled correctly by g++?" is meaningless - it will never know which features and bugfixes the latest version of gcc has.
Also, there is no point in asking such a question at all - if you want to know "what will happen if I do X", just do X!
What you really want to know, I guess, is something like "Are my #include
directives compatible with C++ Standard?". MS Visual Studio is just an implementation - it's not a portability-checking tool. It does its best to compile the code, and do it correctly, which is a great task by itself (with c++ being so hard to compile).
gcc is yet another implementation of c++, with its own headers. Actually, from my experience, the mingw c++ headers are more "minimalistic", that is, they try to not include one-another when possible. My guess is, they try to minimize compilation time for systems that don't use precompiled headers, while MS Visual Studio regards precompiled headers as an essential feature.
So better use gcc/mingw as your "standard compliance" check.

- 26,506
- 9
- 60
- 134
VS2015 supports the clang compiler, which is generally the most compliant compiler of the three. You could compile your code with clang.
It's important to distinguish that a compiler and standard library implementation are not the same thing. i.e. you can mix and match compilers with the standard libraries that ship with different toolchains. So compiling with clang will not pick up issues with variance in standard library implementations.
To do that you would have to link against and supply the headers for the standard library implementation you would like to link against. Visual Studio, by default links against it's own runtime and includes msvc runtime headers, you would have to change your project configuration to achieve it but it should be possible. It would be a lot less hassle to simply compile it with the target platforms own toolchain, however.

- 860
- 5
- 14
-
I never used it, but the documentation suggests that it's the other way around - clang supports VS (they did extra effort to do it) – anatolyg Aug 08 '16 at 00:42
-
The MSVC IDE supports running the clang compiler. The clang compiler supports MSVC extensions to C++. – brian beuning Aug 08 '16 at 01:24