I am a student and I am currently taking a compiler construction course. I develop my compiler in C++ on Ubuntu, using GCC and CMake. While everything works fine on my development machine, the code broke horribly when compiled against MSVC or Visual Studio 2017 on the school test. As I tracked down the error messages thrown by MSVC, I noticed there are several issues causing these failures:
- GCC's headers give you more stuffs than MSVC does.
- Some other weird errors that I can't explain (Sorry but I can find a better word).
To ground the discussion, see the following examples, which work for GCC but break on MSVC.
#include <iostream>
int main() {
std::string S;
std::getline(std::cin, S);
}
MSVC requires #include <string>
to work.
#include <unordered_map>
int main() {
std::min(1, 2);
}
MSVC requires #include <algorithm>
to work.
These issues are not hard to fix once they break out: I can just #include
the required headers. However, it is too expensive to let them break out. It takes away my scores and hurt me.
I have no interest in discussing which compiler is more standard-compliant. I simply want to catch any portability issue before they catch me. But I am not well-informed of all those tricky differences between these compilers. That's why I am asking for tools to catch these issues. I want to update my tool chains to write more portable code.
Edit: Besides tools, I will humbly learn any code disciplines, good practices and known issues in writing portable code.
Edit: Sorry folks. I am not meant to make the second example wrong (just deleted). I just have no access to a MSVC to reproduce the problems.
Edit for Clarification: What this post is not for:
- How to fix certain issues when porting code from GCC to MSVC.
- How to write standard-compliant C++ code that universally compiles.
In fact, this post is asking for practical actions one can take to port code from GCC to MSVC or better, to write code without portability issues from the beginning. The examples in this post are used to make discussion concrete or to show the actual difficulty, but not comprehensive. I don't think there is a single truth about this question, but I'd like to try out some good ones.