Compiling MuseScore with Visual Studio 2017
I've started working on issue #271135: Compile MuseScore with msvc / Visual Studio. I've already made the first PR, with initial changes allowing MuseScore to compile with no errors with the MSVC toolchain, but this is still WIP.
On the positive side:
- CMake system creates solution and projects for MSVC succesfully,
- All the projects comprising MuseScore compile successfully, and generate an executable,
- All required DLLs are copied to the output directory.
On the negative side:
- Compiling with warning level 4 (/W4) the solution still has 1257 unsolved warnings (see below),
- When run, the program stops with an exception (see below).
- The INSTALL CMake target has not been ported to the MSVC toolchain yet.
The unsolved warnings fall in several categories:
- Name hiding; many of the warnings are due to variable name reuse, where a variable defined in an inner scope hides a variable in an outer scope (or a function parameter, or a class field). To solve these, the variables should be renamed. This is a good practice anyway, in keeping with the recommendation ES.12 in the C++ Core Guidelines by Stroustrop & Sutter (http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-reuse : ES.12: Do not reuse names in nested scopes).
- Some cases of name hiding are due to duplicated variable names introduced by the macro-expansion of Qt foreach() loops. To solve these, the loops should be converted to standard C++ for(:) loops, as per the "MuseScore Coding Rules" (https://musescore.org/en/handbook/musescore-coding-rules#Loops), which tells explicitly "Use C++11's "for" instead of Qt's "foreach":" ... "If you happen to be fixing some code and see a "foreach", please change that loop into a "for"."
- Unreachable code. To solve, either eliminate the code, or conditionally exclude for MSVC builds.
- Narrowing conversions, for example from double to float. To solve, explicitly cast the result to the desired type.
- Switch statements with default, but no case labels: this is probably WIP, where the switch will be later populated. If this is the case, this warning could be selectively disabled.
- (Potentially) uninitialized variables. This indicates that MSVC could not determine if the variable was initialized prior to its first use. To solve, initialize variables.
- Assignment within conditional expression. This is not a problem per-se, but can lead to bugs. To solve, separate the assignment as a separate statement.
- There are other, less frequent warnings as well.
Exception on execution
When executing the program, an exception is triggered early in the initialization process (line 343 - libmscore/style.cpp, the QVariant::fromValue() method is throwing an exception. All QVariant::fromValue() invocations for the enum Align work flawlessly. The only difference I've noticed is that for Ms::Align, Q_DECLARE_METATYPE(Ms::Align) is invoked in elementlayout.h, whereas for Direction this is not used, and Q_ENUM_NS(Direction) is. Could this be the source of the problem? ).